🚀Quickstart
Get Started in 5 Minutes
Set up x402 payments in your AI agent or API in just a few steps.
Prerequisites
- Node.js 18+ installed
- A package manager (npm, pnpm, or yarn)
- An x402 Gateway account — sign up free
Choose Your Path
x402 has two sides. Choose based on what you're building:
🤖
Agent Setup
Step 1: Install the SDK
bash
npm install @x402/agent
# or
pnpm add @x402/agentStep 2: Create an API Key
Log into the x402 Gateway dashboard and navigate to Agents → Create API Key. Copy the key — you'll only see it once.
Step 3: Initialize the Client
typescript
import { createAgentClient } from '@x402/agent';
const client = createAgentClient({
gatewayBaseUrl: 'https://xfour.xyz/api/gateway',
apiKey: process.env.X402_AGENT_KEY!,
});Step 4: Make Paid Requests
typescript
// Use fetchWithX402 just like regular fetch
// x402 payments are handled automatically!
const response = await client.fetchWithX402(
'https://api.example.com/ai/complete',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt: 'Hello world' }),
}
);
const result = await response.json();
console.log(result);That's it! When the API returns 402, the SDK automatically handles the payment flow and retries with proof.
🔌
Server Setup
Step 1: Install the SDK
bash
npm install @x402/server
# or
pnpm add @x402/serverStep 2: Register as a Provider
Log into the x402 Gateway dashboard and navigate to Providers → Register. You'll receive your provider API key and configure your Ethereum wallet address.
Step 3: Add Middleware (Express)
typescript
import express from 'express';
import { createX402Middleware } from '@x402/server';
const app = express();
app.use(express.json());
const x402 = createX402Middleware({
gatewayUrl: 'https://xfour.xyz/api/gateway',
apiKey: process.env.X402_SERVER_KEY!,
payToAddress: process.env.ETHEREUM_ADDRESS!, // Your 0x address
network: 'mainnet', // or 'sepolia' for testnet
});
// Protect your paid endpoints
app.post('/api/ai/complete',
x402.requirePayment(0.05), // 0.05 MNEE per request
async (req, res) => {
// Only executes if payment verified!
const result = await processAI(req.body);
res.json(result);
}
);
app.listen(3000);Done! Your endpoint now returns 402 for unpaid requests and verifies payment proofs automatically.