> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://mpp.dev/api/mcp` to find what you need.

# Server \[Protect endpoints with payment requirements]

Configure server methods once, then create a handler for each route price.

## FastAPI quick start

```python [server.py]
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from mpp import Credential, Receipt
from mpp.methods.tempo import server as tempo
from mpp.server import Mpp, pay

app = FastAPI()
payment = Mpp(
    methods=tempo.charge(
        recipient="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
    ),
)

@app.get("/resource")
@pay(payment.charge(amount="0.50"))
async def get_resource(
    request: Request,
    credential: Credential,
    receipt: Receipt,
):
    return JSONResponse({"data": "paid content", "payer": receipt.payer})
```

`@pay` converts FastAPI, Starlette, Django, and Flask requests into the framework-neutral payment flow. It returns the `402` response or adds the Receipt to a successful response.

## Framework-neutral flow

```python [server.py]
from mpp.server import Request, Response

async def handle(request: Request) -> Response:
    result = await payment.charge(amount="0.50")(request)
    if result.status != 200:
        return result.challenge
    if result.response is not None:
        return result.response
    return result.with_receipt(Response.json({"data": "paid content"}))
```

Return `result.response` for method-managed requests such as session top-ups and closes.

## Configure Tempo

```python [server.py]
from mpp.methods.tempo import PATH_USD
from mpp.methods.tempo import server as tempo

methods = tempo.charge(
    chain_id=42431,
    currencies=[PATH_USD],
    recipient="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
    rpc_url="https://rpc.moderato.tempo.xyz",
)
payment = Mpp(methods=methods)
```

Use a shared persistent replay store across replicas. The default in-memory store is process-local and loses replay state on restart.

## Existing `Authorization` headers

If the route already uses `Authorization`, configure a separate Payment credential header:

```python [server.py]
payment = Mpp(methods=methods, requires_auth=True)
```

The Challenge then advertises `Payment-Authorization`.

## Event handling

```python [server.py]
from mpp.events import PAYMENT_SUCCESS

unsubscribe = payment.on(
    PAYMENT_SUCCESS,
    lambda payload: print(payload["receipt"].reference),
)
```

Server event handlers run inline with verification. Keep them short and retain the unsubscribe function while the handler is active.

## Sessions

Use `tempo.session(...)` with a durable store for metered payment channels, then create route handlers with `payment.session(...)`. Forward method-managed responses so channel open, top-up, and close requests complete correctly.
