Building Stripe subscriptions in FastAPI with async SQLAlchemy
The article walks through a production setup for Stripe subscriptions covering Checkout, a local PostgreSQL copy of billing data, idempotent webhook handling, and the customer portal. Stripe remains the authoritative source for payments, while the app caches customers, subscriptions, and processed events so access checks need no API call. Data is written when Checkout starts, when the success page syncs a new subscription, and afterward through webhooks that handle renewals, failed payments, cancellations, and plan changes. The stack uses FastAPI, SQLAlchemy 2.0 async with asyncpg, PostgreSQL, and the official stripe package, with the author recommending the StripeClient's async methods plus an HTTPX client for non-blocking calls. The author also suggests isolating Stripe calls behind a gateway module that tests can replace with fakes, and storing plan-to-price mappings in configuration rather than the database.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.



Discussion (0)
Log in to join the discussion and vote.
Log in