Skip to main content

Frameworks

FastAPI — Scell.io integration

Async via httpx + FastAPI dependency injection

Scell.io does NOT publish an official Python SDK. For FastAPI, use `httpx.AsyncClient` (native async) injected via `Depends()`. Define a singleton client in the app lifespan, reuse across routes for HTTP/2 keep-alive. `X-API-Key` header required. Ideal for high throughput (batch invoicing).

Key facts

  • Singleton `httpx.AsyncClient` in `lifespan`
  • Injected via `Depends(get_scell_client)`
  • Native async, HTTP/2 keep-alive support
  • No official Python SDK — direct HTTP fetch
  • Idempotency-Key for retries

Code example

import os, httpx
from fastapi import FastAPI, Depends
from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    app.state.scell = httpx.AsyncClient(
        base_url='https://api.scell.io/api/v1',
        headers={'X-API-Key': os.environ['SCELL_API_KEY']},
        timeout=30.0,
    )
    yield
    await app.state.scell.aclose()

app = FastAPI(lifespan=lifespan)

@app.post('/invoice')
async def create(amount: float):
    r = await app.state.scell.post('/invoices', json={'format': 'factur-x', 'lines': [{'description': 'Svc', 'quantity': 1, 'unit_price': amount, 'vat_rate': 20.0}]})
    return r.json()

See also

Your cookie preferences

We use cookies to improve your experience. Essential cookies are always active. Cookie policy.