How to initialize and use the Wallet API client
☞ The renaming of “Ledger Live” to “Ledger Wallet” and “wallet” (in the hardware sense) to “signer” is still in progress. This page may contain legacy references that will be updated.
This guide shows you how to create a WalletAPIClient in a Live App, connect it to Ledger Wallet, list the user’s accounts, and disconnect when you are done.
Prerequisites:
- A Live App that runs inside Ledger Wallet, or in the Simulator.
account.listin thepermissionsof your manifest. The server rejects calls to methods that are not in the permission list.
Step 1: Install the client
Install @ledgerhq/wallet-api-client. The package also exports everything from @ledgerhq/wallet-api-core, including WindowMessageTransport.
npm install @ledgerhq/wallet-api-clientStep 2: Create the transport and the client
WindowMessageTransport sends and receives messages through the browser window. Create it, call connect() to start listening, then pass it to WalletAPIClient:
import {
WalletAPIClient,
WindowMessageTransport,
} from "@ledgerhq/wallet-api-client";
const transport = new WindowMessageTransport();
transport.connect();
const walletApiClient = new WalletAPIClient(transport);Step 3: Call a method
The client exposes one module for each group of methods, for example walletApiClient.account and walletApiClient.currency. This call lists the accounts that the user added in the wallet:
const accounts = await walletApiClient.account.list();
console.log(accounts);Each call can throw an error that the server sends back. Wrap your calls in try and catch if you want to show a message to the user.
Step 4: Disconnect when you are done
Call disconnect() to remove the transport listeners:
transport.disconnect();Complete example
import {
WalletAPIClient,
WindowMessageTransport,
} from "@ledgerhq/wallet-api-client";
async function listAccounts() {
const transport = new WindowMessageTransport();
transport.connect();
const walletApiClient = new WalletAPIClient(transport);
try {
const accounts = await walletApiClient.account.list();
console.log(accounts);
} catch (error) {
console.error("Could not list accounts:", error);
} finally {
transport.disconnect();
}
}
listAccounts();You now have a connected client that can call any method your manifest allows. Next, see the module references for the methods you can call, starting with the Account module and the Currency module. If you use React, see Use the Wallet API in a React app.
Related
- Manifest: declare the permissions that your Live App needs
- Wallet API Simulator: run your Live App in a browser without Ledger Wallet
- Wallet API Client: an overview of the client