---
path: /learn/charge-agents-per-request/
title: How to charge AI agents per request in an Express API
description: Charge AI agents per request in an Express API with MPP. Add a paid route with mppx, test it on Tempo testnet and set up replay protection for production.
image: /images/opengraph/og-learn.png
publishDate: 2026-11-20
keywords:
  - charge AI agents per request
  - Express API payments
  - Node.js pay per request
  - mppx Express middleware
  - MPP server
label: Payments
timeToRead: 7
faq:
  - question: How do I charge agents per request in Express?
    answer: Install mppx, create an Mppx instance with a payment method and add mppx.charge({ amount }) as middleware on the route. Unpaid requests get a 402 with the price, and paid requests reach your handler.
  - question: Do callers need an account with my API?
    answer: No. The payment authorizes the request, so any client that speaks MPP can pay and get a response. You can still add your own keys or rate limits.
  - question: Why does my MPP server fail to start in production?
    answer: mppx refuses to start a Tempo charge without a replay-protection store when the runtime looks like production. Pass a shared store such as Store.redis() or Store.postgres(), or Store.memory() for a single instance.
  - question: Which stablecoins can my API accept?
    answer: By default the Tempo method in mppx offers the SDK's default stablecoins for the chosen network. You can pass a currencies list to accept specific TIP-20 tokens instead.
  - question: Can I charge less than one cent per request?
    answer: Yes. Amounts are decimal strings, so a route can charge 0.001. For many small requests, an MPP session lets payments accumulate and settle later instead of once per request.
heroTitle: How to charge AI agents per request in an Express API
seoTitle: Charge AI agents per request in Express with MPP
socialTitle: How to charge AI agents per request in an Express API
locale: en
canonical: https://tempo.com/learn/charge-agents-per-request/
---

# How to charge AI agents per request in an Express API

> **Docs:** [Documentation index](https://tempo.com/developers/llms.txt)
>
> **Site:** [All tempo.com pages](https://tempo.com/llms.txt)

_Charging agents per request means your API returns a price in an HTTP 402 response and serves the request once it is paid, with no sign-up. This guide adds a paid route to an Express API with the [Machine Payments Protocol (MPP)](https://mpp.dev/) and the `mppx` SDK, tests it on Tempo testnet and covers what changes in production. See [API monetization](/learn/api-monetization/) for pricing models._

An agent often finds and calls an API within a single task. It rarely stops to create an account, so an API that requires one loses that request. An MPP route lets any client with a funded wallet pay for a single request and get the response.

## What is per-request billing for agents?

Per-request billing for agents is a pricing model in which each API call is paid at the time it is made, by the client that makes it. The server publishes the price in its response to an unpaid request and returns the result once a valid payment arrives.

### Per-request billing vs. metered invoicing

Metered invoicing counts usage against an account and bills it later, so it needs an account with a payment method on file. Per-request billing collects payment before the response, so there is nothing to invoice and no balance to chase.

## How per-request payments work with MPP

The `mppx` middleware handles the payment exchange before your route handler runs:

1. A request without payment reaches the paid route.
2. The middleware returns `402 Payment Required` with a `WWW-Authenticate: Payment` header naming the amount, currency and recipient.
3. The client pays and retries with an `Authorization: Payment` header.
4. The middleware verifies the payment, runs your handler and adds a `Payment-Receipt` header to the response.

On Tempo, the client's payment is a stablecoin transfer to your recipient address. The guide to [letting an agent pay per API call](/learn/ai-agent-pay-per-api-call/) covers the client side.

## Add a paid route to an Express API

You need Node.js, Express 5 and an address to receive payments. Install the SDK with its peer dependencies:

```bash
npm install mppx express viem
npm install -D tsx
```

Then add `mppx.charge` to any route you want to charge for:

```ts
// server.ts (run with: npx tsx server.ts)
import express from "express"
import { Store } from "mppx"
import { Mppx, tempo } from "mppx/express"

const app = express()

const mppx = Mppx.create({
  methods: [
    tempo.charge({
      recipient: process.env.RECIPIENT_ADDRESS as `0x${string}`, // the address that gets paid
      store: Store.memory(), // replay protection (use a shared store in production)
      testnet: true, // remove for mainnet
    }),
  ],
  secretKey: process.env.MPP_SECRET_KEY!, // openssl rand -hex 32
})

// Free route
app.get("/health", (_req, res) => {
  res.json({ ok: true })
})

// Paid route: 0.01 per request
app.get("/api/forecast", mppx.charge({ amount: "0.01" }), (_req, res) => {
  res.json({ city: "Paris", tempC: 18 })
})

app.listen(3000, () => console.log("Listening on http://localhost:3000"))
```

Start it with your address and a random secret:

```bash
export RECIPIENT_ADDRESS=0xYourAddress
export MPP_SECRET_KEY=$(openssl rand -hex 32)
npx tsx server.ts
```

`secretKey` signs the payment challenges your server issues so it can verify them without keeping state. Every instance of the server must use the same value. Keep it server-side and never log it. Without `currencies`, the Tempo method offers the SDK's default stablecoins for the selected network. The [MPP server quickstart](https://mpp.dev/quickstart/server) shows the same setup for Hono, Next.js and other frameworks.

## Test the paid route

An unpaid request returns the price:

```bash
curl -i http://localhost:3000/api/forecast
```

The response starts like this:

```text
HTTP/1.1 402 Payment Required
WWW-Authenticate: Payment id="...", realm="localhost", method="tempo", intent="charge", request="..."
```

To make a paid request, run the client script from the [agent pay-per-call guide](/learn/ai-agent-pay-per-api-call/) with its URL changed to `http://localhost:3000/api/forecast`. It funds a testnet wallet from the faucet, pays 0.01 and prints `200`, the JSON body and the receipt.

## What changes in production

The same code runs in production with a few changes.

- **Use a shared replay-protection store.** A store records which payments the server has accepted, so one payment can't be used twice. When `mppx` detects a production runtime and no store is set, it throws at startup and names the supported options: `Store.redis()`, `Store.upstash()`, `Store.postgres()`, `Store.sql()`, `Store.d1()`, `Store.durableObject()` or `Store.cloudflare()`. `Store.memory()` is safe only when a single instance serves all traffic. See the [Store reference](https://mpp.dev/sdk/typescript/core/Store.postgres).
- **Switch to mainnet.** Remove `testnet: true` and point `recipient` at an address you control on Tempo mainnet.
- **Keep receipts.** Log the `Payment-Receipt` value with each request so finance can match payments to usage. See [payment reconciliation](/learn/payment-reconciliation/).
- **Keep your other controls.** Payment does not replace rate limits, input validation or monitoring.

For endpoints called many times a second, a [pay-as-you-go session](https://mpp.dev/guides/usage-based) lets the client deposit once and send signed vouchers per request, which the server settles later in one transaction. The [MPP security guide](https://mpp.dev/advanced/security) lists further checks before launch.

## Where Tempo fits

Tempo is a blockchain built for payments, and its MPP method is in production.

- **Settlement in under a second.** Payments on Tempo settle onchain in under a second, which keeps the wait for a paid response short.
- **Sub-cent fees.** Network fees are paid in stablecoins and cost less than a cent, so a route can charge a cent or less per request.
- **Sessions for high volume.** [Pay-as-you-go sessions](/developers/docs/guide/machine-payments/pay-as-you-go) settle many small payments together.
- **Agent tooling.** The [agents documentation](/developers/docs/agents) covers wallets, limits and testnet setup.

You set the prices and decide which routes are paid. [Talk to the Tempo team](/contact/) about your API.

## FAQ

### How do I charge agents per request in Express?

Install mppx, create an Mppx instance with a payment method and add `mppx.charge({ amount })` as middleware on the route. Unpaid requests get a 402 with the price, and paid requests reach your handler.

### Do callers need an account with my API?

No. The payment authorizes the request, so any client that speaks MPP can pay and get a response. You can still add your own keys or rate limits.

### Why does my MPP server fail to start in production?

mppx refuses to start a Tempo charge without a replay-protection store when the runtime looks like production. Pass a shared store such as Store.redis() or Store.postgres(), or Store.memory() for a single instance.

### Which stablecoins can my API accept?

By default the Tempo method in mppx offers the SDK's default stablecoins for the chosen network. You can pass a currencies list to accept specific TIP-20 tokens instead.

### Can I charge less than one cent per request?

Yes. Amounts are decimal strings, so a route can charge 0.001. For many small requests, an MPP session lets payments accumulate and settle later instead of once per request.

---

## Site links

- [Tempo home](/)
- [Privacy policy](/privacy-policy/)
- [LinkedIn](https://www.linkedin.com/company/tempo)
- [X](https://x.com/tempo)
- [YouTube](https://www.youtube.com/@tempo)

### Products

- [Earn](/earn/)
- [Routes](/routes/)
- [Zones](/zones/)

### Solutions

- [Agentic payments](/solutions/agentic-payments/)
- [Credit](/solutions/credit/)
- [Cross-border payments](/solutions/cross-border-payments/)
- [Institutional settlement](/solutions/institutional-settlement/)
- [Stablecoin accounts](/solutions/stablecoin-accounts/)
- [Tokenized deposits](/solutions/tokenized-deposits/)

### Developers

- [Documentation](/developers)
- [Tempo Explorer](https://explore.tempo.xyz/)
- [Tempo API](/developers/docs/api)
- [Performance](/developers/docs/protocol/blockspace/overview)
- [Status](https://status.tempo.xyz/)
- [GitHub](https://github.com/tempoxyz)

### Resources

- [Blog](/blog/)
- [Customers](/customer-stories/)
- [Learn](/learn/)
- [OUSD](/ousd/)
- [Partners](/partners/)
- [Reports](/reports/)
- [Stablecoin Advisory](/stablecoin-advisory/)

### Company

- [About](/about/)
- [Careers](/careers/)
- [Brand kit](/brand/)
- [FAQ](/faq/)
