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 twilio from "twilio";
2
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
3
import z from "zod";
4
5
6
7
// Initialize the Twilio instance
8
// Twilio SDK https://github.com/twilio/twilio-node
9
// Your AccountSID and Auth Token from console.twilio.com
10
// https://www.twilio.com/docs/sms/quickstart/node
11
const twilioClient = twilio(
12
process.env.TWILIO_ACCOUNT_SID,
13
process.env.TWILIO_AUTH_TOKEN
14
);
15
16
client.defineJob({
17
id: "twilio-send-message",
18
name: "Twilio send message",
19
version: "1.0.0",
20
trigger: eventTrigger({
21
name: "twilio-send-message",
22
schema: z.object({
23
// 'from' is Your Twilio phone number.
24
// Adding 'whatsapp:' before the number will send a WhatsApp message.
25
// https://console.twilio.com/us1/develop/sms/try-it-out/whatsapp-learn
26
from: z.string(),
27
to: z.string(), // The phone number you want to send the message.
28
body: z.string(), // The message body
29
}),
30
}),
31
run: async (payload, io, ctx) => {
32
const { from, to, body } = payload;
33
34
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
35
await io.runTask(
36
"Twilio send message",
37
async () => {
38
await twilioClient.messages.create({ from, to, body });
39
},
40
41
// Add metadata to improve how the task displays in the logs
42
{ name: "Twilio send message", icon: "twilio" }
43
);
44
},
45
});