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.

    import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
    import { google } from "googleapis";
    import { JWT } from "google-auth-library";
    import z from "zod";
    // Create a service account and project: https://cloud.google.com/iam/docs/service-account-overview
    // Create a JWT (JSON Web Token) authentication instance for Google APIs.
    // https://cloud.google.com/nodejs/docs/reference/google-auth-library/latest/google-auth-library/jwt
    const auth = new JWT({
    email: process.env.GOOGLE_CLIENT_EMAIL, // The email associated with the service account
    key: process.env.GOOGLE_PRIVATE_KEY!.split(String.raw`\n`).join("\n"), // The private key associated with the service account
    scopes: "https://www.googleapis.com/auth/drive", // The desired scope for accessing Google Drive
    });
    // Initialize the Google Drive API
    // You have to enable the Google Drive API https://console.cloud.google.com/apis/
    const drive = google.drive({ version: "v2", auth });
    client.defineJob({
    id: "google-drive-file-rename",
    name: "Google drive file rename",
    version: "1.0.0",
    trigger: eventTrigger({
    name: "google-drive-file-rename",
    schema: z.object({
    // The fileId is a unique identifier found in the Google Drive sharing link.
    // E.g.: https://drive.google.com/file/d/FILE_ID/view
    fileId: z.string(),
    newName: z.string(),
    }),
    }),
    run: async (payload, io, ctx) => {
    const { fileId, newName } = payload;
    // Wrap an SDK call in io.runTask so it's resumable and displays in logs
    await io.runTask(
    "Google Drive File Rename",
    async () => {
    // NB: You must share the Google Drive file with the service account.
    await drive.files.update({
    fileId,
    requestBody: {
    title: newName,
    },
    });
    },
    // Add metadata to improve how the task displays in the logs
    { name: "Google drive file rename", icon: "google" }
    );
    },
    });
    ,