Event Notification Service signs every notification delivered to your callback with an HMAC-SHA256 signature. The signature is produced by signing the entire notification payload using a signature key unique to the callback. Because the signature is a hash of the payload, the x-sfmc-ens-signature is different for each request.
You receive the signature key when you create the callback. You can retrieve a callback's signature key only during callback creation. Save the signature key to use later.
The x-sfmc-ens-signature is base64 encoded. To provide the HMAC-SHA256 hash, you must first decode the signature. To validate the authenticity of a notification, use the callback signature key to create an HMAC-SHA256 signature of the notification payload received. Then compare the HMAC-SHA256 signature to the value in x-sfmc-ens-signature. If the values match, Marketing Cloud Engagement sent the notification.
This pseudocode example describes a basic algorithm for validating a request.
note: This example uses pseudocode and is intended for illustrative purposes only. The code isn’t intended to be executed.
callback_signature_key = YOUR_KEY
// 1. Receive notification with payload and signature header
notification_payload = received_notification.body
received_signature = received_notification.headers['x-sfmc-ens-signature']
// 2. Decode the base64 signature
decoded_signature = base64_decode(received_signature)
// 3. Create HMAC-SHA256 signature using your callback signature key
calculated_signature = hmac_sha256(
key: callback_signature_key,
message: notification_payload
)
// 4. Compare signatures to validate authenticity
if calculated_signature == decoded_signature:
// Notification came from Marketing Cloud Engagement
process_notification(notification_payload) // Handle the notification
else:
reject_notification() // Handle the inauthentic notification if necessary