> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.mitu.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.mitu.ai/_mcp/server.

# Verify signatures

Mitu signs everything it sends to your systems. Webhook deliveries are signed so you know an event really came from Mitu, and requests forwarded to your API when an agent uses your product are signed so you know they passed through Mitu and were paid for where payment applies. Checking the signature before you act protects you from anyone who discovers your endpoint and tries to imitate Mitu.

Both signatures work the same way. Mitu combines a timestamp with the content it is sending, signs the result with a secret that only you and Mitu hold, and sends the signature in the x-mitu-signature header.

```http
x-mitu-signature: t=1788990420,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
```

The t value is the time the message was signed, in Unix seconds, and v1 is an HMAC-SHA256 signature encoded as hexadecimal.

#### Find your signing secret

Each webhook endpoint has its own signing secret, which you can find in the dashboard under **Webhooks**. Requests forwarded to your API are signed with the secret under **Integration**, which is shown only when it is created, so copy it at that point. You can rotate it there at any time.

Store each secret with your other server credentials. They are only used to verify what Mitu sends you, so they never need to leave your servers.

#### Read the timestamp and signature

Split the x-mitu-signature header on commas, then split each part on the first equals sign. Take t as the timestamp and v1 as the signature you will check.

#### Rebuild the signed content

Join the values below with full stops, in this order, using the request body exactly as it arrived. Parsing the JSON and serialising it again can change spacing or field order, and the signature will no longer match.

| Message             | Signed content                                                                                                                       |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| Webhook delivery    | The timestamp and the raw body                                                                                                       |
| Request to your API | The timestamp, the x-mitu-request-id header, the x-mitu-amount-charged header (or an empty value when it is absent) and the raw body |

For example, a webhook signed at 1788990420 has the signed content `1788990420.{"event":"payment.completed",...}`.

#### Compute and compare the signature

Compute an HMAC-SHA256 of the signed content using your signing secret, encode it as hexadecimal and compare it with v1. Use a constant time comparison, which takes the same time whether or not the values match and so reveals nothing to an attacker.

Finally, check that the timestamp is recent. Rejecting anything signed more than a few minutes ago stops a captured message from being replayed later. Five minutes is a sensible tolerance for most systems.

The example below verifies a webhook delivery. To verify a request to your API, change only the signed content, as described in the table above.

#### Node.js

```js
import crypto from 'node:crypto'

export function verifyMituSignature(header, rawBody, secret, toleranceSeconds = 300) {
  const parts = Object.fromEntries(
    (header ?? '').split(',').map((part) => {
      const i = part.indexOf('=')
      return [part.slice(0, i), part.slice(i + 1)]
    }),
  )
  const { t, v1 } = parts
  if (!t || !v1) return false

  if (Math.abs(Date.now() / 1000 - Number(t)) > toleranceSeconds) return false

  const expected = crypto
    .createHmac('sha256', secret)
    .update(`${t}.${rawBody}`)
    .digest('hex')

  return (
    expected.length === v1.length &&
    crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(v1))
  )
}
```

#### Python

```python
import hashlib
import hmac
import time


def verify_mitu_signature(header: str, raw_body: bytes, secret: str, tolerance: int = 300) -> bool:
    parts = dict(part.split("=", 1) for part in (header or "").split(",") if "=" in part)
    t, v1 = parts.get("t"), parts.get("v1")
    if not t or not v1:
        return False

    if abs(time.time() - int(t)) > tolerance:
        return False

    signed = t.encode() + b"." + raw_body
    expected = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, v1)
```

#### Go

```go
package mituwebhook

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

func VerifyMituSignature(header string, rawBody []byte, secret string, tolerance time.Duration) bool {
	parts := map[string]string{}
	for _, part := range strings.Split(header, ",") {
		if key, value, ok := strings.Cut(part, "="); ok {
			parts[key] = value
		}
	}
	t, v1 := parts["t"], parts["v1"]
	if t == "" || v1 == "" {
		return false
	}

	signedAt, err := strconv.ParseInt(t, 10, 64)
	if err != nil {
		return false
	}
	age := time.Since(time.Unix(signedAt, 0))
	if age > tolerance || age < -tolerance {
		return false
	}

	mac := hmac.New(sha256.New, []byte(secret))
	mac.Write([]byte(t + "."))
	mac.Write(rawBody)
	expected := hex.EncodeToString(mac.Sum(nil))

	return hmac.Equal([]byte(expected), []byte(v1))
}
```

Only act on a message once its signature has been verified. After that, use the event_id of a webhook or the x-mitu-request-id of a request to make sure the same message is never processed twice.