import json
from fastapi import FastAPI, Request, HTTPException
import hmac
import hashlib
app = FastAPI()
# Secret key for signature validation, store this in a safe place!
SECRET = "961c2c1c9c66dad600f29a26e0320a96"
def generate_signature(body: str, secret: str) -> str:
return hmac.new(secret.encode(), body.encode(), hashlib.sha256).hexdigest()
# Define a route to handle incoming webhooks.
@app.post("/webhook")
async def webhook(request: Request):
data = await request.json()
signature = request.headers.get("Decoda-Signature")
expected_signature = generate_signature(json.dumps(data), SECRET)
# Validate the signature
if not hmac.compare_digest(signature, expected_signature):
raise HTTPException(status_code=401, detail="Invalid signature")
return {"status": "success"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8001)