Quick Start
Get running with Corex402 in under 5 minutes
1
Install the Package
Choose based on your use case:
For AI Agents (most common)
npm install x402pp-client @solana/web3.jsFor API Providers
npm install x402pp-server express @solana/web3.js2
Write Your First Agent
agent.ts
import { Connection, Keypair } from '@solana/web3.js';
import { X402Client } from 'x402pp-client';
// 1. Setup
const connection = new Connection('https://api.devnet.solana.com');
const wallet = Keypair.generate();
const client = new X402Client({ connection, wallet });
// 2. Create Intent
const intent = client.createIntent({
capability: 'weather-api',
maxPricePerRequest: 0.01,
token: 'USDC',
sla: { maxLatencyMs: 2000 }
});
// 3. Negotiate with provider
const offer = await client.negotiate(
'https://api.provider.com',
intent
);
console.log('Got offer:', offer.pricePerRequest, offer.token);
// 4. Create prepaid session
const session = await client.createSession(offer, {
depositAmount: 0.1 // 0.1 USDC
});
console.log('Session created:', session.sessionId);
console.log('Can make', Math.floor(0.1 / offer.pricePerRequest), 'requests');
// 5. Execute requests
const result = await client.executeRequest(
session.sessionId,
{ city: 'New York' }
);
console.log('Response:', result.data);
console.log('Receipt:', result.receipt);
console.log('SLA met:', result.receipt.slaVerification.met);
// 6. Close and get refund
await client.closeSession(session.sessionId);3
Run It
# Get devnet SOL (free)
solana airdrop 1 YOUR_WALLET --url devnet
# Run your agent
ts-node agent.tsWhat Happens Behind the Scenes
Agent Provider Solana
│ │ │
│ createIntent() │ │
├─ Intent { │ │
│ capability: "weather-api" │ │
│ maxPrice: 0.01 │ │
│ } │ │
│ │ │
│ negotiate() ───────────────> │ │
│ X-402-Intent header │ │
│ │ createOffer() │
│ ├─ Offer { │
│ │ price: 0.008 │
│ │ signature: "..." │
│ │ } │
│ <──────────────────────────── │ │
│ 402 + X-402-Offer │ │
│ │ │
│ createSession() │ │
├─ Transfer 0.1 USDC ──────────────────────────────────────>│
│ │ verifyPayment() │
│ │<──────────────────────────┤
│ │ createSession() │
│ <──────────────────────────── │ │
│ Session {id, balance} │ │
│ │ │
│ executeRequest() ──────────> │ │
│ + Session-Id header │ │
│ │ processRequest() │
│ │ deductBalance() │
│ │ createReceipt() │
│ <──────────────────────────── │ │
│ Data + Receipt │ │
│ │ │
│ ... 99 more requests ... │ │
│ (NO blockchain overhead!) │ │
│ │ │
│ closeSession() ────────────> │ │
│ │ calculateRefund() │
│ <─────────────────────────────────────────────────────────┤
│ Refund remaining balance │ │