sangria-core
Python SDK for accepting x402 payments. Supports FastAPI.
Install
pip install sangria-core
Quick Start
FastAPI
from fastapi import FastAPI, Request
from sangria_sdk import SangriaMerchantClient
from sangria_sdk.adapters.fastapi import require_sangria_payment
app = FastAPI()
client = SangriaMerchantClient(
base_url="https://api.sangria.net",
api_key="sg_merchants_...",
)
@app.get("/premium")
@require_sangria_payment(client, amount=0.01, description="Premium content")
async def premium(request: Request):
# request.state.sangria_payment.transaction == "0x..."
return {"data": "premium content"}
How It Works
The @require_sangria_payment decorator handles the x402 negotiation loop:
- First request (no
PAYMENT-SIGNATUREheader): calls Sangria's/v1/generate-paymentendpoint, returns402 Payment Requiredwith payment terms and a base64-encodedPAYMENT-REQUIREDresponse header. - Retry (with
PAYMENT-SIGNATUREheader): forwards the signed payload to Sangria's/v1/settle-paymentendpoint. On success, stores the result inrequest.state.sangria_paymentand calls your handler.
Errors
The SDK raises a SangriaError (or subclass) when the Sangria backend is unreachable, times out, or returns a non-2xx status. Business-level payment failures (bad signature, insufficient funds) are not errors — they flow through as normal 402 responses.
SangriaError # base — catch-all
├── SangriaConnectionError # DNS, refused, socket error
│ └── SangriaTimeoutError # client-side timeout
└── SangriaAPIStatusError # backend returned non-2xx (has .status_code, .response)
Every error carries .operation ("generate" or "settle") so you know which call failed.
from fastapi import Request
from fastapi.responses import JSONResponse
from sangria_sdk import SangriaError
@app.exception_handler(SangriaError)
async def sangria_error_handler(_request: Request, exc: SangriaError):
return JSONResponse(
status_code=503,
content={"error": "Payment provider unavailable"},
)
API Contract
POST /v1/generate-payment
Request:
{ "amount": 10000, "resource": "/premium", "description": "Premium content" }
Response (402):
{
"x402Version": 2,
"accepts": [{
"scheme": "exact",
"network": "eip155:84532",
"amount": "10000",
"asset": "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
"payTo": "0xWalletAddress",
"maxTimeoutSeconds": 60
}],
"resource": { "url": "/premium", "description": "Premium content" }
}
POST /v1/settle-payment
Request:
{ "payment_payload": "<base64 EIP-712 signed authorization>" }
Response:
{ "success": true, "transaction": "0x...", "network": "base", "payer": "0x..." }
Requirements
- Python >= 3.10
- FastAPI >= 0.135.1
- httpx >= 0.28.1
Metadata
Release files for sangria-core 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sangria_core-0.3.4.tar.gz | 9.7 kB | Without provenance | Details for sangria_core-0.3.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sangria_core-0.3.4-py3-none-any.whl | Python 3 | none | any | Details for sangria_core-0.3.4-py3-none-any.whl |
Total release size: 21.7 kB
Release files / sangria_core-0.3.4.tar.gz
| Download URL | sangria_core-0.3.4.tar.gz |
|---|---|
| Size | 9.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4142aa8781e437b3c517e0d4bb160a51921067b0cca8b37ab562fbfc373a944c
|
|
BLAKE2b-256 checksum How to use checksums |
8b482b2fd1ebd829ae4630d521b41b92a0f6ad79e3cd6ca0a01574a010d540c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / sangria_core-0.3.4-py3-none-any.whl
| Download URL | sangria_core-0.3.4-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4228567eb10a5355de5f2e16814191522b0e77a05a8ce66fab3a9bb32c2e53cb
|
|
BLAKE2b-256 checksum How to use checksums |
d6795130be94ceadf495558de249c199abbda76d51cc9b24c4f2d9a7114ebbf9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Security for sangria-core 0.3.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.