Skip to main content

SettleSettle Python SDK

The official Python SDK for SettleSettle — the Revenue OS for African developers. Securely monetize your apps via local fiat rails (Paystack, Flutterwave) or stablecoin payments (Solana USDC) with built-in credit wallets, subscription engines, and automated event tracking.


Features

  • Sync & Async Support: Supports both synchronous (Django, Flask, Celery) and asynchronous (FastAPI, Starlette) Python frameworks.
  • Credit Wallets: Effortlessly manage credit packages, debits, credits, and ledger histories.
  • Subscriptions Engine: Add recurring pricing, trial period checks, and subscription access control.
  • Omni-Billing: Single unified API for fiat, crypto, credit, and subscription setups.
  • Usage-Based Tracking: Non-blocking background event buffering and reporting.
  • Rewarded Ad Support: Verified rewarded ad slot integrations to monetize users with zero-credit fallbacks.

Installation

pip install settlesettle

Quick Start

1. Initialize the Client

Set the SETTLESETTLE_API_KEY environment variable:

export SETTLESETTLE_API_KEY="ss_live_..."

Or pass it directly:

from settlesettle import SettleSettle

settle = SettleSettle(api_key="ss_live_...")

Usage Examples

Synchronous (Django / Flask)

from settlesettle import SettleSettle, InsufficientCreditsError

# Reads SETTLESETTLE_API_KEY from environment
settle = SettleSettle()

def process_ai_query(user_id: str, prompt: str):
    try:
        # 1. Debit the wallet for the action cost
        settle.wallet.debit(user_id, amount=10, description="AI Query")
    except InsufficientCreditsError as e:
        return {
            "error": "insufficient_credits",
            "current_balance": e.current_balance,
            "requested": e.requested_amount,
        }

    # 2. Run the actual business logic
    result = run_ai_query(prompt)

    # 3. Track the usage event (non-blocking, buffered)
    settle.events.track(
        user_id=user_id,
        event_type="AI_QUERY",
        metadata={"prompt_length": len(prompt)}
    )

    return {"result": result}

Asynchronous (FastAPI)

from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from settlesettle import AsyncSettleSettle, InsufficientCreditsError

# Initialize async client
settle = AsyncSettleSettle()

@asynccontextmanager
async def lifespan(app: FastAPI):
    yield
    # Flush buffered events and close HTTP connections on shutdown
    await settle.destroy()

app = FastAPI(lifespan=lifespan)

@app.post("/query")
async def query(user_id: str, prompt: str):
    try:
        await settle.wallet.debit(user_id, amount=10, description="AI Query")
    except InsufficientCreditsError as e:
        raise HTTPException(
            status_code=402,
            detail={
                "error": "insufficient_credits",
                "current_balance": e.current_balance,
                "requested": e.requested_amount,
            }
        )

    result = await run_ai_query(prompt)
    
    # Non-blocking event tracking
    settle.events.track(user_id, "AI_QUERY")
    
    return {"result": result}

Context Manager (Scripts & Cron Jobs)

from settlesettle import SettleSettle

# Context manager guarantees events are flushed and resources freed on exit
with SettleSettle() as settle:
    balance = settle.wallet.get_balance("user_123")
    print(f"Credits remaining: {balance.balance}")

Error Handling

All SDK exceptions inherit from SettleSettleError:

from settlesettle import (
    SettleSettleError,
    AuthenticationError,
    InsufficientCreditsError,
    ValidationError,
    RateLimitError,
)

try:
    settle.wallet.debit("user_123", amount=100)
except InsufficientCreditsError as e:
    # Trigger payment modal or display rewarded ad slot
    pass
except ValidationError as e:
    # Invalid request inputs
    print(f"Validation failed: {e.fields}")
except AuthenticationError:
    # Invalid API key
    pass
except RateLimitError as e:
    # Back off
    pass
except SettleSettleError as e:
    # Catch-all
    pass

Development and Testing

  1. Clone the repository and install development dependencies in editable mode:

    pip install -e ".[dev]"
    
  2. Run tests with pytest:

    pytest
    
  3. Run linting & formatting with ruff:

    ruff check settlesettle/
    ruff format settlesettle/
    
  4. Run type verification with mypy:

    mypy settlesettle/
    

License

Metadata

Release files for settlesettle 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for settlesettle 0.1.1
File Size Uploaded Provenance Actions
settlesettle-0.1.1.tar.gz 25.0 kB Without provenance Details for settlesettle-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for settlesettle 0.1.1
File Interpreter ABI Platform Actions
settlesettle-0.1.1-py3-none-any.whl Python 3 none any Details for settlesettle-0.1.1-py3-none-any.whl

Total release size: 44.2 kB

Release files / settlesettle-0.1.1.tar.gz

Download URL settlesettle-0.1.1.tar.gz
Size 25.0 kB
Tags Source
SHA-256 checksum
How to use checksums
783a3facfd113ee5391dcb609d05f9df8d9bea08c112b173139ca833043da9fd
BLAKE2b-256 checksum
How to use checksums
a4dd96a9a8bd53780099344ec95f6601a2ee2d326ad6c6d279a2923c984106db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / settlesettle-0.1.1-py3-none-any.whl

Download URL settlesettle-0.1.1-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bcb1cd0fece59449f2d5400ae4998106817d308b07e724202cb5d7cd5797141
BLAKE2b-256 checksum
How to use checksums
6e742e26278a93e27b01900661e5631417fb53abf49d4c08c119c08469ee6535
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for settlesettle 0.1.1

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page