Back to APIs

Send, delete and filter emails and more.

Using our official SendGrid integration

Create any tasks possible with the SendGrid API.

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

SendGrid integration docs

Example code using SendGrid

Below are some working code examples of how you can use SendGrid 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, cronTrigger } from "@trigger.dev/sdk";
2
import { SendGrid } from "@trigger.dev/sendgrid";
3
import { Slack } from "@trigger.dev/slack";
4
import { weeklySummaryDb } from "./mocks/db";
5
import { weeklySummaryEmail } from "./mocks/emails";
6
7
const sendgrid = new SendGrid({
8
id: "sendgrid",
9
apiKey: process.env.SENDGRID_API_KEY!,
10
});
11
12
const slack = new Slack({ id: "slack" });
13
14
// This Job sends a weekly summary email to users who have
15
// summariesEnabled = true, and then posts the total numbers to Slack.
16
client.defineJob({
17
id: "weekly-user-activity-summary",
18
name: "Weekly user activity summary",
19
version: "1.0.0",
20
integrations: { sendgrid, slack },
21
trigger: cronTrigger({
22
// Send every Friday at 4pm
23
cron: "0 16 * * 5",
24
}),
25
run: async (payload, io, ctx) => {
26
const users = await weeklySummaryDb.getUsers();
27
28
let sentCount = 0;
29
let notSentCount = 0;
30
31
for (const user of users) {
32
if (user.summariesEnabled) {
33
await io.sendgrid.sendEmail(`Weekly summary for ${user.id}`, {
34
to: user.email,
35
// The 'from' email must be a verified domain in your SendGrid account.
36
37
subject: "Your weekly summary",
38
html: weeklySummaryEmail(user),
39
});
40
sentCount++;
41
} else {
42
notSentCount++;
43
}
44
}
45
46
await io.slack.postMessage("Notify team", {
47
text: `Weekly summary sent to ${sentCount} users and not sent to ${notSentCount} users`,
48
// This has to be a channel ID, not a channel name
49
channel: "YOUR_CHANNEL_ID",
50
});
51
},
52
});