https://retellai.com/?utm_source=discord&utm_medium=seo logo
, I need help securing a webhook in ruby
# ❓・ai-evy-support
c
@AI Evy , I need help securing a webhook in ruby. Has anyone done this before? Nothing I seem to do is working.
a
Thanks for tagging me @carsonr317. I'll look through relevant sources and get back to you shortly.
### Answer To secure a webhook, you verify the
x-retell-signature
header together with your Retell API Key. [(1) ]() The documentation provides sample code for Node.js and Python only. There is no official Ruby SDK or Ruby verification example in the available sources. However, the core verification logic uses the
x-retell-signature
header and your API key (specifically the one with a "webhook badge" next to it) . You can also allowlist the Retell IP address:
100.20.5.228
. If you want to implement this in Ruby, you'd need to replicate the signature verification logic from the SDK. Would you like me to provide a general answer on how webhook signature verification typically works so you can adapt it for Ruby? Check out these potentially useful sources: - [Secure the webhook]() - [Webhook Overview]() - [Node.js/Python SDK]()
​ To learn more, see the sources I considered: [[1] Secure the webhook](https://docs.retellai.com/features/secure-webhook) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
I have done this and written code and nothing I seem to try works
Can I speak with a real person?
@AI Evy I have done this and still it doesn't work. I am doing this OpenSSL::HMAC.hexdigest('SHA256', secret, raw_body) to the request raw body and my api key
a
Thanks for the follow-up, @carsonr317. I'll get back to you soon.
### Answer Based on the community discussions, the issue is that the signature format includes a timestamp that must be part of the payload you sign. [(1) ]() [(2) ]() [(3) ]() The
x-retell-signature
header has the format:
v=<timestamp>,d=<signature>
[(1) ]() You need to construct the message as
"#{timestamp}.#{raw_body}"
before hashing. [(3) ]() Here's the corrected approach for Ruby based on what worked in community discussions:
Copy code
ruby
signature_header = request.headers["x-retell-signature"]
timestamp, provided_digest = signature_header.split(',') .map { |part| part.split('=') [1] }

raw_body = request.raw_post
message = "#{timestamp}.#{raw_body}"

expected_digest = OpenSSL::HMAC.hexdigest("sha256", secret, message)
Key points: - Only the API key with the webhook badge next to it can be used to verify webhooks [(4) ]() [(5) ]() - You must extract the timestamp from the header and prepend it to the body with a period separator [(3) ]() - Use the full webhook API key as your secret If you're still having issues, the official recommendation is to use the SDK verification function rather than implementing your own HMAC calculation. For direct support, you can: - Visit [Contact Sales]() - Check [)
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
@AI Evy can you give me a full copy and paste function that ruby devs have used that works?
a
Thanks for the follow-up, @carsonr317. I'll get back to you soon.
### Answer --- I don't have a working, copy-and-paste Ruby function from the sources. The official documentation only provides sample code for Node.js and Python, not Ruby. [(1) ]() The community discussions show attempts at Ruby implementations, but those attempts were still failing with signature mismatches—no confirmed working Ruby solution is documented. [(2) ]() [(3) ]() The official recommendation from support is to use the SDK verification function rather than implementing your own HMAC calculation. [(3) ]() However, there is no official Ruby SDK available. Your options: 1. Use the Node.js or Python SDK in a microservice to handle webhook verification, then forward to your Ruby app [(1) ]() 2. Contact Retell support directly for Ruby-specific guidance: - Visit [Contact Sales]() - Check [) Would you like a general answer on how the signature verification logic typically works based on the format shown in the discussions, so you can continue debugging?
​ To learn more, see the sources I considered: [[1] Secure the webhook](https://docs.retellai.com/features/secure-webhook) [[2] Webhook Signature Verification Failed](https://discord.com/channels/1186424376300679209/1399212780183097498) [[3] Webhook Verification](https://discord.com/channels/1186424376300679209/1400483295082451026) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
I still haven't gotten a working ruby implementation for this. Is there anything?
@AI Evy I still haven't gotten a working ruby implementation for this. Is there anything?
a
Thanks for the follow-up, @carsonr317. I'll get back to you soon.
### Answer --- There is no confirmed working Ruby implementation in the available sources. [(1) ]() [(2) ]() [(3) ]() The official documentation only provides sample code for Node.js and Python. [(1) ]() Community discussions show Ruby attempts that were still failing with signature mismatches, and a support response in one thread confirmed there is no working Ruby implementation yet: > "I have not gotten this to work... is there any working implementation for ruby code yet?" [(2) ]() Key details from the sources for your debugging: - Only the API key with a webhook badge can verify webhooks [(1) ]() [(4) ]() - Use the full API key as the secret—don't remove "key_" [(2) ]() - The signature format is
v=<timestamp>,d=<signature>
[(2) ]() [(3) ]() - The message to sign is
"#{timestamp}.#{raw_body}"
[(2) ]() - You can also allowlist Retell IP address:
100.20.5.228
[(1) ]() Your best options: 1. Use a Node.js or Python microservice for webhook verification and forward to your Ruby app 2. Contact Retell support directly for Ruby-specific guidance: - [Contact Sales]() - [
]() ---
​ To learn more, see the sources I considered: [[1] Secure the webhook](https://docs.retellai.com/features/secure-webhook) [[2] Webhook Verification](https://discord.com/channels/1186424376300679209/1400483295082451026) [[3] Webhook Signature Verification Failed](https://discord.com/channels/1186424376300679209/1399212780183097498) [[4] Manage API Keys](https://docs.retellai.com/accounts/manage-api-keys) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
@AI Evy def verify_retell_webhook! secret = Carriers::RetellClient::WEBHOOK_API_KEY signature = request.headers['X-Retell-Signature'].to_s timestamp_match = signature.match(/v=(\d+)/) digest_match = signature.match(/d=([a-f0-9]+)/) timestamp = timestamp_match&.[](1) expected = digest_match&.[](1) raw_body = request.raw_post.to_s.strip.force_encoding('ASCII-8BIT') message = "#{timestamp}.#{raw_body}" computed = OpenSSL::HMAC.hexdigest('SHA256', secret, message) return render json: :success if ActiveSupport::SecurityUtils.secure_compare(computed, expected) Rollbar.warn( 'Retell webhook verification failed - Invalid Signature Digest', signature_header: signature, extracted_digest: expected, computed_signature: computed, message_to_sign: message, raw_body_encoding: raw_body.encoding.to_s ) return head :unauthorized end This is my function in ruby that is NOT working. Functionally, am I doing anything wrong here?? Steps: Get our secret Get the value sent in header Get the timestamp and the digest separately Get the raw body Concatenate the timestamp with raw body Hex digest Compare
a
Thanks for the follow-up, @carsonr317. I'll get back to you soon.
### Answer Your logic looks correct based on the community discussions. However, there's one potential issue: Remove
.strip
from
raw_body
In your code you have:
Copy code
ruby
raw_body = request.raw_post.to_s.strip.force_encoding('ASCII-8BIT')
The
.strip
call removes leading/trailing whitespace, which would change the body and cause a signature mismatch. The signature is computed on the exact raw body as sent. Based on community discussions, the correct approach is: [(1) ]()
Copy code
ruby
raw_body = request.raw_post
message = "#{timestamp}.#{raw_body}"
expected_digest = OpenSSL::HMAC.hexdigest("sha256", secret, message)
Other things to verify: - Ensure you're using the API key with the webhook badge next to it as your secret - Use the full API key—don't remove "key_" prefix - Make sure no middleware is modifying
request.raw_post
before you access it Unfortunately, community discussions show Ruby implementations were still failing with signature mismatches, and there's no confirmed working Ruby solution documented yet. For direct support: - Visit [Contact Sales]() - Check [) Would you like a general answer on other debugging approaches?
​ To learn more, see the sources I considered: [[1] Webhook Verification](https://discord.com/channels/1186424376300679209/1400483295082451026) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
@User Do I have to do .raw_post on the request? Could I just do request.body? Or do I need to do JSON.parse(request.body)
@AI Evy Do I have to do .raw_post on the request? Could I just do request.body? Or do I need to do JSON.parse(request.body)
@AI Evy Do I have to do .raw_post on the request? Could I just do request.body? Or do I need to do JSON.parse(request.body)
a
Thanks for the follow-up, @carsonr317. I'll get back to you soon.
### Answer --- You need to use the raw, unparsed body for signature verification—not a parsed JSON object. Based on a community discussion, a user encountered this exact issue: [(1) ]() > "I was doing
JSON.stringify(request.body)
instead of doing something like
JSON.stringify(data)
where data is just the `request.json() `" The key insight is that the signature is computed against the exact bytes sent in the request body. If you parse it to JSON and then stringify it back, the formatting may differ (whitespace, key order, etc.) , causing a mismatch. In Ruby: - Use
request.raw_post
to get the untouched raw body string - Do not use
JSON.parse(request.body)
and then re-serialize it - Do not use
request.body.read
if middleware has already consumed the stream The official Node.js example shows using the raw body directly for verification. [(2) ]() Summary: Stick with
request.raw_post
and avoid any parsing/re-serialization before computing the HMAC.
​ To learn more, see the sources I considered: [[1] Retell Webhook Verification keeps failing](https://discord.com/channels/1186424376300679209/1309543929200508988) [[2] Setup guide](https://docs.retellai.com/features/register-webhook) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
Are my steps correct that I gave earlier? Do I need to flip the OpenSSL::HMAC.hexdigest('SHA256', secret, message) and do message first then secret?