☞ The renaming of “Ledger Live” to “Ledger Wallet” is still in progress in the repos.
This page may contain references to “Ledger Live” that will be updated soon.
Use the Wallet API in a React app
This guide shows how to integrate the Ledger Wallet API in a React app: install the packages, configure the transport, wrap your app, and optionally test with the simulator.
Prerequisites
- A React app (client-side)
- Node.js and a package manager (npm, yarn or pnpm)
Install packages
Install the base client:
npm install @ledgerhq/wallet-api-clientInstall the React helper package with hooks:
npm install @ledgerhq/wallet-api-client-reactOptional: install the simulator for server-side testing:
npm install @ledgerhq/wallet-api-simulatorNote: the simulator currently works only in server-side environments because it depends on process.
Configure the transport
Create a transport provider to enable communication with the Ledger Wallet API. Add a TransportProvider.tsx component that uses the Wallet API client to establish a window message transport (SSR-safe).
import { WalletAPIProvider } from "@ledgerhq/wallet-api-client-react";
import { Transport, WindowMessageTransport } from "@ledgerhq/wallet-api-client";
function TransportProvider({ children }) {
function getWalletAPITransport(): Transport {
if (typeof window === "undefined") {
return {
onMessage: undefined,
send: () => {},
};
}
const transport = new WindowMessageTransport();
transport.connect();
return transport;
}
const transport = getWalletAPITransport();
return (
<WalletAPIProvider transport={transport}>{children}</WalletAPIProvider>
);
}
export default TransportProvider;How it works
- Import the client React provider and transport types from the Ledger Wallet API packages.
- Create
TransportProviderto render its children insideWalletAPIProvider. - Use
getWalletAPITransportto supply a transport: during SSR, return a no-op transport; in the browser, useWindowMessageTransportand callconnect(). WindowMessageTransportcommunicates with the Ledger Wallet API via window messages.WalletAPIProviderreceives the transport so the rest of the app (its{children}) can access the API.
Wrap your app
Wrap your root <App /> component with the TransportProvider:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import TransportProvider from './TransportProvider';
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<TransportProvider>
<App />
</TransportProvider>
</React.StrictMode>
);With this setup, your app can access the Ledger Wallet API via the WalletAPIProvider context.
Test with the simulator (optional)
To test without connecting to the Ledger Wallet App directly, use @ledgerhq/wallet-api-simulator in a server-side environment.
npm install @ledgerhq/wallet-api-simulatorRemember: it currently only functions server-side due to a dependency on process.
Next steps
- Make your Live App ready to be registered by creating your manifest.
- Learn how to create your manifest: how to create your manifest