Back to APIs

Google Drive

drive.google.com

Manage your files and folders in Google Drive.

Using the Google Drive API with Trigger.dev

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

Below are some working code examples of how you can use Google Drive 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
// Create a service account and project: https://cloud.google.com/iam/docs/service-account-overview
7
// Create a JWT (JSON Web Token) authentication instance for Google APIs.
8
// https://cloud.google.com/nodejs/docs/reference/google-auth-library/latest/google-auth-library/jwt
9
const auth = new JWT({
10
email: process.env.GOOGLE_CLIENT_EMAIL, // The email associated with the service account
11
key: process.env.GOOGLE_PRIVATE_KEY!.split(String.raw`\n`).join("\n"), // The private key associated with the service account
12
scopes: "https://www.googleapis.com/auth/drive", // The desired scope for accessing Google Drive
13
});
14
15
// Initialize the Google Drive API
16
// You have to enable the Google Drive API https://console.cloud.google.com/apis/
17
const drive = google.drive({ version: "v2", auth });
18
19
client.defineJob({
20
id: "google-drive-file-rename",
21
name: "Google drive file rename",
22
version: "1.0.0",
23
trigger: eventTrigger({
24
name: "google-drive-file-rename",
25
schema: z.object({
26
// The fileId is a unique identifier found in the Google Drive sharing link.
27
// E.g.: https://drive.google.com/file/d/FILE_ID/view
28
fileId: z.string(),
29
newName: z.string(),
30
}),
31
}),
32
run: async (payload, io, ctx) => {
33
const { fileId, newName } = payload;
34
35
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
36
await io.runTask(
37
"Google Drive File Rename",
38
async () => {
39
// NB: You must share the Google Drive file with the service account.
40
await drive.files.update({
41
fileId,
42
requestBody: {
43
title: newName,
44
},
45
});
46
},
47
48
// Add metadata to improve how the task displays in the logs
49
{ name: "Google drive file rename", icon: "google" }
50
);
51
},
52
});