Back to APIs

Google Docs

docs.google.com

Create, update and delete documents in Google Docs.

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

Below is a working code example of how you can use Google Docs 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/document
7
// Open a document 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 Docs 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, by default it is set to On open
40
// Click Save
41
42
// Set the GOOGLE_DOCS_WEBHOOK_SECRET (Secret) in the .env file.
43
44
// Create an HTTP Endpoint, with the Google Docs details
45
const docs = client.defineHttpEndpoint({
46
id: "google-docs",
47
source: "google-docs",
48
icon: "googledocs",
49
verify: async (request) => {
50
const secret = process.env.GOOGLE_DOCS_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-docs",
60
name: "HTTP Google Docs",
61
version: "1.0.0",
62
enabled: true,
63
// Create a trigger from the HTTP endpoint
64
trigger: docs.onRequest(),
65
run: async (request, io, ctx) => {
66
const body = await request.json();
67
await io.logger.info(`Body`, body);
68
},
69
});