Hi @Ciaran thanks for reaching out.Only the API key marked with a webhook badge can verify signatures Timestamp is in milliseconds Your timestamp check is incorrect.Don't modify the secret key Use the full API key, don't remove "key"
def check_key
return true if Rails.env.development?
signature_header = request.headers["X-Retell-Signature"].to_s
timestamp = signature_header[/v=(\d+)/, 1]
provided_digest = signature_header[/d=([a-f0-9]+)/i, 1]
if timestamp.blank? || provided_digest.blank?
Rails.logger.warn("Missing or malformed RetellAi signature")
head :unauthorized and return
end
# Fix: timestamp is already in milliseconds, compare directly
if timestamp.to_i < (Time.now.to_i * 1000 - 5.minutes.to_i * 1000)
Rails.logger.warn("Stale RetellAi webhook timestamp")
head :unauthorized and return
end
# Fix: use the full webhook API key, don't remove "key"
secret = Rails.application.credentials.dig(:retell_ai, :webhook_apikey)
unless secret
Rails.logger.error("Missing RetellAi webhook secret in credentials")
head :unauthorized and return
end
raw_body = request.raw_post
message = "#{timestamp}.#{raw_body}"
expected_digest = OpenSSL::HMAC.hexdigest("sha256", secret, message)
unless ActiveSupport::SecurityUtils.secure_compare(expected_digest, provided_digest)
Rails.logger.warn("Invalid RetellAi signature")
head :unauthorized and return
end
end