Getting Started

This guide covers everything you need to go from installation to making your first chat completion request with Router402.

Prerequisites

Installation

npm install @router402/sdk viem

Or with Bun:

bun add @router402/sdk viem

Configuration

Create an SDK instance:

import { Router402Sdk } from "@router402/sdk";
import { baseSepolia } from "viem/chains";

const sdk = new Router402Sdk({
  chain: baseSepolia,
  pimlicoApiKey: process.env.PIMLICO_API_KEY!,
});

Configuration Options

Option
Type
Required
Default
Description

chain

Chain

Yes

--

Target blockchain (from viem/chains)

pimlicoApiKey

string

Yes

--

Pimlico API key for bundler and paymaster

entryPointVersion

"0.7"

No

"0.7"

ERC-4337 entry point version

sessionKeyValidityPeriod

number

No

31536000

Session key validity in seconds (1 year)

Setup Flow

The complete setup flow from wallet connection to making API calls:

spinner

Step 1: Create a Wallet Client

Step 2: Setup Account (Deploy + Session Key)

The setupAccount() method handles deployment, session key generation, approval, and on-chain enablement in a single call:

Step 3: Authorize with Server

Sign an EIP-712 message and send the session key data to the Router402 server to receive a JWT token:

Step 4: Make Chat Completion Requests

Use the JWT token for authenticated chat completions:

Streaming Example

Complete Example

Putting it all together:

Error Handling

The SDK throws SmartAccountError for domain-specific errors:

Error Types

Type
Description

NOT_CONFIGURED

SDK not properly configured

DEPLOYMENT_FAILED

Smart account deployment failed

INSUFFICIENT_FUNDS

Not enough funds for operation

USER_REJECTED

User rejected the wallet prompt

NETWORK_ERROR

Blockchain network error

SESSION_KEY_NOT_APPROVED

Session key has not been approved

INVALID_SESSION_KEY

Session key data is invalid

SESSION_KEY_EXPIRED

Session key has passed its expiration

Last updated