Back to APIs

Google Sheets

sheets.google.com

Read from and write to spreadsheets programmatically.

Using the Google Sheets 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 Sheets 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 Sheets

Below are some working code examples of how you can use Google Sheets 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 } from "@trigger.dev/sdk";
2
3
// Apps Script Docs: https://developers.google.com/apps-script/reference
4
// Triggers Docs: https://developers.google.com/apps-script/guides/triggers
5
6
// Go to https://docs.google.com/spreadsheets
7
// Open a spreadsheet or create a new one
8
// Click on Extensions > Apps Script
9
10
// Add the following code to the script editor:
11
12
// function pushNotification(e) {
13
// // get endpoint url and secret key from trigger.dev dashboard
14
// var url = ''
15
// var secretKey = ''
16
17
// var payloadData = {
18
// 'message': 'Google Sheets event',
19
// 'details': e // This contains information about the edit event
20
// };
21
22
// var payload = JSON.stringify(payloadData);
23
24
// var options = {
25
// 'method': 'post',
26
// 'contentType': 'application/json',
27
// 'headers': {
28
// 'x-webhook-secret': secretKey
29
// },
30
// 'payload': payload
31
// };
32
33
// UrlFetchApp.fetch(url, options);
34
// }
35
36
// Save the script and click on the clock icon (Triggers) in the sidebar to open the Triggers page
37
// Click on Add Trigger, which will open a dialog box
38
// Choose pushNotification as the function to run
39
// Select event type, e.g. On open, On edit, On change etc.
40
// Click Save
41
42
// Set the GOOGLE_SHEETS_WEBHOOK_SECRET (Secret) in the .env file.
43
44
// Create an HTTP Endpoint, with the Google Sheets details
45
const sheets = client.defineHttpEndpoint({
46
id: "google-sheets",
47
source: "sheets.google.com",
48
icon: "googlesheets",
49
verify: async (request) => {
50
const secret = process.env.GOOGLE_SHEETS_WEBHOOK_SECRET;
51
if (!secret) return { success: false, reason: "Missing Secret" };
52
if (request.headers.get("x-webhook-secret") === secret)
53
return { success: true };
54
return { success: false, reason: "Webhook Secret Match Failed" };
55
},
56
});
57
58
client.defineJob({
59
id: "http-google-sheets",
60
name: "HTTP Google Sheets",
61
version: "1.0.0",
62
enabled: true,
63
// Create a trigger from the HTTP endpoint
64
trigger: sheets.onRequest(),
65
run: async (request, io, ctx) => {
66
const body = await request.json();
67
await io.logger.info(`Body`, body);
68
},
69
});