Guides
Leads

Verify the signature

How to check the X-LI-Signature of lead notifications and the challengeResponse of the challenge, with Node and Python examples.

Every lead notification arrives with the X-LI-Signature header: the lowercase hex HMAC-SHA256 of "hmacsha256=" + raw body, keyed with the app's client secret. The header carries only the hex, with no prefix.

text
X-LI-Signature = lowercase_hex( HMAC_SHA256( key = client_secret, message = "hmacsha256=" + rawBody ) )

Compute it over the exact bytes that arrived, before any JSON parsing: re-serializing changes spacing and order and breaks the comparison. Mimicr sends the body exactly as it signed it.

Node

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

const app = express();

app.post("/li/leads", express.raw({ type: "application/json" }), (req, res) => {
  if (req.query.secret !== process.env.WEBHOOK_SECRET) return res.sendStatus(401);
  const expected = createHmac("sha256", process.env.LINKEDIN_CLIENT_SECRET)
    .update("hmacsha256=")
    .update(req.body)
    .digest("hex");
  const got = String(req.get("X-LI-Signature") ?? "");
  const ok = got.length === expected.length && timingSafeEqual(Buffer.from(got), Buffer.from(expected));
  if (!ok) return res.sendStatus(401);
  const event = JSON.parse(req.body.toString("utf8"));
  // dedupe on event.leadGenFormResponse + event.occurredAt, then GET /rest/leadFormResponses/{id}
  res.sendStatus(200);
});

Python

python
import hashlib, hmac, os
from flask import Flask, request, abort

app = Flask(__name__)

@app.post("/li/leads")
def leads():
    if request.args.get("secret") != os.environ["WEBHOOK_SECRET"]:
        abort(401)
    raw = request.get_data()
    expected = hmac.new(os.environ["LINKEDIN_CLIENT_SECRET"].encode(), b"hmacsha256=" + raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, request.headers.get("X-LI-Signature", "")):
        abort(401)
    event = request.get_json()
    return "", 200

Checking an example

bash
BODY='{"type":"LEAD_ACTION","leadGenFormResponse":"urn:li:leadGenFormResponse:4aa37137-8262-48cb-9736-964d6cf642fe-2","leadGenForm":"urn:li:versionedLeadGenForm:(urn:li:leadGenForm:6700000002689218625,1)","owner":{"sponsoredAccount":"urn:li:sponsoredAccount:521119150"},"associatedEntity":{"associatedCreative":"urn:li:sponsoredCreative:166531097"},"leadType":"SPONSORED","leadAction":"CREATED","occurredAt":1793950472864}'
printf 'hmacsha256=%s' "$BODY" | openssl dgst -sha256 -hmac '<client_secret>' -hex

The result must equal the delivery's X-LI-Signature. In Mimicr, each delivery stores the signature and the attempts: GET /v1/linkedin-ads/deliveries/{id} returns signature, body and attempts.

The challenge uses another formula

The challenge's challengeResponse is the hex HMAC-SHA256 of the challengeCode without the hmacsha256= prefix, with the same key:

javascript
const challengeResponse = createHmac("sha256", process.env.LINKEDIN_CLIENT_SECRET).update(challengeCode).digest("hex");

See Lead Sync and webhook.

Common mistakes

  • Using the access token or the client id as the key: the key is the client secret.
  • Forgetting the hmacsha256= prefix on the notification, or adding it to the challenge.
  • Signing the re-serialized JSON instead of the raw body.
  • Comparing upper and lower case: the hex is lowercase.
  • Forgetting that rotating the secret (PUT /v1/linkedin-ads/app with rotate_secret: true) changes the signature of the next deliveries and the challenge.

To test the failure path, chaos on the webhooks area fails the challenge as if the hash were wrong, fails the first attempt or duplicates notifications (see Sandbox control).