Guides
Leads

Verify the signature

Check X-Open-Signature with an HMAC-SHA256 of the raw body and read the 64-bit ids without losing precision.

Every Subscription API webhook carries the X-Open-Signature header: the HMAC-SHA256 of the body, keyed with the app Secret, in lowercase hex. Check it before processing the event.

What is signed

TikTok signs a version of the payload with escaped unicode: every non-ASCII character becomes \uXXXX with lowercase hex (á becomes á). Mimicr sends exactly those bytes and signs what it sends. So:

  • compute the HMAC over the raw body, as received, before any parsing;
  • if your framework only hands you decoded JSON, serialize it again with non-ASCII escaped and no spaces. That works, but depends on the serializer producing the same bytes; the raw body is safer.

Node.js

javascript
import { createHmac, timingSafeEqual } from "node:crypto";
import express from "express";

const app = express();
const SECRET = process.env.TIKTOK_APP_SECRET;

app.post("/webhooks/tiktok", express.raw({ type: "application/json" }), (req, res) => {
  const expected = createHmac("sha256", SECRET).update(req.body).digest("hex");
  const got = String(req.get("X-Open-Signature") ?? "");
  if (got.length !== expected.length || !timingSafeEqual(Buffer.from(got), Buffer.from(expected))) {
    return res.status(401).end();
  }
  // 64-bit ids: turn long integers into strings before JSON.parse
  const text = req.body.toString("utf8").replace(/([:\[,]\s*)(\d{16,})(?=\s*[,\]}])/g, '$1"$2"');
  const payload = JSON.parse(text);
  for (const lead of payload.entry ?? []) {
    console.log(lead.id, lead.page_id, lead.changes);
  }
  res.status(200).end();
});

Python

python
import hashlib
import hmac
import json
import os

from flask import Flask, abort, request

app = Flask(__name__)
SECRET = os.environ["TIKTOK_APP_SECRET"].encode()

@app.post("/webhooks/tiktok")
def tiktok_webhook():
    raw = request.get_data()
    expected = hmac.new(SECRET, raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, request.headers.get("X-Open-Signature", "")):
        abort(401)
    payload = json.loads(raw)  # Python integers keep full precision
    for lead in payload.get("entry", []):
        print(lead["id"], lead["page_id"], lead["changes"])
    return "", 200

64-bit ids

In webhooks, request_id, id, page_id, advertiser_id, campaign_id, adgroup_id and ad_id are JSON integers, as in TikTok's docs, and go above 2^53. A plain JSON.parse in JavaScript rounds 7000000013373458786 to a different number. Read them as strings (as in the example above, or with a big-integer parser) and compare them with the string ids the API returns.

Test it

  1. Subscribe to LEAD with your callback_url (see Forms and webhook).
  2. Generate a lead with accents in the name, to exercise the escaping: npx mimicr action tiktok-ads lead.submit adId=<ad_id> 'fields={"name":"João Araújo"}'.
  3. Check the delivery and the signature Mimicr computed: npx mimicr get '/v1/tiktok-ads/deliveries?entity=LEAD&limit=1' (the signature, body and attempts fields).

If your server answers 401, the delivery goes into retries. Once fixed, resend it with POST /v1/tiktok-ads/deliveries/{id}/resend. When you rotate the Secret (PUT /v1/tiktok-ads/app with rotate_secret: true), the following deliveries use the new Secret.

TikTok account webhooks (organic APIs) use another scheme, Tiktok-Signature: t=…,s=…. They are not part of the simulated core; do not confuse them with X-Open-Signature.