Skip to main content
The b402 protocol enables you to monetize your APIs or web services with gasless BEP-20 token payments on the BNB Chain. This guide walks you through integrating the b402-express middleware to create paid API routes that require crypto payments before access. Currently, a TypeScript SDK is available for developers. Additional SDKs including Python will be released soon.
b402 does not require you to set up API keys. The b402 facilitator verifies signatures and submits transactions.

Installation

First, install the seller middleware package:
npm install b402-express

Basic Setup

Import and configure the middleware in your Express app, specifying your wallet address and the services you want to sell:
const express = require('express');
const { b402 } = require('b402-express');

const app = express();

app.use(b402(
  { payTo: '0x...' }, // your BNB Chain wallet address
  {
    'GET /premium': { price: '0.01', token: 'USD1' },
    'GET /basic': { price: '0.001', token: 'USDT' },
  }
));

Route Configuration

Protected/Paid Routes

Endpoints listed in the middleware configuration require payment before access. When called without valid proof of payment, the middleware automatically returns an HTTP 402 (Payment Required) response containing all necessary payment details.

Free Routes

Any route not listed in the configuration remains free and accessible as usual.

Payment Details

When a protected endpoint is accessed without payment, the middleware returns a JSON payload with:
  • Token contract address
  • Your receiving wallet (payTo)
  • Required amount
  • Relayer contract address (0xE1C2830d5DDd6B49E9c46EbE03a98Cb44CD8eA5a)
  • Description of the payment
After payment, verified details become available in the request object:
req.b402.txHash  // transaction hash on the BNB Chain
req.b402.payer   // payer’s wallet address

Supported Tokens

For the complete list of supported tokens across both BNB Chain Mainnet and Testnet, see the Supported Tokens page.