x402 Protocol
ShadowFeed implements x402 v2 — the HTTP-native payment protocol for machine-to-machine commerce.What is x402?
x402 is based on the HTTP402 Payment Required status code. It enables:
- No API keys — Payment itself is the authentication
- Pay-per-use — No subscriptions, pay only for what you consume
- Machine-native — Designed for autonomous AI agents
- On-chain — All payments are verifiable blockchain transactions
Protocol Flow
1. Initial Request (No Payment)
2. Server Returns 402
amount: Price in microSTX (5000 = 0.005 STX)payTo: Provider’s Stacks addressnetwork:stacks:1(mainnet) orstacks:2147483648(testnet)
3. Agent Signs and Retries
The SDK:- Parses the
payment-requiredheader (base64 → JSON) - Creates a STX transfer transaction for the exact amount
- Signs it with the agent’s private key
- Encodes the signed TX as base64
- Retries the request:
4. Server Verifies and Settles
The API worker:- Decodes the
payment-signatureheader - Sends to facilitator:
POST /verify(validates amount, recipient, signature) - Sends to facilitator:
POST /settle(broadcasts TX to Stacks via Hiro API) - Waits for TX confirmation
- Returns the data with
payment-responseheader
5. Response with Data
Headers Reference
| Header | Direction | Encoding | Purpose |
|---|---|---|---|
payment-required | Response (402) | Base64 JSON | Payment requirements |
payment-signature | Request | Base64 JSON | Signed payment |
payment-response | Response (200) | Base64 JSON | Settlement confirmation |
x402-stacks SDK
ShadowFeed usesx402-stacks v2 which provides:
paymentMiddleware— Server-side middleware for Express/HonowrapAxiosWithPayment— Client-side axios interceptorprivateKeyToAccount— Wallet derivation from private keySTXtoMicroSTX— Price conversion utility