> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.mitu.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.mitu.ai/_mcp/server.

# 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](/sdks/overview).

> **Note**
>
> 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](/platform/security/keys-and-secrets).

## Install the package

Add the SDK to your project with npm.

```bash
npm install @mitulabs/payments
```

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.

```bash
export MITU_API_KEY=your_test_key
```

## Charge for a route

The example below creates a Mitu client and puts one route behind a payment of 0.05 USD per request.

**`server.ts`**

```ts title="server.ts"
import express from 'express'
import { createMitu } from '@mitulabs/payments'
import { charge } from '@mitulabs/payments/express'

const mitu = createMitu({ apiKey: process.env.MITU_API_KEY! })

const app = express()

app.get('/v1/search', charge(mitu, { amount: '0.05' }), (req, res) => {
  res.json({ results: search(req.query), payment: req.mitu?.payment })
})

app.listen(3000)
```

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.

1. **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.
2. **The caller pays.**\
   The caller completes the payment and sends the same request again, this time with a payment credential attached.
3. **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.
4. **Your handler runs.**\
   The request reaches your handler with the settled payment available on `req.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.

> **Warning**
>
> 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.

```bash
curl -i http://localhost:3000/v1/search
```

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](/guides/agent-wallets). 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](/sdks/typescript/api-reference#adapters) shows how to use each adapter.

## Next steps

#### [Configuration](/sdks/typescript/configuration)

Learn about every option, how environments work, and how to add logging.

#### [API reference](/sdks/typescript/api-reference)

See every adapter, how prices are set, and what your handler receives.

#### [Errors](/sdks/typescript/errors)

Understand which responses the SDK sends for you and which errors reach your code.

#### [Pricing](/platform/your-agent-service/tools/pricing)

Read how Mitu prices and settles agent payments.