Skip to content

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.

Terminal window
npm create vite@latest my-d9-dapp -- --template vanilla-ts
cd my-d9-dapp
npm install
npm install --save-exact @d9-network/dapp-connector@0.2.0 effect@4.0.5

Create .env.local using the mainnet values from Mainnet Endpoints:

Terminal window
VITE_D9_RPC_URL=wss://rpc-hermes.d9network.com
VITE_D9_GENESIS_HASH=0xc3140da27f348abd94a638e9ca7ee7e9005caf5f2b451044c9487905ea5e0991
VITE_D9_RELAY_URL=wss://relay.d9network.com
VITE_D9_PROJECT_ID=d9-wallet-public
VITE_D9_WALLET_SCHEME=d9wallet

Create 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"),
};

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);

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;
}),
);
}

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);
}
});
  1. Run npm run dev, open the page on your computer, and click Connect Wallet.
  2. Render qrUri with any QR library (such as qrcode) and scan it with D9 Wallet. Never write pairing URIs to logs or analytics.
  3. 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.

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.