---
title: "Sign requests with HMAC"
site: api
version: 2027.1
versions: "all"
url: https://docs-new.servicely.ai/api/2027.1/rest/sign-requests-with-hmac/
type: task
application: platform_integration
last_updated: 2026-12-03
commit: 6777698daac5
---


Sign each request with a System API token's shared secret when Servicely must verify who sent it. HMAC body validation also proves that the method, body and path were not changed in transit.

## Before you begin

- You have a System API token with the validation type **HMAC body validation** or **HMAC header validation**, its token and its shared secret. An administrator creates it, see [Create a System API token](page:admin/integrations/create-a-system-api-token) in the Administrator guide.
- You know the token's **Hash method**. It defaults to SHA256, which the examples use. MD5 and SHA1 are also offered.
- The token's **Impersonate user** is a person's user account. Servicely refuses HMAC sign-in for non-human users such as AI agents.
- The clock of the calling system is accurate. Servicely rejects a request whose date is more than five minutes in the past.

## Steps

These steps sign a request for HMAC body validation.

<Steps>
  <Step>
    Format the current time in UTC as an HTTP date, for example `Tue, 12 Jan 2016 14:57:28 GMT`. Send it in the `Date` header.
  </Step>
  <Step>
    If the request has a body, compute its MD5 digest and Base64-encode it. Send the result in the `Content-MD5` header. Without that header, Servicely signs an empty line in its place.
  </Step>
  <Step>
    Build the string to sign: five values joined by newline characters, with no newline at the end.

    | Line | Value |
    | --- | --- |
    | 1 | The method, for example `POST` |
    | 2 | The Base64 MD5 digest of the body, or empty |
    | 3 | The `Content-Type` header exactly as sent, or empty |
    | 4 | The `Date` header exactly as sent |
    | 5 | The path, for example `/v1/Incident`, then `?` and the URL-decoded query string if there is one |

    ```text
    POST
    vVqHE1k/uBRCoWe0FAh95g==
    application/json
    Tue, 12 Jan 2016 14:57:28 GMT
    /v1/Incident
    ```
  </Step>
  <Step>
    Compute the HMAC of the string with the shared secret and the token's hash method, then Base64-encode it. This is the signature.
  </Step>
  <Step>
    Send the token and the signature in the `Authorization` header, separated by a colon.

    ```http
    Authorization: HMAC <token>:<signature>
    ```
  </Step>
</Steps>

This Postman pre-request script does all five steps. Store the token and secret as Postman variables rather than in the script.

```javascript
const token = pm.variables.get("servicelyToken");
const sharedSecret = pm.variables.get("servicelySecret");

const formattedDate = (new Date()).toUTCString();
const contentType = "application/json";
const body = pm.request.body ? pm.request.body.toString() : "";
const bodyMD5 = body !== "" ? CryptoJS.enc.Base64.stringify(CryptoJS.MD5(body)) : "";

// The server signs the path plus the URL-decoded query string
const query = pm.request.url.getQueryString();
const path = pm.request.url.getPath() + (query ? "?" + decodeURIComponent(query.replace(/\+/g, " ")) : "");

const stringToSign = [pm.request.method, bodyMD5, contentType, formattedDate, path].join("\n");
const signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(stringToSign, sharedSecret));

pm.request.headers.upsert({ key: "Date", value: formattedDate });
pm.request.headers.upsert({ key: "Content-Type", value: contentType });
if (bodyMD5 !== "") {
    pm.request.headers.upsert({ key: "Content-MD5", value: bodyMD5 });
}
pm.request.headers.upsert({ key: "Authorization", value: "HMAC " + token + ":" + signature });
```

## Variations

### HMAC header validation

With HMAC header validation, the string to sign is the values of the headers named in the token's **Header key**, joined by colons in the order listed. With the default, `Date`, it is the date alone. Header validation proves the sender knows the secret. It does not protect the body.

This script signs the `Date` header and a custom header. It assumes **Header key** is `Date,X-Custom`.

```javascript
const token = pm.variables.get("servicelyToken");
const sharedSecret = pm.variables.get("servicelySecret");

const formattedDate = (new Date()).toUTCString();
const customValue = pm.variables.replaceIn("{{$guid}}");

const stringToSign = formattedDate + ":" + customValue;
const signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(stringToSign, sharedSecret));

pm.request.headers.upsert({ key: "Date", value: formattedDate });
pm.request.headers.upsert({ key: "X-Custom", value: customValue });
pm.request.headers.upsert({ key: "Authorization", value: "HMAC " + token + ":" + signature });
```

## Result

Servicely recomputes the signature from the request it received and compares it with yours. If they match and the date is recent, the request runs as the token's **Impersonate user**. Any change to the signed values, or a date older than five minutes, makes the request unauthenticated.

## Related

- [Authentication methods for inbound integrations](page:admin/integrations/inbound-authentication)
- [Create a System API token](page:admin/integrations/create-a-system-api-token)
- [Quick start](page:rest/quick-start)
- [Custom endpoints](page:rest/custom-endpoints)
