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 is a working code example 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.

    import twilio from "twilio";
    import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
    import z from "zod";
    // Initialize the Twilio instance
    // Twilio SDK https://github.com/twilio/twilio-node
    // Your AccountSID and Auth Token from console.twilio.com
    // https://www.twilio.com/docs/sms/quickstart/node
    const twilioClient = twilio(
    process.env.TWILIO_ACCOUNT_SID,
    process.env.TWILIO_AUTH_TOKEN
    );
    client.defineJob({
    id: "twilio-send-message",
    name: "Twilio send message",
    version: "1.0.0",
    trigger: eventTrigger({
    name: "twilio-send-message",
    schema: z.object({
    // 'from' is Your Twilio phone number.
    // Adding 'whatsapp:' before the number will send a WhatsApp message.
    // https://console.twilio.com/us1/develop/sms/try-it-out/whatsapp-learn
    from: z.string(),
    to: z.string(), // The phone number you want to send the message.
    body: z.string(), // The message body
    }),
    }),
    run: async (payload, io, ctx) => {
    const { from, to, body } = payload;
    // Wrap an SDK call in io.runTask so it's resumable and displays in logs
    await io.runTask(
    "Twilio send message",
    async () => {
    await twilioClient.messages.create({ from, to, body });
    },
    // Add metadata to improve how the task displays in the logs
    { name: "Twilio send message", icon: "twilio" }
    );
    },
    });
    ,