---
path: /learn/mpp-python/
title: How to implement MPP in Python with pympp
description: Implement the Machine Payments Protocol in Python. Add a paid FastAPI route with pympp, pay it from a Python client on Tempo testnet and accept x402 clients.
image: /images/opengraph/og-learn.png
publishDate: 2026-12-03
keywords:
  - MPP Python
  - pympp
  - Machine Payments Protocol Python
  - x402 Python
  - FastAPI pay per request
  - HTTP 402 Python
label: Payments
timeToRead: 8
faq:
  - question: Is there a Python SDK for MPP?
    answer: Yes. pympp is the Python SDK for the Machine Payments Protocol. It includes a server that issues 402 challenges and verifies payments, and a client that pays them automatically.
  - question: Which pip extra do I need for Tempo payments?
    answer: Install pympp with the tempo extra, as pip install "pympp[tempo]". The base package does not include the dependencies the Tempo payment method needs.
  - question: Why does my pympp server fail with a missing secret key?
    answer: The server signs its payment challenges with a secret key. Set MPP_SECRET_KEY to a random value, such as the output of openssl rand -hex 32, or pass secret_key to Mpp. Every instance of the server must use the same value.
  - question: Can a Python MPP server accept x402 clients?
    answer: Yes. The evm method in pympp serves MPP clients and x402 v2 exact clients from the same route. An x402 facilitator settles those payments.
  - question: Can a Python client pay x402 servers?
    answer: Yes. The pympp client pays x402 v2 exact servers through its evm method. When a server offers both, the client pays with MPP.
  - question: How do I run a pympp server on more than one instance?
    answer: Pass a shared replay-protection store, such as RedisStore from mpp.stores, to tempo.charge. The default in-memory store only protects a single process.
heroTitle: How to implement MPP in Python with pympp
seoTitle: "MPP in Python: charge and pay per request with pympp"
socialTitle: How to implement MPP in Python with pympp
locale: en
canonical: https://tempo.com/learn/mpp-python/
---

# How to implement MPP in Python with pympp

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

