PaywallError, and the route answers 402 with a checkout link.
Prerequisites
- The Python SDK installed, with
SOLVAPAY_SECRET_KEYandSOLVAPAY_PRODUCT_REFset. See Installation. - Starlette and uvicorn:
pip install starlette uvicorn. FastAPI works the same way.
Make a function paid
1
Write the app
Save this as
app.py:2
Run it
3
Call it
402:How payable() works
customer_refis your own user id. It must be passed as a keyword argument. SolvaPay creates the customer on first use and links it to that id. A call withoutcustomer_refraisesSolvaPayErrorwith codeidentity_required.payable()wraps async functions only.- The product comes from
SOLVAPAY_PRODUCT_REF, or@solvapay.payable(product="prd_..."). It’s resolved when the decorator runs, so set the variable before you import the module. - Use
meter="..."to bill a meter other than the defaultrequests, andcustomer_ref_arg="user_id"when your function names the customer argument differently. - If the function raises, the call is recorded as failed and the exception reaches you unchanged.
Show the paywall
PaywallError.gate is a dict with the API’s camelCase keys. The fields you’ll use most:
The message is written for MCP clients and names the
account tool. In a web app, show your own copy and link to checkoutUrl.Next steps
- Paid MCP server: charge for MCP tools.
- Webhooks: react to purchases.
- The TypeScript equivalent is the quick start.