Verify an HMAC-SHA256 request signature
https://api.testingapis.com/api/auth/hmacHeaders:
X-Timestamp: current Unix time in seconds; must be within 300 seconds of server time. X-Signature: lowercase hex of HMAC-SHA256(key: "apibee-hmac-secret", message: X-Timestamp + "." + raw request body). An optional sha256= prefix is accepted. Bash: SIG=$(printf '%s' "$TS.$BODY" | openssl dgst -sha256 -hmac apibee-hmac-secret | awk '{print $NF}')
Note: X-Signature = hex(HMAC-SHA256("apibee-hmac-secret", X-Timestamp + "." + rawBody)); timestamp must be within 5 minutes. Each signature is accepted once: the identical request sent again within the 5-minute window gets 401 (replay refused).
HMAC-SHA256 request signature (X-Timestamp + X-Signature), secret apibee-hmac-secret
Parameters 2
| Name | Type | Description |
|---|---|---|
X-Timestamprequired | header · string | Unix time in seconds; must be within 5 minutes of the server clock. |
X-Signaturerequired | header · string | Hex HMAC-SHA256 of "{X-Timestamp}.{raw body}" with secret apibee-hmac-secret (optional sha256= prefix). |
Request body
Optional. application/json. Any body; it is covered by X-Signature.
{"event":"ping"}Responses
- 200
- Signature valid.
- 401
- Headers missing, timestamp not numeric or outside the tolerance, signature mismatch, or a replayed signature.
Example
curl -X POST "https://api.testingapis.com/api/auth/hmac" \
-H "Content-Type: application/json" \
-H "X-Timestamp: {{unixTimestamp}}" \
-H "X-Signature: {{hmacSignature}}" \
-d '{"event":"ping"}'{"authenticated": true, "scheme": "hmac-sha256", "timestamp": 1790792796, "bodyLength": 16}Code examples
const response = await fetch("https://api.testingapis.com/api/auth/hmac", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Timestamp": "{{unixTimestamp}}",
"X-Signature": "{{hmacSignature}}",
},
body: "{\"event\":\"ping\"}",
});
console.log(response.status, await response.text());import requests
response = requests.request(
"POST",
"https://api.testingapis.com/api/auth/hmac",
headers={
"Content-Type": "application/json",
"X-Timestamp": "{{unixTimestamp}}",
"X-Signature": "{{hmacSignature}}",
},
data="{\"event\":\"ping\"}".encode("utf-8"),
allow_redirects=False,
)
print(response.status_code, response.text)import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.testingapis.com/api/auth/hmac"))
.header("Content-Type", "application/json")
.header("X-Timestamp", "{{unixTimestamp}}")
.header("X-Signature", "{{hmacSignature}}")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"event\":\"ping\"}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}