SDK - Python

Python SDK (paynode-sdk-python)

The Python SDK is highly optimized for Python-based AI workflows (e.g., LangChain, AutoGen) and modern Python backends (FastAPI).

📦 Installation

pip install paynode-sdk-python web3

🤖 For AI Agents (Client)

The Client class inherits from requests.Session. It intercepts 402 errors and seamlessly executes the Base L2 payment before returning the final response.

import os
from paynode_sdk import Client
 
# 1. Initialize with private key
agent = Client(private_key=os.getenv("AGENT_PRIVATE_KEY"))
 
# 2. Make standard requests
# If the server returns 402, the Client pays and retries automatically.
response = agent.get("https://api.merchant.com/inference")
 
if response.status_code == 200:
    print("Data acquired:", response.json())
else:
    print("Failed with status:", response.status_code)

Gas Optimization

The Python SDK automatically applies a 20% gas_price boost to ensure transactions are mined rapidly, avoiding replacement transaction underpriced errors during sequential payments.


🏪 For Merchants (FastAPI Middleware)

The PayNodeMiddleware is designed to be injected into FastAPI routes seamlessly.

from fastapi import FastAPI, Depends
from paynode_sdk.middleware import PayNodeMiddleware
import os
 
app = FastAPI()
 
# 1. Configure the requirement
require_payment = PayNodeMiddleware(
    price=0.50, # 0.50 USDC
    merchant_wallet="0xYourWalletAddress...",
    rpc_url=os.getenv("BASE_RPC_URL", "https://mainnet.base.org")
)
 
# 2. Inject as a dependency
@app.post("/generate-image", dependencies=[Depends(require_payment)])
def generate_image(prompt: str):
    # This block only executes if payment is verified on-chain
    return {
        "status": "success",
        "image_url": "https://bucket.com/image.png"
    }