Back to APIs

Send emails & React emails using Resend's email service.

Using our official Resend integration

Create any tasks possible with the Resend API.

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

Resend integration docs

Example code using Resend

Below are some working code examples of how you can use Resend 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 { SupabaseManagement } from "@trigger.dev/supabase";
2
import { Database } from "./mocks/supabase-types";
3
import { Resend } from "@trigger.dev/resend";
4
import { TriggerClient } from "@trigger.dev/sdk";
5
6
// Use OAuth to authenticate with Supabase Management API
7
const supabaseManagement = new SupabaseManagement({
8
id: "supabase-management",
9
});
10
11
const db = supabaseManagement.db<Database>(
12
// process.env.NEXT_PUBLIC_SUPABASE_URL! // Use if using standard Supabase domain
13
process.env.SUPABASE_REFERENCE_ID! // Use if using a Supabase custom domain
14
);
15
16
const resend = new Resend({
17
id: "resend",
18
apiKey: process.env.RESEND_API_KEY!,
19
});
20
21
// This job triggers when a Supabase user confirms their email address
22
client.defineJob({
23
id: "welcome-email-campaign",
24
name: "Welcome Email Campaign",
25
version: "1.0.0",
26
trigger: db.onUpdated({
27
// This job triggers when there is an update in the 'users' table of the 'auth' schema.
28
// Specifically, it watches for a change in the 'email_confirmed_at' field from null (unconfirmed email)
29
// to a timestamp (confirmed email).
30
schema: "auth",
31
table: "users",
32
filter: {
33
old_record: {
34
email_confirmed_at: [{ $isNull: true }],
35
},
36
record: {
37
email_confirmed_at: [{ $isNull: false }],
38
},
39
},
40
}),
41
integrations: {
42
resend,
43
},
44
run: async (payload, io, ctx) => {
45
if (!payload.record.email) {
46
return;
47
}
48
49
const isTestOrDev =
50
ctx.run.isTest || ctx.environment.type === "DEVELOPMENT";
51
52
// Only wait for 10 seconds when running as a test or in the development environment
53
await io.wait("wait-1", isTestOrDev ? 10 : 60 * 60); // 1 hour
54
55
const email1 = await io.resend.sendEmail("email-1", {
56
to: payload.record.email,
57
subject: `Thanks for joining Acme Inc`,
58
text: `Hi there, welcome to our community! This is the first email we send you to help you get started.`,
59
from: process.env.RESEND_FROM_EMAIL!,
60
});
61
62
await io.wait("wait-2", isTestOrDev ? 10 : 60 * 60 * 12); // 12 hours
63
64
const email2 = await io.resend.sendEmail("email-2", {
65
to: payload.record.email,
66
subject: `Here are some tips to get started`,
67
text: `Hi there, welcome to our community! This is the second email we send you to help you get started.`,
68
from: process.env.RESEND_FROM_EMAIL!,
69
});
70
71
await io.wait("wait-3", isTestOrDev ? 10 : 60 * 60 * 24); // 24 hours
72
73
const email3 = await io.resend.sendEmail("email-3", {
74
to: payload.record.email,
75
subject: "Do you have any questions?",
76
text: `Hi there, welcome to our community! This is the third email we send you to help you get started.`,
77
from: process.env.RESEND_FROM_EMAIL!,
78
});
79
80
return {
81
email1,
82
email2,
83
email3,
84
};
85
},
86
});