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 .…