@meterlane/x402 package is Meterlane’s core SDK. It ships two independent exports: a server-side middleware for protecting your API routes with USDC payments, and a MIT-licensed agent client that lets any script or AI agent pay those routes automatically. You can use either export on its own, or both together if you are building end-to-end.
Scaffolding a new project
If you are starting from scratch, the fastest way to get a working Meterlane project is the scaffolding CLI:createCKMiddleware, a funded test wallet script, and environment variable placeholders. If you are adding Meterlane to an existing project, follow the sections below.
Agent client — @meterlane/x402/client
The agent client is a thin wrapper around the standard fetch API. When the server returns HTTP 402, it automatically signs an ERC-3009 USDC authorization and retries the request with payment headers. Your calling code needs no changes beyond swapping fetch for the wrapped version.
The client entry point is intentionally MIT-licensed so you can audit exactly what your agent signs before it spends funds.
Install
createAgentClient(privateKey, network)
createAgentClient accepts a private key and an optional network identifier, and returns a fetch-compatible function. Drop it in wherever you currently call fetch.
Return value: A
fetch-compatible async function. Call it exactly like fetch(url, options).
Network options
Paying the public demo route
The Meterlane public sandbox demo is available athttps://gateway.meterlane.app/gateway/demo/demo. An unpaid curl returns 402; a funded agent receives 200.
Reading settlement metadata
After a successful payment the server attaches settlement information to the response headers. Check for these headers if you need a transaction hash or receipt:Server middleware — @meterlane/x402
createCKMiddleware is a Hono middleware that gates one or more of your API routes behind x402 payments. When a caller sends a request without valid payment headers the middleware returns HTTP 402 with the price and payment requirements. When payment is valid it settles on-chain and passes the request through to your handler.
createCKMiddleware(opts)
Return value: A Hono
MiddlewareHandler.
Example: gating routes in a Hono app
Tracking settlements with onSettled
The onSettled callback fires after every confirmed payment. Use it to record usage, trigger webhooks, or update a database.