
Charge per request, not per checkout
Payment middleware and framework integrations
Drop-in x402 middleware for Express, Next.js, FastAPI and Spring Boot, gated on verified receipts.
Mount the middleware into your existing app in one call. A protected route answers HTTP 402 with a payment-required header, the caller returns a payment signature, and your handler releases its resource only after a sequencer-signed LayerX receipt verifies against the offer you issued. The middleware records each fulfillment under an idempotency key derived from the caller and the request digest, so a repeat returns the stored result instead of charging twice.
What it does
Four role packages (buyer, seller, merchant and agent) implement x402 version 2 over three HTTP headers, and framework adapters mount them into an existing app in one call. A protected route answers HTTP 402 with a PAYMENT-REQUIRED header, the caller returns a PAYMENT-SIGNATURE header, and the route releases its resource only after a sequencer-signed LayerX receipt verifies against the offer that was issued. Each fulfillment is recorded under an idempotency key derived from the caller principal and the request digest, so a repeated request returns the stored result instead of charging twice. A changed request under an existing key, or a receipt replayed from another request, is refused.
Key capabilities
- Four role packages share one x402 version 2 wire contract: buyer, seller, merchant and agent.
- One-call mounts:
mountLayerXfor Express and Next.js,mount_layerxfor FastAPI, auto-configuration for Spring Boot. - Three offer schemes (
exact,meteredandsubscription), each naming one registered asset id. - The agent package reserves, holds, commits or releases a budget around every spend.
- Signed webhook consumer verifies Ed25519 deliveries, bounds their age and claims each delivery once.
- A Next.js command fails the build when a declared secret reaches a browser bundle.
Who it's for
Developers charging per API call, and developers building agents that pay for calls.
Why it matters
A service can charge for a single request without a checkout page or an invoice: the payment travels in the request headers, and the handler runs only once a verified receipt binds the payer, payee, asset and amount to the offer. Existing Express, Next.js, FastAPI and Spring Boot routes keep their shape, and the middleware supplies the idempotency and replay rules rather than each service writing its own.
Differentiators
Release is gated on a verified receipt rather than an HTTP acknowledgement: the seller recomputes the receipt digest, resolves authority from configured trust, and refuses on any mismatch of payer, asset, recipient or amount. The same three payment signals encode over HTTP, MCP and A2A in the x402 interop adapter, and settlement identity is independent of which transport carried the request.
Outcomes
- Charge for a single API call without a checkout page or invoice.
- Keep your Express, Next.js, FastAPI or Spring Boot routes in their current shape.
- Refuse replayed receipts and repeated charges without writing your own idempotency rules.
Technical notes
- x402 version 2 only. Any other
x402Versionis refused. The adapter pins upstream spec revision7d5363a6d51750dc246041f2b0ed5819dd46a0d7. - Payment headers are standard-base64 JSON bounded at 64 KiB; an offer set holds 1 to 32 entries; amounts are canonical decimal strings up to
u128;assetandpayToare 32-byte hex. - Runtime floors: Node.js 22 or newer for the TypeScript packages, Python 3.11 or newer for FastAPI, Java 21 for the Spring Boot starter.
- The middleware writes no balances. Seller issuance is edge translation, and settlement succeeds only against a gateway-verified canonical LayerX receipt.
- The conformance package is private and non-authority; its vectors are local and do not attest interoperability with third-party implementations.
- The Android and iOS packages carry session, receipt-gate and secret-scan source plus sample apps, not a framework mount; the iOS sample needs XcodeGen.
import express from "express";
import { mountLayerX, SingleProcessWebhookDeliveryStore } from "layerx-express";
const app = express();
mountLayerX(app, {
environment: process.env,
resources: { async release() { return { contentType: "application/json", body }; } },
fulfillments: repository,
deliveries: new SingleProcessWebhookDeliveryStore(),
events: { async handle(event, deliveryId) { await record(deliveryId, event); } },
});
app.listen(8080);More in Developers
Node EVM JSON-RPC namespaces
Standard Ethereum JSON-RPC on chain ID 125
ExploreProgram SDKs and porting kits
Deterministic programs in three languages
ExplorePublic JSON-RPC gateway and OpenRPC schema
One endpoint for chain and agent reads
ExploreRepo build system and developer tooling
One command set builds the whole network
ExploreBuild on Paxeer.
Give your agent a wallet, set its policy, and settle your first call on LayerX.