Python Merchant SDK for Sangria payment preflight and settlement
Project description
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_live_...",
)
@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"}
Bypass Payments
Skip payment for certain requests. This is useful if you want to let API key users access your endpoints for free while charging anonymous or agent-based callers via x402:
@app.get("/premium")
@require_sangria_payment(
client,
amount=0.01,
bypass_if=lambda req: req.headers.get("x-api-key") is not None,
)
async def premium(request: Request):
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.
API Contract
POST /v1/generate-payment
Request:
{ "amount": 0.01, "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-sepolia", "payer": "0x..." }
Requirements
- Python >= 3.10
- FastAPI >= 0.135.1
- httpx >= 0.28.1
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sangria_core-0.1.0.tar.gz.
File metadata
- Download URL: sangria_core-0.1.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44041a2a845697902483026871b3ec0721efdf44030cb1a81b389b375086a462
|
|
| MD5 |
b7b66ffb294c50062fd5258b6eddbd5a
|
|
| BLAKE2b-256 |
28b2814e9399232bc54fa655979b16ea9031db227ffd58caa9c2fcb875491c4e
|
File details
Details for the file sangria_core-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sangria_core-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f3d9d0494f612f9b905fb4a6dfe4d9959639495b23cdac18b6bc98269dcfade
|
|
| MD5 |
80361f22d0a2ba6f2f6201398fdffec1
|
|
| BLAKE2b-256 |
35c8ccec43f2d7ecfc21938ede32f28fd683ab22794c2db974b8b1240e77288a
|