LayerHopDocsLaunch app (opens in a new tab)

x402 pay-per-request

Charge USDC per API request with the open x402 protocol (HTTP 402). LayerHop issues the payment requirements, verifies and settles each payment, and pays you out through a non-custodial fee-splitting deposit address.

x402 is an open protocol for paying per HTTP request: your server answers 402 Payment Required with payment requirements, the client (an app or an AI agent) signs a USDC authorization and sends the request again with it in a header, and you serve the response once the payment is verified. LayerHop is an x402 facilitator: it gives you the requirements, verifies payments and settles them on chain, so you don't run any blockchain code.

Rolling out. Requirements and verify work with any API key; settle is being enabled gradually (GET /v1/x402/supported shows settle: true once it is on). Until then POST /v1/x402/settle answers FORBIDDEN.

How it works#

  1. Your server asks LayerHop for the requirements of a resource: the price you want to net per request (USDC base units) and your payout address.
  2. A request without payment gets 402 with those requirements. The client signs an EIP-3009 transferWithAuthorization for the amount and sends it in X-PAYMENT (x402 v1) or PAYMENT-SIGNATURE (v2).
  3. Your server calls verify; LayerHop checks the signature, amount, recipient, validity window, nonce, the payer's balance, and simulates the transfer.
  4. You serve the response, then call settle; LayerHop submits the authorization on chain (it pays the gas) and you add the result to the response in X-PAYMENT-RESPONSE / PAYMENT-RESPONSE. You also get an x402.payment.settled webhook.

Networks: Base (default), OP Mainnet, Arbitrum and Polygon, in native USDC. Both x402 v1 (network names such as base) and v2 (CAIP-2 ids such as eip155:8453) are accepted.

Where the money goes#

The requirements' payTo is not your address: it is your own deposit address for that resource, created by the same non-custodial fee-splitting contract as checkout deposit addresses (CheckoutSplitterFactory, 0xbC8CF31dd2E2f501587df0cfbA703E7298DB100e). The contract has no owner and can only ever pay your payout address and LayerHop's fee. The payer pays your price plus LayerHop's 0.25% fee on top, so you net your price. LayerHop sweeps each deposit address to your payout address once it holds at least $1 (payee.lastSweepTx, and sweepTx on each payment); anyone, you included, can trigger a sweep.

With the SDK#

@layerhop/sdk has a middleware for fetch handlers (Cloudflare Workers, Bun, Deno, Next.js route handlers), Hono and Express:

TypeScript
import { x402 } from "@layerhop/sdk";

const pay = x402({
  apiKey: process.env.LAYERHOP_API_KEY!, // lh_live_… to settle real payments
  price: "10000",                         // you net 0.01 USDC per request
  payTo: "0xYourPayoutAddress",
  network: "base",
  description: "Current weather",
});

// Hono
app.get("/weather", pay.hono(), (c) => c.json({ temp: 21 }));

// Express (settles before your handler runs)
app.get("/weather", pay.express(), (req, res) => res.json({ temp: 21 }));

// fetch handler
export default { fetch: pay.fetch(async () => Response.json({ temp: 21 })) };

A response with status 400 or above is not charged (fetch and Hono). The requirements are cached per resource for 10 minutes; the resource key is the request URL without its query string, or the resource option.

With the API#

Terminal
curl -X POST https://api.layerhop.com/v1/x402/requirements \
  -H "x-api-key: $LAYERHOP_API_KEY" -H "content-type: application/json" \
  -d '{ "network": "base", "amount": "10000", "payTo": "0xYourPayoutAddress", "resource": "https://api.example.com/v1/weather" }'

The answer has accepts (x402 v2 requirements), acceptsV1 (the v1 form) and paymentRequired (the whole v2 object, base64 it into the PAYMENT-REQUIRED header). The price must be at least 0.01 USDC (10000).

Then, for a request with a payment header, decode the header (base64 JSON) and send it with the matching requirements:

Terminal
curl -X POST https://api.layerhop.com/v1/x402/verify \
  -H "x-api-key: $LAYERHOP_API_KEY" -H "content-type: application/json" \
  -d '{ "x402Version": 2, "paymentPayload": { … }, "paymentRequirements": { … } }'
# → { "isValid": true, "payer": "0x…" }

curl -X POST https://api.layerhop.com/v1/x402/settle … (same body)
# → { "success": true, "transaction": "0x…", "network": "eip155:8453", "payer": "0x…" }

verify always answers HTTP 200; isValid: false carries the x402 invalidReason (insufficient_funds, invalid_exact_evm_payload_signature, invalid_exact_evm_payload_authorization_value_mismatch, invalid_exact_evm_payload_authorization_nonce_used, …). settle is idempotent per authorization. errorReason: settlement_pending means the transaction was sent but its receipt wasn't in yet; LayerHop finishes it and the webhook fires then.

List your payments with GET /v1/x402/payments (filter by status, network, from and to). See the API reference for every field.

In the dashboard#

The x402 page of the dashboard shows settled payments and USDC volume over the last 24 hours and 7 days, every payment (resource, network, payer, amount, LayerHop's fee, status, settle transaction and payout sweep) with filters by status, network and date, the details of one payment, and your deposit addresses (payTo) per network with copy buttons. While settlement is not enabled yet it shows an "x402 is in preview" banner. x402 payments are always live, so test mode has nothing to show.

Limits#

  • Payers must sign with a regular wallet (EOA); smart-contract wallet signatures (ERC-1271) are not supported yet.
  • Only scheme exact with EIP-3009 (USDC). BNB Chain's USDC has no EIP-3009, so it isn't offered.
  • Test keys (lh_test_) can get requirements and verify, but can't settle.

Last updated