Stay organized with collections
Save and categorize content based on your preferences.
The Advanced Google Workspace Events service lets you use the
Google Workspace Events API in
Apps Script. This API lets you subscribe to
Google Workspace resources so that you receive relevant events that you're
interested in. Events represent changes to resources, such as when resources are
created, updated, or deleted.
Prerequisites
An Apps Script project using a standard Google Cloud project
instead of the default one created automatically by Apps Script.
To subscribe to Chat events, you must have a
Google Chat app configured on the Chat API configuration
page in the Google Cloud console. To create a Google Chat app, see
Build a Google Chat app with Apps Script.
The necessary authorization scopes added to the Apps Script
project's appsscript.json file. The necessary scopes depend on the types of
the subscriptions' target resources and events. For details, see
Choose Google Workspace Events API scopes.
For example:
For more information about this service, see the
Google Workspace Events API reference documentation.
Like all advanced services in Apps Script, the
Google Workspace Events service uses the same objects, methods, and
parameters as the public API.
Sample code
These samples show you how to perform common
Google Workspace Events API
actions using the advanced service.
Create a subscription
To create a subscription to a Google Workspace resource, add
the following function to the Apps Script project's code:
/** * Creates a subscription to receive events about a Google Workspace resource. * For a list of supported resources and event types, see the * [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events). * For additional information, see the * [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create) * method reference. * @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to. * @param {!string|!Array<string>} eventTypes The types of events to receive about the resource. * @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription. */functioncreateSubscription(targetResource,eventTypes,pubsubTopic){try{constoperation=WorkspaceEvents.Subscriptions.create({targetResource:targetResource,eventTypes:eventTypes,notificationEndpoint:{pubsubTopic:pubsubTopic,},});console.log(operation);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failed to create subscription with error %s',err.message);}}
List subscriptions
To list subscriptions filtered by event types and target resource,
add the following function to the Apps Script project's code:
/** * Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource. * For additional information, see the * [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list) * method reference. * @param {!string} filter The query filter. */functionlistSubscriptions(filter){try{constresponse=WorkspaceEvents.Subscriptions.list({filter});console.log(response);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failed to list subscriptions with error %s',err.message);}}
Get subscription
To get information about a subscription, add the following function to
the Apps Script project's code:
/** * Gets details about a subscription. * For additional information, see the * [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get) * method reference. * @param {!string} name The resource name of the subscription. */functiongetSubscription(name){try{constsubscription=WorkspaceEvents.Subscriptions.get(name);console.log(subscription);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failed to get subscription with error %s',err.message);}}
Update subscription
To update or renew a subscription, add the following function to the
Apps Script project's code:
/** * Updates an existing subscription. * This can be used to renew a subscription that is about to expire. * For additional information, see the * [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch) * method reference. * @param {!string} name The resource name of the subscription. */functionpatchSubscription(name){try{constoperation=WorkspaceEvents.Subscriptions.patch({// Setting the TTL to 0 seconds extends the subscription to its maximum expiration time.ttl:'0s',},name);console.log(operation);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failed to update subscription with error %s',err.message);}}
Reactivate subscription
To reactivate a subscription, add the following function to the
Apps Script project's code:
/** * Reactivates a suspended subscription. * Before reactivating, you must resolve any errors with the subscription. * For additional information, see the * [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate) * method reference. * @param {!string} name The resource name of the subscription. */functionreactivateSubscription(name){try{constoperation=WorkspaceEvents.Subscriptions.reactivate({},name);console.log(operation);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failed to reactivate subscription with error %s',err.message);}}
Delete subscription
To delete a subscription, add the following function to the
Apps Script project's code:
/** * Deletes a subscription. * For additional information, see the * [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete) * method reference. * @param {!string} name The resource name of the subscription. */functiondeleteSubscription(name){try{constoperation=WorkspaceEvents.Subscriptions.remove(name);console.log(operation);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failed to delete subscription with error %s',err.message);}}
Get operation
Most Google Workspace Events API methods return a
long-running operation.
To determine the status of the operation, you can use the
operations.get()
method.
To get information about an operation, add the following function
to the Apps Script project's code:
/** * Gets details about an operation returned by one of the methods on the subscription * resource of the Google Workspace Events API. * For additional information, see the * [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get) * method reference. * @param {!string} name The resource name of the operation. */functiongetOperation(name){try{constoperation=WorkspaceEvents.Operations.get(name);console.log(operation);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failed to get operation with error %s',err.message);}}
To get the name of an operation, use the value from the name field returned
from one of the Google Workspace Events API methods, such as
subscriptions.create() or
subscriptions.patch().
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-18 UTC."],[[["\u003cp\u003eThe Advanced Google Workspace Events service enables you to use the Google Workspace Events API in Apps Script to subscribe to Google Workspace resources and receive relevant events.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize this service, you need an Apps Script project linked to a standard Google Cloud project, a Pub/Sub topic for receiving events, and specific authorization scopes in your project's \u003ccode\u003eappsscript.json\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe service offers functionality to create, list, get, update, reactivate, and delete subscriptions, as well as retrieve information about long-running operations through the provided sample code snippets.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Workspace Events represent changes to resources such as creation, updates, or deletions, providing insights into resource activities within your Workspace environment.\u003c/p\u003e\n"]]],[],null,["The Advanced Google Workspace Events service lets you use the\n[Google Workspace Events API](/workspace/events) in\nApps Script. This API lets you subscribe to\nGoogle Workspace resources so that you receive relevant events that you're\ninterested in. Events represent changes to resources, such as when resources are\ncreated, updated, or deleted.\n\nPrerequisites\n\n- An Apps Script project using a standard Google Cloud project instead of the default one created automatically by Apps Script.\n- A [Pub/Sub](https://cloud.google.com/pubsub/docs/) topic created in the same Google Cloud project to receive subscription events. To create a Pub/Sub topic, see [Create and subscribe to a Pub/Sub topic](/workspace/events/guides/create-subscription#pubsub).\n- To subscribe to Chat events, you must have a Google Chat app configured on the Chat API configuration page in the Google Cloud console. To create a Google Chat app, see [Build a Google Chat app with Apps Script](/apps-script/quickstart/chat-app).\n- The necessary authorization scopes added to the Apps Script\n project's `appsscript.json` file. The necessary scopes depend on the types of\n the subscriptions' target resources and events. For details, see\n [Choose Google Workspace Events API scopes](/workspace/events/guides/auth).\n For example:\n\n \"oauthScopes\": [\n \"https://www.googleapis.com/auth/chat.messages.readonly\"\n ]\n\n| **Note:** This is an advanced service that you must [turn on before use](/apps-script/guides/services/advanced).\n\nReference\n\nFor more information about this service, see the\n[Google Workspace Events API reference documentation](/workspace/events/reference/rest/v1).\nLike all advanced services in Apps Script, the\nGoogle Workspace Events service uses the same objects, methods, and\nparameters as the public API.\n\nSample code\n\nThese samples show you how to perform common\n[Google Workspace Events API](/workspace/events)\nactions using the advanced service.\n\nCreate a subscription\n\nTo create a subscription to a Google Workspace resource, add\nthe following function to the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```gosu\n/**\n * Creates a subscription to receive events about a Google Workspace resource.\n * For a list of supported resources and event types, see the\n * [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events).\n * For additional information, see the\n * [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create)\n * method reference.\n * @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to.\n * @param {!string|!Array\u003cstring\u003e} eventTypes The types of events to receive about the resource.\n * @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription.\n */\nfunction createSubscription(targetResource, eventTypes, pubsubTopic) {\n try {\n const operation = WorkspaceEvents.Subscriptions.create({\n targetResource: targetResource,\n eventTypes: eventTypes,\n notificationEndpoint: {\n pubsubTopic: pubsubTopic,\n },\n });\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to create subscription with error %s', err.message);\n }\n}\n```\n\nList subscriptions\n\nTo list subscriptions filtered by event types and target resource,\nadd the following function to the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```gosu\n/**\n * Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource.\n * For additional information, see the\n * [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list)\n * method reference.\n * @param {!string} filter The query filter.\n */\nfunction listSubscriptions(filter) {\n try {\n const response = WorkspaceEvents.Subscriptions.list({ filter });\n console.log(response);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to list subscriptions with error %s', err.message);\n }\n}\n```\n\nGet subscription\n\nTo get information about a subscription, add the following function to\nthe Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```gosu\n/**\n * Gets details about a subscription.\n * For additional information, see the\n * [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction getSubscription(name) {\n try {\n const subscription = WorkspaceEvents.Subscriptions.get(name);\n console.log(subscription);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get subscription with error %s', err.message);\n }\n}\n```\n\nUpdate subscription\n\nTo update or renew a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```gosu\n/**\n * Updates an existing subscription.\n * This can be used to renew a subscription that is about to expire.\n * For additional information, see the\n * [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction patchSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.patch({\n // Setting the TTL to 0 seconds extends the subscription to its maximum expiration time.\n ttl: '0s',\n }, name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to update subscription with error %s', err.message);\n }\n}\n```\n\nReactivate subscription\n\nTo reactivate a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```gosu\n/**\n * Reactivates a suspended subscription.\n * Before reactivating, you must resolve any errors with the subscription.\n * For additional information, see the\n * [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction reactivateSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.reactivate({}, name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to reactivate subscription with error %s', err.message);\n }\n}\n```\n\nDelete subscription\n\nTo delete a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```gosu\n/**\n * Deletes a subscription.\n * For additional information, see the\n * [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction deleteSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.remove(name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to delete subscription with error %s', err.message);\n }\n}\n```\n\nGet operation\n\nMost Google Workspace Events API methods return a\n[long-running operation](/workspace/events/reference/rest/v1/operations).\nTo determine the status of the operation, you can use the\n[`operations.get()`](/workspace/events/reference/rest/v1/operations/get)\nmethod.\n\nTo get information about an operation, add the following function\nto the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```gosu\n/**\n * Gets details about an operation returned by one of the methods on the subscription\n * resource of the Google Workspace Events API.\n * For additional information, see the\n * [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get)\n * method reference.\n * @param {!string} name The resource name of the operation.\n */\nfunction getOperation(name) {\n try {\n const operation = WorkspaceEvents.Operations.get(name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get operation with error %s', err.message);\n }\n}\n```\n\nTo get the name of an operation, use the value from the `name` field returned\nfrom one of the Google Workspace Events API methods, such as\n[`subscriptions.create()`](#create-subscription) or\n[`subscriptions.patch()`](#update-subscription)."]]