Skip to content

Python SDK

The sangho SDK supports Python 3.8+ with both synchronous and asynchronous (asyncio) interfaces. Compatible with Django, FastAPI, Flask, and any standard Python project.

Installation

bash
# With PIP
pip install sangho
# With POETRY
poetry add sangho

Synchronous usage

In synchronous mode, each call blocks until the response is received. Suited for scripts, Celery tasks, and most Django applications.

Environment variables

Never hardcode your API key in source code. Use os.environ[“SANGHO_SECRET_KEY”] or a library like python-dotenv for local development.

Synchronous usage

python
import os
import sangho


sangho.api_key = os.environ["SANGHO_SECRET_KEY"]
# Create a PaymentIntent
intent = sangho.payment_intents.create(
    amount=5000,
    currency="XAF",
    payment_method_types=["mobile_money", "card"],
    metadata={"order_id": "CMD-001"},
)
print(intent.id)             # pi_xxxxxxxxxxxx
print(intent.client_secret)  # pi_xxx_secret_xxx

Asynchronous usage (asyncio)

For FastAPI and any async Python framework, use AsyncSangho instead of Sangho. The interface is identical — all methods are await-able coroutines.

Error handling

All errors inherit from sangho.error.SanghoError. Subclasses allow each business case to be handled distinctly.

Automatic retry

The SDK automatically retries 429 and 5xx errors with exponential backoff. Pass max_retries=0 to the constructor to disable this behavior.

Async usage (FastAPI)

python
import os
from sangho import AsyncSangho
from fastapi import FastAPI


app = FastAPI()
sangho = AsyncSangho(api_key=os.environ["SANGHO_SECRET_KEY"])


@app.post("/create-payment")
async def create_payment(amount: int):
    intent = await sangho.payment_intents.create(
        amount=amount,
        currency="XAF",
        payment_method_types=["mobile_money"],
    )
    return {"client_secret": intent.client_secret}

Error handling

python
import time
import sangho


try:
    intent = sangho.payment_intents.create(
        amount=5000,
        currency="XAF",
    )
except sangho.error.AuthenticationError:
    # Invalid or expired API key
    print("Invalid API key")
except sangho.error.RateLimitError:
    # Too many requests — retry with backoff
    time.sleep(2 ** attempt)
    retry()
except sangho.error.InvalidRequestError as e:
    # Invalid parameters
    print(e.param, e.message)
except sangho.error.SanghoError as e:
    # Generic error
    print(e.code, e.message, e.status_code)