Skip to navigation

Python SDK quickstart

Charge for your first FastAPI, Flask or Django route and follow it to a settled payment.
View as Markdown

This guide takes you through charging for a single route. By the end, a request without payment will receive a 402 Payment Required response, and a paid request will reach your view along with the details of the payment. The main example uses FastAPI, and the same approach works in Flask and Django. If you would like to understand the payment flow before you start, read the SDK overview.

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.

Install the package

Install the SDK together with the extra for your framework. The extra pulls in everything the SDK needs to work with that framework.

pip install "mitulabs-payments[fastapi]" # or [flask] or [django]

The SDK requires Python 3.12 or later.

Keep your secret key out of your code by storing it in an environment variable.

export MITU_API_KEY=your_test_key

Charge for a route

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

main.py
import os
from fastapi import FastAPI, Request
from mitulabs_payments import Mitu
mitu = Mitu(os.environ["MITU_API_KEY"])
app = FastAPI()
@app.get("/v1/search")
@mitu.charge(amount="0.05")
async def search(request: Request):
return {"results": run_search(request.query_params), "payment": request.state.mitu_payment}

The Mitu class sets up a client from your key. You create it once when your application starts and use it on every route that charges. The @mitu.charge decorator sets the route’s price. Place it directly below your framework’s route decorator, as shown above.

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 view runs.
    The request reaches your view with the settled payment available on request.state.mitu_payment, and the response includes the payment receipt in its headers.

Your view never runs for a request that has not been paid, so you do not need to write any payment checks of your own.

In FastAPI, a charged route must accept a request: Request argument, because the SDK needs the request to read the payment. 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, because the caller controls the body and could choose how much to pay.

Flask and Django

The decorator works the same way in every supported framework, and both sync and async views are supported. The only difference is where your view finds the payment.

In Flask, the payment is stored on g.mitu_payment.

from flask import g, request
@app.get("/v1/search")
@mitu.charge(amount="0.05")
def search():
return run_search(request.args, g.mitu_payment)

Try the route

Start your application and call the route without paying.

curl -i http://localhost:8000/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. The paid request returns 200 OK along with your view’s response.

Next steps