import { Router402Sdk } from "@router402/sdk";
import { baseSepolia } from "viem/chains";
import { signTypedData } from "viem/actions";
const USDC_BASE_SEPOLIA = "0x036CbD53842c5426634e7929541eC2318f3dCF7e";
const sdk = new Router402Sdk({
chain: baseSepolia,
pimlicoApiKey: process.env.PIMLICO_API_KEY!,
});
async function onboard(walletClient, eoaAddress: string) {
// 1. Setup account (deploy + session key)
const setup = await sdk.setupAccount(walletClient, eoaAddress, {
usdcAddress: USDC_BASE_SEPOLIA,
onStatus: (s) => console.log("Status:", s),
});
// 2. Authorize with server (get JWT)
const backendData = sdk.exportSessionKeyForBackend(setup.sessionKey);
const signature = await signTypedData(walletClient, {
domain: {
name: "Router402 Authorization",
version: "1",
chainId: BigInt(sdk.getChainId()),
},
types: {
Authorization: [
{ name: "smartAccountAddress", type: "address" },
{ name: "privateKey", type: "string" },
{ name: "serializedSessionKey", type: "string" },
{ name: "eoaAddress", type: "address" },
{ name: "chainId", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
},
primaryType: "Authorization",
message: {
smartAccountAddress: setup.info.address,
privateKey: backendData.privateKey,
serializedSessionKey: backendData.serializedSessionKey,
eoaAddress,
chainId: BigInt(sdk.getChainId()),
nonce: 0n,
},
});
const authResponse = await fetch("https://api.router402.xyz/v1/authorize", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-authorization-signature": signature,
},
body: JSON.stringify({
smartAccountAddress: setup.info.address,
privateKey: backendData.privateKey,
serializedSessionKey: backendData.serializedSessionKey,
eoaAddress,
chainId: sdk.getChainId(),
nonce: 0,
}),
});
const { data } = await authResponse.json();
// 3. Make a chat completion request
const chatResponse = await fetch("https://api.router402.xyz/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${data.token}`,
},
body: JSON.stringify({
model: "anthropic/claude-sonnet-4.5",
messages: [{ role: "user", content: "Hello, Router402!" }],
}),
});
const result = await chatResponse.json();
console.log("Response:", result.choices[0].message.content);
}