SDK

@relai-fi/x402

Turn any API into a paid API — with one fetch call.

The SDK automatically handles:

402 responses
payment signing
request retry
settlement via facilitator

No API keys. No billing. No backend changes.

Install

terminal
$ npm install @relai-fi/x402

10-second example

client.ts
import { createX402Client } from "@relai-fi/x402/client"

const client = createX402Client({
  wallets: {
    solana: solanaWallet,
    evm: evmWallet,
  },
})
// this endpoint requires payment
const res = await client.fetch("https://api.example.com/protected")
const data = await res.json()
That's it.
x402 — live demo
Click "Try request" to see the x402 flow →

What just happened?

If the API returns 402 Payment Required, the SDK automatically:

signs the payment
retries the request
returns the response
ClientAPI402sign paymentretry200 OK

All handled automatically.

React example

PayButton.tsx
import { useRelaiPayment } from "@relai-fi/x402/react"

const { fetch, isLoading } = useRelaiPayment({ wallets })
<button onClick={()=> fetch("/api/protected")}>
  {isLoading ? "Paying..." : "Access API"}
</button>

Supported Networks

The SDK works across:

SolanaSPL
Base · Ethereum · Polygon · AvalancheEVM
SKALEGasless

All payments use USDC. Gas is sponsored via facilitator — users don't need native tokens.

Why this SDK exists

Without x402
API keys
subscriptions
billing systems
With x402
pay per request
no accounts
no API keys
native agent payments

Configuration

client.ts
createX402Client({
  wallets: {
    solana: solanaWallet,
    evm: evmWallet,
  },
  facilitatorUrl: "https://facilitator.x402.fi", // optional
  preferredNetwork: "base",            // optional
  maxAmountAtomic: "5000000",           // $5 USDC cap
  verbose: true,                    // log payment flow
})

Wallet adapters supported:

Solana Wallet Adapterwagmi / viem

Server — Express

Add payment requirements to any Express endpoint.

server.ts
import express from "express"
import { relai } from "@relai-fi/x402"

const app = express()

app.use(relai({
  price: "0.01",
  network: "base",
}))

app.get("/protected", (req, res) => {
  res.json({ data: "paid content" })
})

Per-endpoint pricing

api.ts
app.get("/premium", relai({ price: "0.05" }), handler)

How it works (under the hood)

01API returns 402 Payment Required
02SDK reads payment requirements from headers
03Wallet signs payment (EIP-3009 or SPL)
04Facilitator verifies and settles on-chain
05Request succeeds with 200 OK

Advanced

Smart wallets
supported out of the box
no private keys in your code
Gasless mode
automatic via facilitator
no native tokens required
Multi-chain
same SDK works across all networks

Next steps

    @relai-fi/x402 SDK — RelAI Documentation | RelAI