Enable HMAC signatures
Each secret HMAC key is linked to one webhook endpoint. If you have multiple endpoints for receiving webhooks, generate an HMAC key for each of them. You also need to generate a new HMAC key when you switch from test to live. To start receiving HMAC signed webhooks, if the option is available:- In your Customer Area, go to Developers > Webhooks.
- From the list of webhooks, select the one to configure.
- Select Edit webhook or the edit icon **.
- Under Security, generate a new HMAC key or enter an existing HMAC key.
If you generate a new HMAC key, copy it and store it securely in your system. - Select Save configuration.
additionalData field.
For other types of webhooks, although rare, the signature can be included in the request header. When the HMAC signature is included in the header, the steps to follow to verify the signature is different. See Verify HMAC keys returned in the header.
Changing your HMAC key
If you need to change the secret HMAC key used to sign webhook events, it is enough to generate a new HMAC key in your Customer Area. If you generate a new HMAC key, it can take some time to propagate this in our infrastructure, so make sure that you can still accept webhook events signed with your previous HMAC key for some time.Verify using our libraries
You can verify HMAC signatures using Adyen’s packaged HMAC validator libraries:Verify using your own solution
To build your own solution for verifying HMAC signatures, follow these steps:Step 1: Construct the payload
The values used below are from an example webhook. To test your solution, replace the values with actual values that you receive in a webhook event. Concatenate the following values from the webhook event, in the given order:
Assign an empty string to any fields that are empty, and use a colon (”:”) to delimit the values.
For the above values, with an empty
originalReference, you get:
Step 2: Calculate the HMAC signature
For hints about how to calculate the signature, have a look at the library code samples above.- Convert the payload that you constructed to a binary representation, given the UTF-8 charset.
- Convert the hexadecimal HMAC key that you generated in your Customer Area from a hexadecimal string to a binary representation, given the UTF-8 charset.
-
Calculate an HMAC using:
- The SHA256 function.
- The binary representation of the payload.
- The binary representation of the HMAC key.
- To get the final signature, Base64-encode the result.
Step 3: Compare signatures
If the signature that you calculated in Step 2 matches thehmacSignature that you received, you’ll know that the webhook event was sent by Adyen and was not modified during transmission.
Example
Sample HMAC key:Verify HMAC keys returned in the header
For non-standard webhooks, Adyen can sign the webhook with an HMAC signature in the request header, instead of including it in theadditionalData field. If you enabled other webhooks that follow this pattern, for example, the Recurring tokens life cycle events webhook, there are different steps to verify the HMAC signature.
-
For every webhook that you receive, get the values from the following headers:
hmacsignature: Contains the signature.protocol: The protocol used to create the signature, HmacSHA256.
-
Calculate the signature using:
- SHA256.
- The binary representation of the payload.
- The binary representation of the HMAC key.
- Make sure that the request body is as it is—do not deserialize it.
- To get the final signature, Base64-encode the result.
-
Compare the
hmacsignaturereceived from the header and the calculated signature. If the signatures match, then the webhook was sent by Adyen and was not modified during transmission.