Python SDK
The pympp Python library
Overview
The mpp Python library provides a typed interface over the Machine Payments Protocol, from high-level abstractions to low-level primitives and building blocks.
Install
install.sh
$ pip install pymppWith Tempo blockchain support:
install-with-tempo.sh
$ pip install "pympp[tempo]"Requirements
- Python 3.10+
httpxfor async HTTPeth-accountfor Tempo signing (with[tempo]extra)
Quick start
Server
server.py
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from mpp import Challenge
from mpp.server import Mpp
from mpp.methods.tempo import tempo, ChargeIntent
app = FastAPI()
mpp = Mpp.create(
method=tempo(
currency="0x20c0000000000000000000000000000000000000",
recipient="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
intents={"charge": ChargeIntent()},
),
)
@app.get("/resource")
async def get_resource(request: Request):
result = await mpp.charge(
authorization=request.headers.get("Authorization"),
amount="0.50",
)
if isinstance(result, Challenge):
return JSONResponse(
status_code=402,
content={"error": "Payment required"},
headers={"WWW-Authenticate": result.to_www_authenticate(mpp.realm)},
)
credential, receipt = result
return {"data": "paid content", "payer": credential.source}Client
client.py
import asyncio
from mpp.client import Client
from mpp.methods.tempo import tempo, TempoAccount, ChargeIntent
async def main():
account = TempoAccount.from_env()
async with Client(methods=[tempo(account=account, intents={"charge": ChargeIntent()})]) as client:
response = await client.get("https://mpp.dev/api/ping/paid")
print(response.json())
asyncio.run(main())