Algorithm Overview
Kie AI uses the HMAC-SHA256 algorithm to generate signatures, ensuring the integrity and authenticity of webhook callbacks. Signature Generation Process:-
Concatenate the data to sign:
taskId + "." + timestampSecondstaskId: Task ID from the request bodytimestampSeconds: Unix timestamp in seconds from theX-Webhook-Timestampheader
-
Calculate HMAC-SHA256 signature:
-
Base64 encode the signature:
Obtain Webhook HMAC Key
You can generate and view yourwebhookHmacKey on the Kie AI Settings Page.
The
webhookHmacKey is used to verify that callback requests originate from Kie AI’s official servers. Keep this key secure and never expose it or commit it to code repositories.Webhook Header Description
When you enable thewebhookHmacKey feature in the settings page, all callback requests will include the following fields in the HTTP headers:
integer
required
Unix timestamp (in seconds) when the callback request was sent.
string
required
Signature generated using the HMAC-SHA256 algorithm with Base64 encoding.Signature generation rule:Where:
taskIdis the task ID from the callback bodytimestampis the value ofX-Webhook-TimestampwebhookHmacKeyis the key you generated in the console
Webhook Verification Process
Follow these steps to verify the legitimacy of webhook requests:1
Read Header Fields
Extract the
X-Webhook-Timestamp and X-Webhook-Signature fields from the HTTP headers.2
Generate Signature
Using your locally stored
webhookHmacKey, generate the HMAC-SHA256 signature following these rules:- Extract
task_idfrom the request body - Concatenate the string:
taskId + "." + timestamp - Generate signature using HMAC-SHA256 algorithm with
webhookHmacKey - Base64 encode the signature result
3
Compare Signatures
Compare the computed signature with
X-Webhook-Signature using a constant-time comparison algorithm to prevent timing attacks.If the signatures match, the webhook request is confirmed to be from Kie AI’s official servers and can be safely processed.
Complete Example Code
Here are complete examples of implementing webhook signature verification in popular programming languages:- Node.js
- Python
- PHP
- Java
