https://retellai.com/?utm_source=discord&utm_medium=seo logo
How can i secure my webhook endpoints
# ❓・bug-report
b
I've developed some custom function and webhook endpoints in php and would like to add some more security, how can i verify the request header and specifically the x_retell_signature value?
r
This could be challenging in PHP. You can review the Python SDK code to see how signatures are verified: https://github.com/RetellAI/retell-python-sdk/blob/main/src/retell/lib/webhook_auth.py
You can get some level of security though by checking the IP address
c
if you want to use php continuously, you can manually set header with secret key and hash to verify signature.
please dm me when you need more help
r
@crystaldev1003 we don't offer any control over request headers when tool calls are made.
and ditto with webhooks
you could send a key as a query parameter, but I don't think you necessarily gain a lot there in terms of security
best option is if you can somehow figure out how to implement a PHP version of the auth code
second best option is blocking by IP
s
@brooklyn88ua Here's what I'm using.
Copy code
public static function verifyRetellWebhook($payload, $signature, $apiKey, $logFile)
    {
        $apiKey = trim($apiKey);

        if (empty($apiKey))
        {
            error_log("Retell API key not configured\n", 3, $logFile);
            return false;
        }

        // Ensure API key is UTF-8 encoded
        $apiKey = mb_convert_encoding($apiKey, 'UTF-8');

        // Parse the signature header (e.g., "v=1741628797736,d=ff1454ca...")
        $signatureParts = [];
        parse_str(str_replace(',', '&', $signature), $signatureParts);

        // Extract the digest (d=) and timestamp (v=) from the header
        $receivedDigest = $signatureParts['d'] ?? '';
        $timestamp = $signatureParts['v'] ?? '';

        if (empty($receivedDigest))
        {
            error_log("Invalid signature format: missing digest\n", 3, $logFile);
            return false;
        }
        if (empty($timestamp))
        {
            error_log("Invalid signature format: missing timestamp\n", 3, $logFile);
            return false;
        }

        // Ensure payload is UTF-8 encoded
        $payload = mb_convert_encoding($payload, 'UTF-8');

        // Compute the expected HMAC-SHA256 signature
        $dataToSign = $payload . $timestamp;
        $expectedSignature = hash_hmac('sha256', $dataToSign, $apiKey);

        // Compare the computed signature with the received digest
       return hash_equals($expectedSignature, $receivedDigest);

    }
r
Bravo!
s
Copy code
$signature = $_SERVER['HTTP_X_RETELL_SIGNATURE'] ?? '';
        $apiKey  = "Your API key";
        $logFile = __DIR__ . '/webhook_errors.log';

        if (!verifyRetellWebhook($payload, $signature, $apiKey, $logFile))  // Invalid signature, reject the request
        {
            http_response_code(403); // Forbidden
            $result = ['error' => 'Invalid webhook signature'];
            echo json_encode($result);
            exit;
        }
b
@si2026_40031 Thank you, very much appreciated 🙏
n
For anyone else browsing this in the future, make sure you're using your webhook api key. You'll see a badge "Webhook" on your apiKey dashboard: https://dashboard.retellai.com/apiKey