Does FastAPI Work With Lemon Squeezy?
FastAPI and Lemon Squeezy work together seamlessly—use FastAPI to build your backend and integrate Lemon Squeezy's API for payments, subscriptions, and product management.
Quick Facts
How FastAPI Works With Lemon Squeezy
FastAPI's async-first design makes it ideal for integrating with Lemon Squeezy's REST API. You'll typically use FastAPI to create webhook endpoints that receive payment events (orders, subscriptions, refunds) from Lemon Squeezy, handle them in your database, and manage user access accordingly. The framework's built-in validation with Pydantic ensures webhook payloads are correctly typed before processing, reducing bugs. For outbound requests, use `httpx` or `aiohttp` to asynchronously fetch products, check subscription status, or manage customer data from Lemon Squeezy's API. The automatic OpenAPI documentation is a bonus—you can document your payment-related endpoints alongside your core API. Most developers structure this by creating a dedicated module for Lemon Squeezy API calls and another for webhook handlers, keeping concerns separated. Authentication is straightforward: store your Lemon Squeezy API key in environment variables and include it in request headers.
Best Use Cases
Quick Setup
pip install fastapi uvicorn httpx python-dotenv pydanticimport hmac
import hashlib
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
import httpx
import os
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
LS_API_KEY = os.getenv("LEMONSQUEEZY_API_KEY")
LS_WEBHOOK_SECRET = os.getenv("LEMONSQUEEZY_WEBHOOK_SECRET")
class WebhookEvent(BaseModel):
data: dict
def verify_webhook_signature(payload: bytes, signature: str) -> bool:
expected = hmac.new(
LS_WEBHOOK_SECRET.encode(),
payload,
hashlib.sha256
).hexdigest()
return hmac.compare_digest(expected, signature)
@app.post("/webhooks/lemonsqueezy")
async def handle_webhook(request: Request):
payload = await request.body()
signature = request.headers.get("X-Signature", "")
if not verify_webhook_signature(payload, signature):
raise HTTPException(status_code=401, detail="Invalid signature")
event = await request.json()
# Process event (e.g., create user license, update subscription)
print(f"Event: {event['meta']['event_name']}")
return {"success": True}
@app.get("/products")
async def get_products():
async with httpx.AsyncClient() as client:
response = await client.get(
"https://api.lemonsqueezy.com/v1/products",
headers={"Authorization": f"Bearer {LS_API_KEY}"}
)
return response.json()Known Issues & Gotchas
Webhook signature verification—Lemon Squeezy signs webhooks with HMAC-SHA256, and failing to verify them opens security vulnerabilities
Fix: Always validate the webhook signature using the `X-Signature` header and your webhook secret. Libraries like `hmac` in Python make this straightforward.
Async database operations can timeout if Lemon Squeezy API is slow, causing webhook handlers to fail silently
Fix: Use FastAPI's background tasks or an async queue (Celery, RQ) to process webhook events asynchronously, returning a 200 response immediately to prevent retries.
Rate limiting—Lemon Squeezy has API rate limits, and unoptimized FastAPI code can quickly hit them
Fix: Implement exponential backoff for API calls and cache frequently accessed data (products, license keys) using Redis or in-memory caching.
Missing environment variable for API keys will cause runtime errors in production
Fix: Use `python-dotenv` for local development and ensure CI/CD properly injects secrets. Use FastAPI's dependency injection to centralize credential management.
Alternatives
- •Django REST Framework + Stripe: Stripe offers more advanced fraud detection but less built-in tax handling than Lemon Squeezy
- •Express.js + Paddle: Lightweight JavaScript stack with Paddle's global payment support, though less opinionated than FastAPI
- •NestJS + Gumroad: TypeScript-first backend with Gumroad's creator-friendly approach, better for indie developers than enterprises
Resources
Related Compatibility Guides
Explore more compatibility guides