SDK
A TypeScript SDK for building payment-gated APIs using the X402 protocol on the Cronos blockchain.
The X402 SDK enables developers to monetize their APIs with per-request micropayments. When a client calls your API without payment, they receive the payment requirements. Once they submit a valid payment, your function executes and settlement happens automatically on-chain.
Key Benefits:
Simple one-function setup for payment-gated endpoints
Automatic payment verification and settlement
USDC stablecoin payments on Cronos
Built on Hono for high performance
Installation
npm install @axicov/x402-cronos-sdkbun add @axicov/x402-cronos-sdkyarn add @axicov/x402-cronos-sdkQuick Start
Create a paid API in under 10 lines of code:
import { createx402Tool } from "@axicov/x402-cronos-sdk";
createx402Tool(
async (input: { prompt: string }) => {
const response = await generateText(input.prompt);
return { result: response };
},
{
price: 0.01,
devWallet: "0xYourWalletAddress",
port: 8000,
}
);Your API is now live at http://localhost:8000/send and requires 0.01 USDC per request.
Configuration
X402Config
price
number
Yes
Price per request in USDC (e.g., 0.01 = 1 cent)
devWallet
string
Yes
Your wallet address to receive payments
port
number
No
Server port (default: 3000)
description
string
No
Human-readable payment description
API Reference
createx402Tool
Creates a payment-protected HTTP endpoint.
Parameters:
fn
Your async function that processes the request
config
Payment configuration object
Returns: A Hono application instance
Example:
x402Middleware
For custom Hono applications, use the middleware directly.
Usage:
Facilitator
Access the underlying facilitator client for advanced use cases.
Generate Payment Requirements:
Verify a Payment:
Settle a Payment:
Payment Flow
How X402 Works
HTTP Headers
X-PAYMENT-REQUIRED
Response
Base64-encoded payment requirements
X-PAYMENT
Request
Signed payment proof
Payment Requirements Format
Note:
maxAmountRequiredis in USDC base units (6 decimals). 10000 = 0.01 USDC.
Client Integration
Making Paid Requests
Using ethers.js
Examples
Image Generation API
Data Lookup Service
Multi-Endpoint API with Middleware
Network
Cronos Testnet
The SDK operates on Cronos Testnet by default.
Chain ID
338
RPC URL
https://evm-t3.cronos.org
Currency
TCRO
Explorer
https://testnet.cronoscan.com
Getting Test Tokens
Get TCRO from the Cronos Testnet Faucet
Acquire test USDC for payment testing
Environment Variables
For client-side payment signing:
TypeScript Support
The SDK is written in TypeScript and exports full type definitions.
Generic type inference works automatically:
Error Handling
Common Errors
402
Payment required
Include valid X-PAYMENT header
402 + error message
Payment invalid
Check payment signature and expiry
500
Server error
Check your function implementation
Handling in Middleware
Last updated