IntroductionUsing PetraConnecting to Petra WalletSending a TransactionSigning MessagesEvent ListeningIdentifying Errors

Connecting to Petra Wallet

To use Petra Wallet with your dApp, your users must first install the Petra Wallet Chrome extension in their browser. Petra Wallet injects an aptos object into the window of any web app the user visits.

To check if the user has installed Petra Wallet, perform the below check:

const isPetraInstalled = window.aptos;

If Petra Wallet is not installed, you can prompt the user to first install Petra Wallet and provide the below installation instructions. For example, see below:

const getAptosWallet = () => { if ('aptos' in window) { return window.aptos; } else {'', `_blank`); } };

Installing Petra Wallet

To install Petra Wallet on your Chrome browser, you can either:

  • Download the latest stable version from the Chrome Web Store
  • Install the latest development release from GitHub

The first option offers simplicity and reliability; the second gives you an opportunity to experiment with the Petra Wallet itself.

Connecting to Petra Wallet

After confirming that the web app has the aptos object, we can connect to Petra Wallet by calling wallet.connect().

When you call wallet.connect(), it prompts the user to allow your web app to make additional calls to Petra Wallet, and obtains from the user basic information such as the address and public key.

See the example code below:

const wallet = getAptosWallet(); try { const response = await wallet.connect(); console.log(response); // { address: string, address: string } const account = await wallet.account(); console.log(account); // { address: string, address: string } } catch (error) { // { code: 4001, message: "User rejected the request."} }

NOTE: After the user has approved the connnection for the first time, the web app's domain will be remembered for the future sessions.

Disconnecting Petra Wallet

When you want the web app to forget about the connection status with Petra Wallet, you can do this by calling wallet.disconnect() in your web app. See below:

await wallet.disconnect();

NOTE: After disconnecting, the web app must reconnect to Petra Wallet to make requests.


Using Petra


Sending a Transaction


© 2023 Petra. All Rights Reserved.