coinbase-commerce-python
coinbase-commerce-python copied to clipboard
how to use in webhook with fastapi
Hey, you just need to expose and endpoint where your request payload is a Charge, something like below can help:
@app.post("/coinbase-payment")
async def coinbase_payment(request: Request) -> dict:
"""Handle Webhook Events from Coinbase Commerce"""
payload = await request.body()
signature = request.headers.get('X-CC-Webhook-Signature')
try:
# Validate the webhook payload and signature
event = Webhook.construct_event(payload.decode(), signature, COINBASE_SECRET)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# Handle specific event types
if event.type == 'charge:confirmed':
handle_charge_confirmed(event.data)
elif event.type == 'charge:failed':
handle_charge_failed(event.data)
else:
logging.info(f"Coinbase: Unhandled Event Type {event.type}")
return JSONResponse(content={'status': 'success'})