Back to APIs

Automate sending and receiving emails, and mailbox management.

Using the Gmail API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Gmail 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 Gmail

Below are some working code examples of how you can use Gmail 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, eventTrigger } from "@trigger.dev/sdk";
2
import { google } from "googleapis";
3
import { JWT } from "google-auth-library";
4
import z from "zod";
5
6
// NB: This job only works if you have a Google Workspace account
7
8
// Create a service account and project: https://cloud.google.com/iam/docs/service-account-overview
9
// Create a JWT (JSON Web Token) authentication instance for Google APIs.
10
// https://cloud.google.com/nodejs/docs/reference/google-auth-library/latest/google-auth-library/jwt
11
const auth = new JWT({
12
email: process.env.GOOGLE_CLIENT_EMAIL, // The email associated with the service account
13
key: process.env.GOOGLE_PRIVATE_KEY!.split(String.raw`\n`).join("\n"), // The service account private key
14
scopes: ["https://www.googleapis.com/auth/gmail.send"], // The desired scope for sending Gmail emails
15
});
16
17
// In order to send an email from a user's account, you must enable Gmail API Domain-Wide Delegation of Authority:
18
// 1. Google Admin Console: Go to your Google Admin Console.
19
// 2. Security Settings: Navigate to Security > API controls.
20
// 3. Manage Domain-Wide Delegation: In the "Domain wide delegation" panel, click on "Manage Domain-Wide Delegation".
21
// 4. Add Service Account: Click on "Add new" and provide:
22
// 4a. Client ID: This is the Service Account's Client ID, which can be found in your Google Cloud Console under the Service Account's details.
23
// 4b. OAuth Scopes: Enter https://www.googleapis.com/auth/gmail.send to grant send permissions via Gmail.
24
// 5. Click on "Authorize".
25
26
// Replace with the email of the user you're impersonating (the user that will send the email)
27
auth.subject = process.env.GOOGLE_IMPERSONATION_EMAIL;
28
29
// Initialize the Gmail API
30
const gmail = google.gmail({ version: "v1", auth });
31
32
client.defineJob({
33
id: "send-gmail-email",
34
name: "Send an email from Gmail",
35
version: "1.0.0",
36
trigger: eventTrigger({
37
name: "send-gmail-email",
38
schema: z.object({
39
to: z.string(),
40
subject: z.string(),
41
message: z.string(),
42
}),
43
}),
44
run: async (payload, io, ctx) => {
45
const { to, subject, message } = payload;
46
47
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
48
await io.runTask(
49
"Send Gmail",
50
async () => {
51
// Create the email message
52
const email = `To: ${to}\r\nSubject: ${subject}\r\n\r\n${message}`;
53
// Send the email
54
const res = await gmail.users.messages.send({
55
userId: "me",
56
requestBody: {
57
raw: Buffer.from(email).toString("base64"),
58
},
59
});
60
61
console.log("Message sent: ", res.data);
62
},
63
64
// Add metadata to improve how the task displays in the logs
65
{ name: "Send Gmail", icon: "google" }
66
);
67
},
68
});