What is x402?
x402 is a protocol for pay-per-use API requests using cryptocurrency. Instead of subscriptions, you pay for each request with USDC.
Using x402 (Advanced)
For direct wallet-to-wallet payments:
import { ethers } from 'ethers';
// 1. Create payment proof
const signer = new ethers.Wallet(privateKey);
const requestId = `req_${Date.now()}`;
const amount = 1000; // cents
const timestamp = Date.now();
const message = `PromptPort Payment
Request ID: ${requestId}
Amount: ${amount} USDC cents
Timestamp: ${timestamp}`;
const signature = await signer.signMessage(message);
// 2. Send request with payment
const response = await fetch('https://api.chat402.xyz/api/v1/prompt', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-4',
prompt: 'Your question',
payment: {
from: await signer.getAddress(),
amount,
signature,
timestamp,
requestId,
network: 'ethereum'
}
})
});
Most users should use custodial API keys for simplicity. x402 is for advanced use cases.