Skip to content

Holidays without missed messages: free reconnection

Pick a plan
n8n

n8n

n8n workflows with signature verification and authenticated calls.

Receive events

  1. Add a Webhook node, method POST, and enable Raw Body under Options. Copy the Production URL.
  2. In aichat.md, Settings → Developers → Add webhook with the n8n URL and the events you need.
  3. Add a Code node that verifies the signature (below), then the rest of the workflow.
// Webhook node: Options → Raw Body = on. Needs NODE_FUNCTION_ALLOW_BUILTIN=crypto.
const crypto = require('crypto');
const secret = $env.AICHAT_WEBHOOK_SECRET;
const item = $input.first();
const raw = Buffer.from(item.binary.data.data, 'base64').toString('utf8');
const header = item.json.headers['aichat-signature'] || '';
const t = Number((header.match(/t=(\d+)/) || [])[1]);
const expected = crypto.createHmac('sha256', secret).update(`${t}.${raw}`).digest('hex');
const ok = Math.abs(Date.now() / 1000 - t) <= 300 && header.split(',').some((p) => p === `v1=${expected}`);
if (!ok) throw new Error('Invalid aichat.md signature');
return [{ json: JSON.parse(raw) }];

On self-hosted n8n the crypto module must be allowed: NODE_FUNCTION_ALLOW_BUILTIN=crypto. Keep the whsec_… secret in the AICHAT_WEBHOOK_SECRET environment variable.

Calling the API

Create a Header Auth credential named Authorization with the value Bearer aik_live_…, then use the HTTP Request node with it. For lists, enable n8n pagination: cursor from {{$response.body.next_cursor}}, stop when has_more is false.

curl -X PATCH https://aichat.md/api/public/v1/leads/lead_MTEy \
  -H "Authorization: Bearer $AICHAT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"status": "qualified", "external_id": "crm-4821", "metadata": {"owner": "Vlad"}}'
NextAPI reference