Skip to main content
The @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:
This generates a Hono app pre-wired with 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

The public x402.org facilitator does not support Base mainnet. Passing "eip155:8453" without a private mainnet facilitator will produce HTTP 500 errors. Use "eip155:84532" for all development and testing.
Never embed your private key in source code. Read it from an environment variable such as AGENT_PRIVATE_KEY. Use a dedicated hot wallet with a limited USDC balance — do not reuse cold-storage or exchange keys.

Paying the public demo route

The Meterlane public sandbox demo is available at https://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.
The callback receives: