Back to APIs

Automate SMS, WhatsApp, voice, video, and email.

Using the Twilio API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Twilio 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 Twilio

Below are some working code examples of how you can use Twilio 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 { validateRequest } from "twilio";
2
import { TriggerClient } from "@trigger.dev/sdk";
3
4
// Documentation
5
// https://www.twilio.com/docs/usage/webhooks/sms-webhooks
6
// https://www.twilio.com/docs/usage/webhooks/webhooks-security
7
8
// Steps
9
// Get trigger HTTP endpoints set to TWILIO_WEBHOOK_URL
10
// Goto twilio console > Messaging > Try it out > Send a WhatsApp message and connect sandbox
11
// From the reference code, copy the auth token and set to TWILIO_AUTH_TOKEN
12
// Goto Sandbox settings and configure `When a message comes in` to the trigger HTTP endpoint
13
// Send a message to the whatsapp sandbox number
14
15
// Create an HTTP Endpoint, with the twilio.com details
16
const twilio = client.defineHttpEndpoint({
17
id: "twilio.com",
18
source: "twilio.com",
19
icon: "twilio",
20
verify: async (request) => (validateRequest(
21
process.env.TWILIO_AUTH_TOKEN!,
22
request.headers.get("x-twilio-signature")!,
23
process.env.TWILIO_WEBHOOK_URL!,
24
parse(await request.text())
25
) ? {success: true}: {success: false, message: "Invalid signature"}),
26
});
27
28
client.defineJob({
29
id: "http-twilioDotcom",
30
name: "HTTP twilio.com",
31
version: "1.0.0",
32
enabled: true,
33
// Create a trigger from the HTTP endpoint
34
trigger: twilio.onRequest(),
35
run: async (request, io, ctx) => {
36
const body = parse(await request.text());
37
await io.logger.info(`Body`, body);
38
},
39
});