_MPP in Python means using pympp, the Python SDK for the [Machine Payments Protocol](https://mpp.dev/), to charge for an API route or to pay for one over HTTP. This guide adds a paid FastAPI route, pays it from a Python client on Tempo testnet and shows how the same server can accept x402 clients. For the protocol comparison, see [MPP vs x402](/learn/mpp-vs-x402/)._

Most agent tooling is written in Python, so a Python team that wants to charge agents per request needs a server and a client in the same language. pympp provides both, and the code below was run end to end against Tempo's Moderato testnet.

## What is pympp?

pympp is the Python SDK for the Machine Payments Protocol (MPP), an open protocol co-authored by Tempo and Stripe for charging machines over HTTP. Its server returns `402 Payment Required` with a price, and its client pays that price and retries the request.

### pympp vs. pytempo

pympp handles the payment exchange between a client and a server. [pytempo](/developers/docs/sdk/python) is the Python SDK for sending Tempo transactions directly, such as stablecoin transfers or batched calls. pympp uses pytempo for its Tempo payment method, so you only install pympp to build a paid API.

## How an MPP payment works in Python

Each paid request follows four steps:

1. The client calls a paid route without payment.
2. The server returns `402 Payment Required` with a `WWW-Authenticate: Payment` header that names the amount, currency and recipient.
3. The client pays and retries with an `Authorization: Payment` credential.
4. The server verifies the payment, runs your handler and returns the response with a `Payment-Receipt` header.

On Tempo, the payment is a stablecoin transfer to your recipient address. The [agent pay-per-call guide](/learn/ai-agent-pay-per-api-call/) explains the client side in more detail.

## Add a paid route to a FastAPI app

You need Python 3.11 or later and an address to receive payments. Install pympp with its Tempo extra and a web server:

```bash
pip install "pympp[tempo]" fastapi uvicorn
```

The `tempo` extra is required. The base `pympp` package does not include the dependencies the Tempo payment method imports.

Add the `pay` decorator to any route you want to charge for. Save this as `server.py` and run it with `uvicorn server:app --port 8000`:

```python
import os

from fastapi import FastAPI, Request
from mpp import Credential, Receipt
from mpp.methods.tempo import server as tempo
from mpp.server import Mpp, pay

PATH_USD = "0x20c0000000000000000000000000000000000000"  # pathUSD, a USD stablecoin on Tempo

app = FastAPI()
mpp = Mpp(
    methods=tempo.charge(
        recipient=os.environ["RECIPIENT"],  # the address that gets paid
        currency=PATH_USD,
        chain_id=42431,  # Moderato testnet, mainnet is 4217
    ),
)


@app.get("/forecast")
@pay(mpp.charge(amount="0.01", description="One weather forecast"))
async def forecast(request: Request, credential: Credential, receipt: Receipt):
    return {"forecast": "sunny", "payer": credential.source, "tx": receipt.reference}
```

Start it with your address and a random secret:

```bash
export RECIPIENT=0xYourAddress
export MPP_SECRET_KEY=$(openssl rand -hex 32)
uvicorn server:app --port 8000
```

`MPP_SECRET_KEY` signs the challenges your server issues, so it can verify them later without keeping state. The server refuses to start without it. Every instance must use the same value, and it should never appear in logs. For frameworks other than FastAPI, call the route handler with pympp's framework-neutral `Request`, as the [pympp README](https://github.com/tempoxyz/pympp) shows.

## Test the route with curl

An unpaid request returns the price:

```bash
curl -i http://localhost:8000/forecast
```

The response starts like this:

```text
HTTP/1.1 402 Payment Required
www-authenticate: Payment id="...", realm="localhost", method="tempo", intent="charge", request="...", description="One weather forecast"
```

## Pay the route from a Python client

The pympp client pays the challenge and retries the request for you. Create a test key and fund it with testnet stablecoins from the [faucet](/developers/docs/quickstart/faucet):

```bash
export PRIVATE_KEY=$(python -c "from eth_account import Account; print(Account.create().key.to_0x_hex())")
```

Then save this as `client.py` and run it while the server is up:

```python
import asyncio
import os

from mpp.client import Mpp
from mpp.methods.tempo import TempoAccount
from mpp.methods.tempo import client as tempo

PATH_USD = "0x20c0000000000000000000000000000000000000"

account = TempoAccount.from_key(os.environ["PRIVATE_KEY"])
method = tempo.charge(
    account=account,
    chain_id=42431,  # Moderato testnet
    currency=PATH_USD,
    max_amount="0.05",  # never pay more than 0.05 pathUSD for one request
    max_amount_decimals=6,
)


async def main():
    async with Mpp(methods=[method]) as mpp:
        response = await mpp.fetch("http://localhost:8000/forecast")
        print(response.status_code, response.json())


asyncio.run(main())
```

Running `python client.py` prints `200` with the forecast, the payer and the transaction hash of the payment. `max_amount` caps what the client pays for one request, and it needs a pinned `currency` and `max_amount_decimals` to compare amounts. Set a cap in any client an agent controls.

## Accept x402 clients on the same server

Some agents only speak x402. pympp's `evm` method serves native MPP clients and x402 v2 `exact` clients from one endpoint. Unpaid responses carry a `PAYMENT-REQUIRED` header next to `WWW-Authenticate`, and an x402 facilitator settles the payments.

```bash
pip install "pympp[evm]"
```

```python
from mpp.methods.evm import BASE_USDC
from mpp.methods.evm import server as evm
from mpp.server import Mpp

mpp = Mpp(
    methods=evm.charge(
        currency=BASE_USDC,
        recipient="0xYourAddress",
        facilitator="https://facilitator.example.com",
        route_binding="resource",  # accept standard x402 clients
    ),
)
```

Pass `methods=[tempo.charge(...), evm.charge(...)]` to offer both on one route. On the client side, the `evm` client method pays x402 servers, and it pays with MPP when a server offers both. Tempo's [x402 facilitator guide](/developers/docs/guide/machine-payments/x402-facilitator) covers settlement on Tempo.

## What changes in production

The same code runs in production with a few changes.

- **Use a shared replay-protection store.** The store records which payments the server has accepted, so one payment can't be used twice. Without one, pympp falls back to an in-memory store and logs a warning. Pass `RedisStore` or `SQLStore` from `mpp.stores` to `tempo.charge(store=...)` when more than one process serves the route.
- **Switch to mainnet.** Set `chain_id=4217` 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 or input validation.

For endpoints called many times a second, a [pay-as-you-go session](/developers/docs/guide/machine-payments/pay-as-you-go) lets the client deposit once and send signed vouchers per request. The server settles them later in one transaction.

## Where Tempo fits

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

- **Settlement in under a second.** Payments settle onchain in under a second, so a paid response arrives with little added wait.
- **Fees in stablecoins.** Network fees can be paid in any stablecoin, so neither side holds a separate gas token.
- **Sessions for high volume.** [Pay-as-you-go sessions](/developers/docs/guide/machine-payments/pay-as-you-go) settle many small payments together.
- **Python tooling.** The [Python SDK docs](/developers/docs/sdk/python) cover direct Tempo transactions beyond MPP.

You set the prices and choose which routes are paid. For the Node.js version of this guide, see [charging agents per request in Express](/learn/charge-agents-per-request/). [Talk to the Tempo team](/contact/) about your API.

## FAQ

### Is there a Python SDK for MPP?

Yes. pympp is the Python SDK for the Machine Payments Protocol. It includes a server that issues 402 challenges and verifies payments, and a client that pays them automatically.

### Which pip extra do I need for Tempo payments?

Install pympp with the tempo extra, as pip install "pympp[tempo]". The base package does not include the dependencies the Tempo payment method needs.

### Why does my pympp server fail with a missing secret key?

The server signs its payment challenges with a secret key. Set MPP_SECRET_KEY to a random value, such as the output of openssl rand -hex 32, or pass secret_key to Mpp. Every instance of the server must use the same value.

### Can a Python MPP server accept x402 clients?

Yes. The evm method in pympp serves MPP clients and x402 v2 exact clients from the same route. An x402 facilitator settles those payments.

### Can a Python client pay x402 servers?

Yes. The pympp client pays x402 v2 exact servers through its evm method. When a server offers both, the client pays with MPP.

### How do I run a pympp server on more than one instance?

Pass a shared replay-protection store, such as RedisStore from mpp.stores, to tempo.charge. The default in-memory store only protects a single process.

---

## 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 Platform](/platform/)
- [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/)
