Back to APIs

Send messages, images, video with WhatsApp.

Using the WhatsApp API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your WhatsApp background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Use our HTTP endpoint to subscribe to webhooks

Example code using WhatsApp

Below are some working code examples of how you can use WhatsApp with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, verifyRequestSignature } from "@trigger.dev/sdk";
2
3
// WhatsApp
4
const whatsApp = client.defineHttpEndpoint({
5
id: "whatsapp",
6
source: "whatsapp.com",
7
icon: "whatsapp",
8
// This is only needed for certain APIs like WhatsApp which don't setup the webhook until you pass the test
9
respondWith: {
10
// Don't trigger runs if they match this filter
11
skipTriggeringRuns: true,
12
filter: {
13
method: ["GET"],
14
query: {
15
"hub.mode": [{ $startsWith: "sub" }],
16
},
17
},
18
handler: async (request, verify) => {
19
const searchParams = new URL(request.url).searchParams;
20
if (
21
searchParams.get("hub.verify_token") !==
22
process.env.WHATSAPP_WEBHOOK_SECRET
23
) {
24
return new Response("Unauthorized", { status: 401 });
25
}
26
return new Response(searchParams.get("hub.challenge") ?? "OK", {
27
status: 200,
28
});
29
},
30
},
31
verify: async (request) => {
32
return await verifyRequestSignature({
33
request,
34
headerName: "x-hub-signature-256",
35
secret: process.env.WHATSAPP_APP_SECRET!,
36
algorithm: "sha256",
37
xp,
38
});
39
},
40
});
41
42
client.defineJob({
43
id: "http-whatsapp",
44
name: "HTTP WhatsApp",
45
version: "1.1.0",
46
enabled: true,
47
trigger: whatsApp.onRequest(),
48
run: async (request, io, ctx) => {
49
const body = await request.json();
50
await io.logger.info(`Body`, body);
51
},
52
});