Frameworks
FastAPI — Intégration Scell.io
Async via httpx + dependency injection FastAPI
Scell.io ne publie PAS de SDK Python officiel. Pour FastAPI, utiliser `httpx.AsyncClient` (async natif) injecté via `Depends()`. Définir un client singleton dans le lifespan de l'app, le réutiliser dans toutes les routes pour bénéficier du keep-alive HTTP/2. Header `X-API-Key` requis. Idéal pour throughput élevé (factures en batch).
À retenir
- Client `httpx.AsyncClient` singleton dans `lifespan`
- Injection via `Depends(get_scell_client)`
- Async natif, support HTTP/2 keep-alive
- Pas de SDK Python officiel — fetch HTTP direct
- Idempotency-Key pour les retries
Exemple de code
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()