---
path: /learn/ai-agent-pay-per-api-call/
title: How to let an AI agent pay per API call
description: Let an AI agent pay per API call with the Machine Payments Protocol. Cap each payment before the first paid call, test on Tempo testnet and route paid tools.
image: /images/opengraph/og-learn.png
publishDate: 2026-11-20
keywords:
  - let AI agent pay per API call
  - agent pay per request
  - AI agent API payments
  - mppx client
  - agent spending limit
label: Payments
timeToRead: 7
faq:
  - question: How can an agent pay for an API call?
    answer: The agent calls the API and receives a 402 Payment Required response with the price. An MPP client signs a payment that fits its policy, retries the request and receives the response with a receipt.
  - question: Does the agent need an account or API key with each provider?
    answer: No. With MPP the payment itself authorizes the request, so the agent needs a funded wallet and a client that speaks the protocol. Providers can still require their own keys for other reasons.
  - question: How do I stop an agent from overspending?
    answer: Set limits in the payment client, such as a maximum amount per charge, and limit the key the agent signs with. On Tempo, access keys can carry onchain spending limits and an expiry that the agent cannot change.
  - question: Can I test agent payments without real money?
    answer: Yes. Tempo testnet has a public faucet that funds a new account with test stablecoins in one call, so a script can create, fund and use a wallet without a person approving anything.
  - question: What is Mercator?
    answer: Mercator is a service from Tempo that finds paid tools for an agent and pays for them. The agent connects through MCP after a person approves a wallet and its spending limits once in a browser.
heroTitle: How to let an AI agent pay per API call
seoTitle: How to let an AI agent pay per API call
socialTitle: How to let an AI agent pay per API call
locale: en
canonical: https://tempo.com/learn/ai-agent-pay-per-api-call/
---

# How to let an AI agent pay per API call

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

_An agent pays per API call when it receives a price in an HTTP 402 response, pays it from a wallet and retries the request. This guide shows how to set that up with the [Machine Payments Protocol (MPP)](https://mpp.dev/), cap what the agent can spend and test it on Tempo testnet. For the wider picture, see [agentic payments on Tempo](/solutions/agentic-payments/)._

A coding agent asked to "get the weather for 50 cities from a paid API" usually stops at the sign-up page. It cannot create an account or paste in a card. When the API speaks MPP, the agent pays for each call from its own wallet, within limits you set, and the work continues.

## What does it mean for an agent to pay per API call?

An agent pays per API call when each request carries its own payment, made from a wallet the agent controls, instead of drawing on a prepaid account or monthly invoice. The service states its price in the response to an unpaid request, and the agent decides whether to pay it.

### Pay per call vs. a prepaid API key

A prepaid key needs a person to sign up, add a card and copy the key into the agent's environment. Per-call payment needs only a funded wallet and a client that understands the payment request. The provider gets paid for each response it serves, and you can see each payment as a separate record.

## How a paid API call works with MPP

MPP uses standard HTTP authentication headers, so the exchange is visible in any HTTP log:

1. The agent sends a normal request to a paid endpoint.
2. The server responds with `402 Payment Required` and a `WWW-Authenticate: Payment` header that names the amount, currency, recipient and payment method.
3. The client checks the request against its policy and signs a payment credential.
4. The client retries the request with an `Authorization: Payment` header.
5. The server verifies the payment and returns the response with a `Payment-Receipt` header.

The [HTTP 402 Payment Required](/learn/402-payment-required/) guide covers the status code's history. The [MPP protocol overview](https://mpp.dev/protocol) documents each header.

## Let an agent pay per call in TypeScript

The `mppx` SDK wraps `fetch` so payment happens inside the request. This script creates a throwaway testnet wallet, funds it from the Tempo testnet faucet and pays a demo endpoint on mpp.dev that charges 0.1 test stablecoins. It runs without a browser or a person approving anything, so an agent can run it unattended.

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

```ts
// agent.ts (run with: npx tsx agent.ts)
import { Mppx, tempo } from "mppx/client"
import { createClient, http } from "viem"
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import { tempoModerato } from "viem/chains"
import { Actions } from "viem/tempo"

// 1. A throwaway testnet account for the agent, funded from the public faucet
const account = privateKeyToAccount(generatePrivateKey())
const client = createClient({ chain: tempoModerato, transport: http() })
await Actions.faucet.fundSync(client, { account })

// 2. A payment client that refuses anything outside your policy
const mppx = Mppx.create({
  methods: [
    tempo({
      account,
      allowedChainIds: [tempoModerato.id], // testnet only
      currencies: ["0x20c0000000000000000000000000000000000000"], // pathUSD on Tempo testnet
      decimals: 6,
      maxAmount: "0.25", // refuse any single charge above 0.25 pathUSD
      maxDeposit: "1", // lock at most 1 pathUSD in a session
    }),
  ],
  polyfill: false,
})

// 3. Call a paid API. mppx pays only when the server answers 402.
const res = await mppx.fetch("https://mpp.dev/api/ping/paid")
console.log(res.status, await res.text())
console.log("receipt:", res.headers.get("Payment-Receipt"))
```

A successful run prints `200`, the response body and a base64url receipt that includes the onchain transaction reference. To use a long-lived wallet, load the private key from an environment variable instead of generating one. The [MPP agent quickstart](https://mpp.dev/quickstart/agent) and [client quickstart](https://mpp.dev/quickstart/client) cover other setups, including the `mppx` CLI.

## Set spending limits before the first paid call

A prompt such as "spend at most $5" does not stop an agent from paying more. Put the limit where the agent cannot change it.

- **Per-charge cap in the client.** `maxAmount` makes `mppx` refuse any charge above the cap before it signs anything. In the script above, an endpoint that asks for 0.30 fails with an error and no payment is made.
- **Allowed networks and currencies.** `allowedChainIds` and `currencies` stop the client from paying on a network or in a token you did not expect. A server that asks for a different currency gets no payment.
- **Session deposit cap.** `maxDeposit` limits how much the client locks into a [pay-as-you-go session](https://mpp.dev/guides/usage-based) when a server offers one.
- **Onchain key limits.** On Tempo, the agent can sign with an [access key](/developers/docs/accounts/access-keys/spending-limits) that carries a token allowance and an expiry. Tempo enforces those limits even if the agent's own code is wrong.

Client-side caps protect against a mistaken or hostile price. Onchain key limits protect against a compromised agent. Use both for anything beyond testing. The [agent spending limits guide](/learn/ai-agent-spending-limits/) explains how to size a budget, and [managing agent spend](https://mpp.dev/guides/managing-agent-spend) shows the MPP options in detail.

## Use Mercator when the agent needs tools it doesn't have

Sometimes the agent does not know which API to call. [Mercator](https://mercator.sh/) is a service from Tempo that takes a description of the task, searches a catalog of paid services and pays for the calls it makes. The agent connects through MCP, or an application calls its REST API, so neither needs an account with each downstream provider.

Mercator setup has one step that needs a person. Installing the CLI from the [Mercator docs](https://mercator.sh/docs) and running its setup connects Mercator to the agent client. A person then completes a browser authorization once and chooses the wallet and its spending limits, and paid jobs stay within those limits. Plan for that approval when an agent runs unattended. Direct MPP calls, as in the script above, do not need it. Tempo's guide to [connecting an agent to Mercator](/developers/docs/guide/mercator) walks through the setup.

## Common mistakes when agents pay for APIs

Check a first integration for these mistakes.

- **No cap on the client.** Without `maxAmount`, the client pays whatever price a server asks for within the wallet's balance.
- **Funding a wallet with more than the task needs.** A large balance lets a faulty agent spend more than you intended. Keep the agent's wallet small or limit its key.
- **Testing only the happy path.** Point the client at an endpoint that charges more than the cap and confirm the call fails without a payment.
- **Ignoring receipts.** Store the `Payment-Receipt` header with the task so you can reconcile what the agent bought.

## Where Tempo fits

Tempo is a blockchain built for payments. Agents can pay MPP services on it in stablecoins.

- **Fast settlement.** A payment on Tempo settles onchain in under a second, so a paid call adds little delay to the request.
- **Fees in stablecoins.** Network fees are paid in stablecoins, so the agent's wallet needs no separate gas token.
- **Enforced key limits.** [Access keys](/developers/docs/accounts/access-keys/spending-limits) carry spending limits and expiry at the protocol level.
- **Testnet faucet.** The public faucet funds new accounts in one call, which suits automated tests.

You decide which services the agent may pay and how much. Start with the [agents documentation](/developers/docs/agents) or [talk to the Tempo team](/contact/).

## FAQ

### How can an agent pay for an API call?

The agent calls the API and receives a 402 Payment Required response with the price. An MPP client signs a payment that fits its policy, retries the request and receives the response with a receipt.

### Does the agent need an account or API key with each provider?

No. With MPP the payment itself authorizes the request, so the agent needs a funded wallet and a client that speaks the protocol. Providers can still require their own keys for other reasons.

### How do I stop an agent from overspending?

Set limits in the payment client, such as a maximum amount per charge, and limit the key the agent signs with. On Tempo, access keys can carry onchain spending limits and an expiry that the agent cannot change.

### Can I test agent payments without real money?

Yes. Tempo testnet has a public faucet that funds a new account with test stablecoins in one call, so a script can create, fund and use a wallet without a person approving anything.

### What is Mercator?

Mercator is a service from Tempo that finds paid tools for an agent and pays for them. The agent connects through MCP after a person approves a wallet and its spending limits once in a browser.

---

## 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/)
