Menu

Stripe webhooks in FastAPI: signature check and idempotent handlers
📰

Stripe webhooks in FastAPI: signature check and idempotent handlers

DEV Community: fastapi·LaunchPad Kits·3 days ago
#yGlCBTEf
#devtofastapi#devto#dev#stripe#event
Long article (608 words) — no AI summary yet.
Reading 0:00
15s threshold

Stripe retries webhooks, delivers them out of order, and anyone who finds your endpoint can POST to it. A handler that trusts the request or runs twice will eventually corrupt a customer's plan. Two rules fix most of it: verify the signature, and make processing idempotent.

1. Verify the signature on the raw body

Stripe signs the exact bytes it sends. Parse the JSON first and the signature check fails, so read the raw body.

@router . post ( "/billing/webhook" , include_in_schema = False ) async def webhook ( request : Request , session : AsyncSession = Depends ( get_session ) , settings : Settings = Depends ( get_settings_dep ) ) : payload = await request . body ( ) try : event = verify_signature ( payload , request . headers . get ( "stripe-signature" , "" ) , settings .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.