TypeScript SDK quickstart
This guide takes you through charging for a single Express route. By the end, a request without payment will receive a 402 Payment Required response, and a paid request will reach your handler along with the details of the payment. If you would like to understand the payment flow before you start, read the SDK overview.
You will need a Mitu merchant account and a secret key. Use a test key while you build. It connects to the sandbox, so you can test the whole flow without moving real money. You can create keys in the dashboard under Keys and secrets.
Install the package
Add the SDK to your project with npm.
The SDK runs on Node.js 22.12 or later. It also runs on Fetch API runtimes such as Cloudflare Workers, Deno and Bun.
Keep your secret key out of your code by storing it in an environment variable.
Charge for a route
The example below creates a Mitu client and puts one route behind a payment of 0.05 USD per request.
The createMitu function sets up a client from your key. You create it once when your server starts and share it across every route. The charge middleware takes that client and the route’s price, and from then on it guards the route.
When a request reaches /v1/search, the following happens.
- The first request has no payment.
The SDK asks Mitu to create a payment for this route and price, then replies with 402 Payment Required. The response explains how much to pay and lists the MPP and x402 options the caller can use. - The caller pays.
The caller completes the payment and sends the same request again, this time with a payment credential attached. - The payment is checked and settled.
The SDK confirms that the credential belongs to a payment made for this route at this price, and Mitu verifies and settles it. - Your handler runs.
The request reaches your handler with the settled payment available onreq.mitu.payment, and the response includes the payment receipt in its headers.
Your handler never runs for a request that has not been paid, so you do not need to write any payment checks of your own.
Always work out prices from information on your own server, such as the user’s plan or a stored order total. Never read a price from the request body. The caller controls the body, so a price taken from it would let the caller decide how much to pay.
Try the route
Start your server and call the route without paying.
You should see a 402 Payment Required response with the payment options in its headers. To complete the flow, pay with any MPP or x402 client, such as the mppx command line tool or an Agent Wallet. The paid request returns 200 OK along with your handler’s response.
Use another framework
Express is one of several frameworks the SDK supports. There are ready-made adapters for Hono, the Next.js App Router, Elysia and Fetch API runtimes such as Cloudflare Workers. If your framework is not on that list, you can call mitu.admit() directly with any standard Fetch API Request. The API reference shows how to use each adapter.