https://retellai.com/?utm_source=discord&utm_medium=seo logo
<@1145576664366055464> I got some python code I am...
# ❓・ai-evy-support
t
@AI Evy I got some python code I am using with ngrok and a webhook # Install the SDK: https://docs.retellai.com/get-started/sdk from fastapi import FastAPI, Request from fastapi.responses import JSONResponse from retell import Retell retell = Retell(api_key=os.environ[""]) @app.post("/webhook") async def handle_webhook(request: Request): try: post_data = await request.json() if post_data["event"] == "call_started": print("Call started event", post_data["data"]["call_id"]) elif post_data["event"] == "call_ended": print("Call ended event", post_data["data"]["call_id"]) elif post_data["event"] == "call_analyzed": print("Call analyzed event", post_data["data"]["call_id"]) else: print("Unknown event", post_data["event"]) return JSONResponse(status_code=204) except Exception as err: print(f"Error in webhook: {err}") return JSONResponse( status_code=500, content={"message": "Internal Server Error"} ) Step 1. ran ngrok and put the link in my registered webhook link and then I added the link to my agent level webhook. I can't even get this to work I always get 404. What am I doing wrong? I added the key to retell, not sure what else is going wrong. so. far i either get 404 error or 403
a
Thanks for tagging me @trill_og.sol. I'll look through relevant sources and get back to you shortly.
----------------------------- **While I generate an answer, here are some sources that might be helpful**: * [Secure the webhook]() * [Register the webhook]() * [Integrate any system with custom function]() * [Overview]() -----------------------------
### Answer Based on the provided code and information sources, there are a few key issues to address: 1. Missing Webhook Verification Your code is missing the required webhook verification [(1)]() . You need to verify the webhook signature using the x-retell-signature header. Here's the corrected Python code:
Copy code
python
# Install the SDK: https://docs.retellai.com/get-started/sdk
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from retell import Retell

retell = Retell(api_key=os.environ["RETELL_API_KEY"])

@app.post("/webhook")
async def handle_webhook(request: Request):
    try:
        post_data = await request.json()
        valid_signature = retell.verify(
            json.dumps(post_data, separators=(",", ":"), ensure_ascii=False),
            api_key=str(os.environ["RETELL_API_KEY"]),
            signature=str(request.headers.get("X-Retell-Signature")),
        )
        if not valid_signature:
            print(
                "Received Unauthorized",
                post_data["event"],
                post_data["data"]["call_id"],
            )
            return JSONResponse(status_code=401, content={"message": "Unauthorized"})
        if post_data["event"] == "call_started":
            print("Call started event", post_data["data"]["call_id"])
        elif post_data["event"] == "call_ended":
            print("Call ended event", post_data["data"]["call_id"])
        elif post_data["event"] == "call_analyzed":
            print("Call analyzed event", post_data["data"]["call_id"])
        else:
            print("Unknown event", post_data["event"])
        return JSONResponse(status_code=204)
    except Exception as err:
        print(f"Error in webhook: {err}")
        return JSONResponse(
            status_code=500, content={"message": "Internal Server Error"}
        )
2. IP Allowlisting Make sure to allowlist Retell's IP address: 100.20.5.228 3. API Key Management Ensure your API key is properly set up in the dashboard [(2)]() . You need to: - Select an existing API key - Click "Set as Webhook Key" to designate it for webhook authentication 4. Event Types The webhook supports these events [(3)]() : - `call_started`: Triggered when a new call begins - `call_ended`: Triggered when a call completes - `call_analyzed`: Triggered when call analysis is complete If you're still getting 404 errors, double-check that your ngrok URL matches exactly what you've registered in the dashboard, including the
/webhook
endpoint path [(4)]() .
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.