# Callbacks security

For security reasons, when sending and receiving data and information to and from an external endpoint (i.e. [real-time webhooks](/core/real-time-webhooks.md), [external resources](/core/external-resources.md)) we recommend verifying the callback authenticity by signing the payload with the shared secret ([SHA256 HMAC](https://en.wikipedia.org/wiki/HMAC)) and comparing the result with the **X-CommerceLayer-Signature** callback header. In details:

1. Read the **X-CommerceLayer-Signature** header and get the encrypted signature.
2. Rebuild the signature according to the SHA256 HMAC algorithm, using the payload body and the provided shared secret.
3. Compare your signature with the one you got from the header.
4. If the two signatures match, you can proceed safely — if not, you can't trust the callback.

#### Example

This is a sample script in **Node.js** that you can use as a reference to check the signature:

```javascript
import { createHmac } from "node:crypto";

export default (req, res) => {
  const signature = req.headers['x-commercelayer-signature']
  
  const encode = createHmac("sha256", process.env.CL_SHARED_SECRET)
    .update(req.body)
    .digest("base64");
  
  if (req.method === 'POST' && signature === encode) {
		// your-code
    res.status(200).json({
      success: true,
    })
  } else {
    res.status(401).json({
      error: 'Unauthorized',
    })
  }
}
```

{% hint style="warning" %}
When verifying the callback authenticity, make sure to read the **raw body** of the payload and NOT the parsed one.
{% endhint %}

{% hint style="info" %}
Do you prefer to use [Next.js](https://github.com/commercelayer/examples/tree/main/webhooks/nextjs-signature-verification) or [Express.js](https://github.com/commercelayer/examples/tree/main/webhooks/expressjs-signature-verification) to verify the signature? Check the linked code samples from our [Examples](https://github.com/commercelayer/examples/) open-source repository on GitHub.
{% endhint %}

## IP ranges

All the external requests (i.e. [real-time webhooks](/core/real-time-webhooks.md), [external resources](/core/external-resources.md)) are sent from a set of dedicated IP addresses. When you receive requests to your endpoints, please make sure their source IP is included in the following lists:

| Region          | IPs                                                                                          | Description                                                                |
| --------------- | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| **`eu-west-1`** | <p><code>54.246.59.198</code><br><code>99.81.212.137</code><br><code>54.74.119.33</code></p> | Source IPs for customers with organization localized in the **EU region**. |
| **`us-east-1`** | <p><code>3.220.172.67</code><br><code>34.202.177.238</code><br><code>52.0.55.171</code></p>  | Source IPs for customers with organization localized in the **US region**. |

{% hint style="info" %}
In case there are any changes to the list of IPs above, they will be communicated widely in advance through all our channels.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.commercelayer.io/core/callbacks-security.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
