Documentation
Monetize Your API in Minutes
Production-ready integration guide for xMCPKit and x402 payment protocol
Quick Start
1
Install the x402 SDK
bash
npm install x402-sdk
# or
yarn add x402-sdk2
Add x402 Middleware
typescript
import express from 'express';
import { x402PaymentMiddleware } from 'x402-sdk';
const app = express();
// Add payment middleware
app.use(x402PaymentMiddleware({
walletAddress: '0xYourSOLWallet',
pricePerCall: 0.001,
network: 'solana',
currency: 'SOL'
}));
// Your API endpoints
app.get('/api/data', (req, res) => {
res.json({ message: 'Paid access granted!' });
});
app.listen(3000);3
Configure Your Wallet
Set up a Solana wallet to receive SOL payments:
bash
# Generate a new wallet (or use existing)
solana-keygen new --outfile ~/.config/solana/xmcpkit-wallet.json
# Get your public address
solana address
# Fund it with SOL for transaction fees
solana airdrop 14
Enable MCP Discovery
Add an MCP manifest so AI agents can discover your API:
json
{
"name": "My API",
"version": "1.0.0",
"description": "Sentiment analysis API",
"endpoints": [
{
"path": "/api/sentiment",
"method": "POST",
"price": 0.001,
"currency": "SOL",
"description": "Analyze text sentiment"
}
],
"payment": {
"protocol": "x402",
"wallet": "0x7a3f...9d2c",
"network": "solana"
}
}How x402 Protocol Works
1
Client Makes Request
An AI agent or client sends a request to your API endpoint
POST /api/sentiment2
Server Returns 402
Your API responds with payment requirements
HTTP 402 Payment Required
x402-PayTo: usdc:0x7a3f...9d2c
x402-Amount: 0.001
x402-Network: solana3
Client Pays
The agent automatically sends SOL to your wallet
0.001 SOL → 0x7a3f...9d2c4
Access Granted
Your API verifies payment and returns the response
HTTP 200 OK
{ "sentiment": "positive", "score": 0.95 }Framework Examples
Express.js
typescript
import express from 'express';
import { x402Middleware } from 'x402-sdk';
const app = express();
app.use('/api/*', x402Middleware({
wallet: '0xYourWallet',
price: 0.001
}));
app.post('/api/translate', (req, res) => {
// Your translation logic
res.json({ translated: "Hola" });
});Next.js API Route
typescript
import { NextRequest, NextResponse } from 'next/server';
import { verifyX402Payment } from 'x402-sdk';
export async function POST(req: NextRequest) {
// Check payment
const payment = await verifyX402Payment(req, {
wallet: '0xYourWallet',
amount: 0.001
});
if (!payment.verified) {
return NextResponse.json(
{ error: 'Payment required' },
{
status: 402,
headers: {
'x402-PayTo': 'usdc:0xYourWallet',
'x402-Amount': '0.001'
}
}
);
}
return NextResponse.json({ data: "Success" });
}Testing Your API
Use AgentKit to test your monetized API:
bash
# Install AgentKit CLI
npm install -g agentkit
# Test your API
agentkit call \
--url http://localhost:3000/api/sentiment \
--wallet ~/.config/solana/test-wallet.json \
--data '{"text": "I love this!"}' \
--auto-pay
# Output:
# ✓ Payment sent: 0.001 SOL
# ✓ Response: { "sentiment": "positive", "score": 0.95 }