When a new user is created, send them a welcome drip campaign from SendGrid.
Integrations
Help and guides
Get started
Run this command in your terminal to create a new project using this template.
When a new user is created, send them a welcome drip campaign from SendGrid.
Integrations
Help and guides
Get started
Run this command in your terminal to create a new project using this template.
This repo contains a customEvent Trigger that will send an example drip email campaign using SendGrid.
import { customEvent, Trigger } from "@trigger.dev/sdk";
import * as sendgrid from "@trigger.dev/sendgrid";
import { z } from "zod";
new Trigger({
id: "sendgrid",
name: "SendGrid",
// Trigger on a custom event, see https://docs.trigger.dev/triggers/custom-events
on: customEvent({
name: "new.user",
// Use zod to verify event payload. See https://docs.trigger.dev/guides/zod
schema: z.object({ id: z.string(), email: z.string(), name: z.string() }),
}),
// The run functions gets called once per "new.user" event
run: async (event, ctx) => {
// Send the initial welcome email. See https://docs.trigger.dev/integrations/apis/sendgrid/actions/mail-send
await sendgrid.mailSend("send-welcome-email", {
from: {
// Your 'from' email needs to be verified in SendGrid. https://docs.sendgrid.com/for-developers/sending-email/sender-identity
email: "john@acme.test",
name: "John from the Acme Corporation",
},
personalizations: [
{
to: [
{
name: event.name,
email: event.email,
},
],
},
],
subject: "Welcome to the Acme Corporation!",
content: [
{
// This can either be text/plain or text/html, text/html in this case
type: "text/html",
value: `<p>Hi ${event.name},</p>
<p>Thanks for signing up to the Acme Corporation. </p>
<p>To get started, we recommend browsing our <a href="https://app.acme.test/templates">templates</a>.</p>
<p>Best,</p>
<p>John</p>
<p>CEO, the Acme Corporation</p>`,
},
],
});
// Wait for 1 hour. See https://docs.trigger.dev/functions/delays
await ctx.waitFor("⏲", { hours: 1 });
// Send the follow up email
await sendgrid.mailSend("send-follow-up-email", {
// Your 'from' email needs to be verified in SendGrid. https://docs.sendgrid.com/for-developers/sending-email/sender-identity
from: {
email: "john@acme.test",
name: "John Doe",
},
personalizations: [
{
to: [
{
name: event.name,
email: event.email,
},
],
},
],
subject: "How are you finding the Acme Corporation?",
content: [
{
// This can either be text/plain or text/html, text/plain in this case
type: "text/plain",
value: `Hi ${event.name},
We hope you're enjoying using our product. If you have any questions, please get in touch!
Best,
John,
CEO, the Acme Corporation`,
},
],
});
},
}).listen();
You can easily create a new project interactively based on this template by running:
npx create-trigger@latest sendgrid-welcome-drip-campaign
# or
yarn create trigger sendgrid-welcome-drip-campaign
# or
pnpm create trigger@latest sendgrid-welcome-drip-campaign
Follow the instructions in the CLI to get up and running locally in <30s.
You can customize the delays between emails by editing the ctx.waitFor
call:
await ctx.waitFor("⏲", { hours: 1 });
After successfully running this template locally, head over to your Trigger.dev Dashboard and you should see your newly created workflow:
Click on the "Test your workflow" button and fill in the JSON needed for this workflow's customEvent Trigger:
After click "Run Test" you'll be redirected to the Run Details page and you should see a prompt for entering your SendGrid API Key:
After hitting "Save" the Run will pickup where it left off and make the request to SendGrid to send your email:
Once you are happy with your campaign you can deploy it live to Render.com (or another hosting service). You can then send custom events that trigger your workflow sendEvent function from the @trigger.dev/sdk
, or simply by making requests to our events
API endpoint.
Here is an example of sending the custom event to trigger the workflow contained in this repo using fetch
:
const eventId = ulid();
const event = {
name: "new.user",
payload: {
id: "user_1234",
email: "eric@trigger.dev",
name: "Eric",
},
};
const response = await fetch("https://app.trigger.dev/api/v1/events", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.TRIGGER_API_KEY}`,
},
body: JSON.stringify({
id: eventId,
event,
}),
});