> ## Documentation Index
> Fetch the complete documentation index at: https://openapi-docs.yidianting.cc/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Verifying Webhook Signatures

> Validate HMAC-SHA256 webhook signatures and protect your endpoint against replay attacks.

Every webhook HTTP request includes an `X-Ake-Signature` header signed with your endpoint's **Webhook Signing Secret** (`whsec_...`):

```http theme={null}
X-Ake-Signature: t=1764215400,v1=9a7d2b44e0f3e6d1...
```

## Verification Steps

1. Parse the timestamp `t` (Unix seconds) and signature `v1` (lowercase hex) from `X-Ake-Signature`.
2. Verify `t` is within an acceptable tolerance window (recommended: **300 seconds**) of your server's current UTC time to prevent replay attacks.
3. Construct the signed payload string: `${t}.${rawRequestBody}` (UTF-8, using the exact raw bytes before JSON parsing).
4. Compute `HMAC-SHA256` using your signing secret and compare against `v1` using a **constant-time comparison**.

## Code Examples

```javascript Node.js theme={null}
import crypto from "node:crypto";

export function verifyAkeWebhook(rawBody, signatureHeader, secret) {
  const parts = Object.fromEntries(
    signatureHeader.split(",").map((kv) => kv.split("="))
  );
  const timestamp = Number(parts.t);
  const expectedSig = parts.v1;

  if (!timestamp || !expectedSig) return false;
  if (Math.abs(Math.floor(Date.now() / 1000) - timestamp) > 300) {
    return false;
  }

  const signedPayload = `${timestamp}.${rawBody}`;
  const computed = crypto
    .createHmac("sha256", secret)
    .update(signedPayload, "utf8")
    .digest("hex");

  const a = Buffer.from(computed, "hex");
  const b = Buffer.from(expectedSig, "hex");
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}
```

```python Python theme={null}
import hashlib
import hmac
import time

def verify_ake_webhook(raw_body: bytes, signature_header: str, secret: str) -> bool:
    parts = dict(item.split("=", 1) for item in signature_header.split(",") if "=" in item)
    timestamp = int(parts.get("t", "0"))
    expected_sig = parts.get("v1", "")

    if abs(int(time.time()) - timestamp) > 300:
        return False

    signed_payload = f"{timestamp}.".encode("utf-8") + raw_body
    computed = hmac.new(
        secret.encode("utf-8"), signed_payload, hashlib.sha256
    ).hexdigest()
    return hmac.compare_digest(computed, expected_sig)
```

```java Java theme={null}
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.HexFormat;

public class AkeWebhookVerifier {
    public static boolean verify(String rawBody, String header, String secret) throws Exception {
        String[] parts = header.split(",");
        long timestamp = 0;
        String sigHex = "";
        for (String part : parts) {
            if (part.startsWith("t=")) timestamp = Long.parseLong(part.substring(2));
            if (part.startsWith("v1=")) sigHex = part.substring(3);
        }
        if (Math.abs(Instant.now().getEpochSecond() - timestamp) > 300) return false;

        String payload = timestamp + "." + rawBody;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        String computed = HexFormat.of().formatHex(mac.doFinal(payload.getBytes(StandardCharsets.UTF_8)));

        return MessageDigest.isEqual(
            computed.getBytes(StandardCharsets.UTF_8),
            sigHex.getBytes(StandardCharsets.UTF_8)
        );
    }
}
```

```go Go theme={null}
package webhook

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "math"
    "strconv"
    "strings"
    "time"
)

func VerifySignature(rawBody []byte, header, secret string) bool {
    var ts int64
    var v1 string
    for _, p := range strings.Split(header, ",") {
        if strings.HasPrefix(p, "t=") {
            ts, _ = strconv.ParseInt(strings.TrimPrefix(p, "t="), 10, 64)
        } else if strings.HasPrefix(p, "v1=") {
            v1 = strings.TrimPrefix(p, "v1=")
        }
    }
    if ts == 0 || v1 == "" {
        return false
    }
    if math.Abs(float64(time.Now().Unix()-ts)) > 300 {
        return false
    }

    mac := hmac.New(sha256.New, []byte(secret))
    mac.Write([]byte(fmt.Sprintf("%d.%s", ts, string(rawBody))))
    expected := hex.EncodeToString(mac.Sum(nil))
    return hmac.Equal([]byte(expected), []byte(v1))
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.