Skip to main content

Method Signature

align.webhooks.create(data: CreateWebhookRequest): Promise<Webhook>

Parameters

url
string
required
HTTPS endpoint URL to receive webhook events.
Webhooks subscribe to all supported events by default.

Example

import Align from "@tolbel/align";

const align = new Align({
  apiKey: process.env.ALIGN_API_KEY!,
  environment: "sandbox",
});

const webhook = await align.webhooks.create({
  url: "https://your-app.com/webhooks/align",
});

console.log(`Webhook ID: ${webhook.id}`);

Webhook Handler Example

Your webhook signature is signed using your API Key.
import express from "express";
import crypto from "crypto";

const app = express();

const API_KEY = process.env.ALIGN_API_KEY!;

app.post(
  "/webhooks/align",
  express.raw({ type: "application/json" }),
  (req, res) => {
    const signature = req.headers["x-hmac-signature"] as string;
    const payload = req.body.toString("utf8"); // Raw body

    // Verify signature using your API KEY
    const hmac = crypto.createHmac("sha256", API_KEY);
    const expectedSignature = hmac.update(payload).digest("hex");

    if (signature !== expectedSignature) {
      return res.status(401).send("Invalid signature");
    }

    const event = JSON.parse(payload);
    console.log("Received event:", event.event_type);

    res.status(200).send("OK");
  }
);