Next.js Integration

This guide covers setting up @bsv/simple in a Next.js application with both browser wallet (client components) and server wallet (API routes).

1. Install Dependencies

npm install @bsv/simple

Note: @bsv/sdk is NOT needed as a direct dependency — @bsv/simple wraps it entirely.

2. Configure next.config.ts

This is required. Without it, Turbopack will try to bundle server-only packages (@bsv/wallet-toolbox, database drivers) for the browser, causing build failures.

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  serverExternalPackages: [
    "@bsv/wallet-toolbox",
    "knex",
    "better-sqlite3",
    "tedious",
    "mysql",
    "mysql2",
    "pg",
    "pg-query-stream",
    "oracledb",
    "dotenv"
  ]
};

export default nextConfig;

3. Browser Wallet (Client Components)

Basic Page

Auto-Check MessageBox on Connect

4. Server API Routes (Handler Factories)

All server routes use pre-built handler factories — no boilerplate needed. Each factory handles lazy initialization, key persistence, error handling, and all API actions automatically.

Server Wallet

API endpoints:

  • GET ?action=create — Server identity key + status

  • GET ?action=request&satoshis=1000 — BRC-29 payment request

  • GET ?action=balance — Output count + total satoshis

  • GET ?action=status — Key persistence status

  • GET ?action=outputs — List outputs

  • GET ?action=reset — Reset wallet

  • POST ?action=receive body: { tx, senderIdentityKey, derivationPrefix, derivationSuffix, outputIndex }

Custom config:

Identity Registry

DID Resolver

Credential Issuer

Key Persistence

Server wallet private keys persist automatically:

  1. process.env.SERVER_PRIVATE_KEY — Environment variable (production)

  2. .server-wallet.json file — Persisted from previous run (development)

  3. Auto-generated via generatePrivateKey() — Fresh key (first run)

No @bsv/sdk import needed.

5. Client-Side Funding Flow

6. .gitignore

Add these entries to prevent committing secrets:

7. Environment Variables

For production deployments, set the server wallet key as an environment variable instead of using file persistence:

Common Issues

Problem
Solution

Build fails with "Can't resolve 'fs'"

Add serverExternalPackages to next.config.ts

Import error for @bsv/simple/server

Use handler factories (static imports work) or dynamic await import() for lower-level access

Last updated