The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import * as z from "zod/v4";
import { clickEventSchema } from "../zod/schemas/clicks";
import { CustomerSchema } from "../zod/schemas/customers";
import { linkEventSchema } from "../zod/schemas/links";
import { WEBHOOK_TRIGGERS } from "./constants";
const webhookSaleSchema = z.object({
amount: z.number(),
currency: z.string(),
paymentProcessor: z.string(),
invoiceId: z.string().nullable(),
});
export const clickWebhookEventSchema = z.object({
click: clickEventSchema,
link: linkEventSchema,
});
const coerceJsonString = (val: unknown) => {
if (typeof val === "string") {
try {
return JSON.parse(val);
} catch {
return val;
}
}
return val;
};
const metadataSchema = z.preprocess(
coerceJsonString,
z.record(z.string(), z.any()).nullish().default(null),
);
export const leadWebhookEventSchema = z.object({
eventName: z.string(),
customer: CustomerSchema,
click: clickEventSchema,
link: linkEventSchema,
metadata: metadataSchema,
});
export const saleWebhookEventSchema = z.object({
eventName: z.string(),
customer: CustomerSchema,
click: clickEventSchema,
link: linkEventSchema,
sale: webhookSaleSchema,
metadata: metadataSchema,
});
// Schema of the payload sent to the webhook endpoint by Dub
export const webhookPayloadSchema = z.object({
id: z.string().describe("Unique identifier for the event."),
event: z
.enum(WEBHOOK_TRIGGERS)
.describe("The type of event that triggered the webhook."),
createdAt: z
.string()
.describe("The date and time when the event was created in UTC."),
data: z.any().describe("The data associated with the event."),
});
// Exported for the OpenAPI spec
export const webhookEventSchema = z
.union([
z
.object({
id: z.string(),
event: z.union([
z.literal("link.created"),
z.literal("link.updated"),
z.literal("link.deleted"),
]),
createdAt: z.string(),
data: linkEventSchema,
})
.meta({
description: "Triggered when a link is created, updated, or deleted.",
id: "LinkWebhookEvent",
outputId: "LinkWebhookEvent",
}),
z
.object({
id: z.string(),
event: z.literal("link.clicked"),
createdAt: z.string(),
data: clickWebhookEventSchema,
})
.meta({
description: "Triggered when a link is clicked.",
id: "LinkClickedEvent",
outputId: "LinkClickedEvent",
}),
z
.object({
id: z.string(),
event: z.literal("lead.created"),
createdAt: z.string(),
data: leadWebhookEventSchema,
})
.meta({
description: "Triggered when a lead is created.",
id: "LeadCreatedEvent",
outputId: "LeadCreatedEvent",
}),
z
.object({
id: z.string(),
event: z.literal("sale.created"),
createdAt: z.string(),
data: saleWebhookEventSchema,
})
.meta({
description: "Triggered when a sale is created.",
id: "SaleCreatedEvent",
outputId: "SaleCreatedEvent",
}),
])
.meta({
description: "Webhook event schema",
"x-speakeasy-include": true,
id: "WebhookEvent",
});