Quickstart
Build a web page with a Connect Wallet button that retrieves the user’s D9 accounts. It works seamlessly inside D9 Wallet’s in-app browser and, via WalletConnect, in any desktop or mobile browser.
Prerequisites: Node.js with npm, and D9 Wallet installed on your phone.
1. Create the project
Section titled “1. Create the project”npm create vite@latest my-d9-dapp -- --template vanilla-tscd my-d9-dappnpm installnpm install --save-exact @d9-network/dapp-connector@0.2.0 effect@4.0.52. Configure environment variables
Section titled “2. Configure environment variables”Create .env.local using the mainnet values from Mainnet Endpoints:
VITE_D9_RPC_URL=wss://rpc-hermes.d9network.comVITE_D9_GENESIS_HASH=0xc3140da27f348abd94a638e9ca7ee7e9005caf5f2b451044c9487905ea5e0991VITE_D9_RELAY_URL=wss://relay.d9network.comVITE_D9_PROJECT_ID=d9-wallet-publicVITE_D9_WALLET_SCHEME=d9walletCreate src/config.ts to validate and export them:
function required(name: string): string { const value = import.meta.env[name]?.trim(); if (!value) throw new Error(`Missing environment variable: ${name}`); return value;}
export const d9Config = { rpcUrl: required("VITE_D9_RPC_URL"), expectedGenesisHash: required("VITE_D9_GENESIS_HASH"), relayUrl: required("VITE_D9_RELAY_URL"), projectId: required("VITE_D9_PROJECT_ID"), walletScheme: required("VITE_D9_WALLET_SCHEME"),};3. Create the wallet runtime
Section titled “3. Create the wallet runtime”Create src/wallet.ts. Initialize the runtime once at the application root and import it wherever needed:
import { ManagedRuntime } from "effect";import { D9DAppConnector } from "@d9-network/dapp-connector";import { d9Config } from "./config";
export const walletLayer = D9DAppConnector.layer({ dappName: "My D9 DApp", walletConnect: { projectId: d9Config.projectId, relayUrl: d9Config.relayUrl, metadata: { name: "My D9 DApp", description: "My D9 decentralized application", url: window.location.origin, icons: [`${window.location.origin}/icon.png`], }, },});
export const walletRuntime = ManagedRuntime.make(walletLayer);4. Write the connect function
Section titled “4. Write the connect function”Create src/connect.ts. Setting transport: "auto" uses the injected in-app wallet when present and falls back to WalletConnect in external browsers:
import { Effect } from "effect";import { D9DAppConnector } from "@d9-network/dapp-connector";import { buildD9WalletConnectAppLink } from "@d9-network/dapp-connector/protocol";import { d9Config } from "./config";import { walletRuntime } from "./wallet";
export async function connectWallet( showPairing: (qrUri: string, deepLink: string, cancel: () => Promise<void>) => void,) { return walletRuntime.runPromise( Effect.gen(function* () { const connector = yield* D9DAppConnector; const start = yield* connector.connect({ transport: "auto", requiredMethods: ["d9_signMessage", "d9_signTransaction"], });
if (start.transport === "walletconnect") { if (start.uri) { const deepLink = buildD9WalletConnectAppLink(start.uri, d9Config.walletScheme); showPairing(start.uri, deepLink, () => walletRuntime.runPromise(start.cancel)); } yield* start.approval; }
return yield* connector.getAccounts; }), );}5. Wire up the connect button
Section titled “5. Wire up the connect button”Replace src/main.ts. Always invoke connectWallet from an explicit user click—mobile wallets and browsers require a user gesture to open deep links or prompts.
import { connectWallet } from "./connect";
const app = document.querySelector<HTMLDivElement>("#app")!;const button = document.createElement("button");const status = document.createElement("p");const qrContainer = document.createElement("div");button.textContent = "Connect Wallet";app.replaceChildren(button, status, qrContainer);
function renderPairingQr(qrUri: string, container: HTMLElement) { // Replace with your preferred QR library (for example, qrcode.toCanvas). // Never log pairing URIs to the console or analytics in production. container.textContent = "Render QR code for WalletConnect pairing"; void qrUri;}
button.addEventListener("click", async () => { try { const accounts = await connectWallet((qrUri, deepLink, cancel) => { renderPairingQr(qrUri, qrContainer); const link = document.createElement("a"); link.href = deepLink; link.textContent = "Open D9 Wallet"; const cancelBtn = document.createElement("button"); cancelBtn.textContent = "Cancel"; cancelBtn.onclick = () => void cancel(); status.replaceChildren(link, " ", cancelBtn); }); qrContainer.replaceChildren(); status.textContent = accounts.map((account) => account.address).join(", "); } catch (error) { qrContainer.replaceChildren(); status.textContent = String(error); }});Verify
Section titled “Verify”- Run
npm run dev, open the page on your computer, and click Connect Wallet. - Render
qrUriwith any QR library (such asqrcode) and scan it with D9 Wallet. Never write pairing URIs to logs or analytics. - Approve the request in D9 Wallet. The page displays your approved SS58 addresses.
In a mobile browser, tap Open D9 Wallet instead of scanning a QR code. Inside D9 Wallet’s in-app browser, the approval prompt appears immediately. If connection fails, see Troubleshooting.
Use a single transport
Section titled “Use a single transport”If you do not want transport: "auto", replace the body of Effect.gen in src/connect.ts with one of the following snippets.
In-app browser only (injected PAPI):
const connector = yield* D9DAppConnector;const isAvailable = yield* connector.papi.isAvailable;if (!isAvailable) { throw new Error("No injected D9 provider found");}yield* connector.papi.connect();return yield* connector.getAccounts;WalletConnect only (external browsers):
const connector = yield* D9DAppConnector;const start = yield* connector.walletConnect.connect({ requiredMethods: ["d9_signMessage", "d9_signTransaction"],});if (start.uri) { const deepLink = buildD9WalletConnectAppLink(start.uri, d9Config.walletScheme); showPairing(start.uri, deepLink, () => walletRuntime.runPromise(start.cancel));}yield* start.approval;return yield* connector.getAccounts;When the user dismisses the QR modal, invoke the cancel callback passed to showPairing.
Verify: The page prints your SS58 addresses. If you see No injected D9 provider found, the page is not running inside D9 Wallet’s in-app browser—use WalletConnect instead.
Next steps
Section titled “Next steps”- Session Lifecycle: subscribe to account changes and handle disconnects
- Sign Messages
- Transactions & Finality
- Sign-In (SIWX)
