How it works
Gmail uses Google Cloud Pub/Sub to deliver push notifications. The flow is:- You create a Pub/Sub topic and subscription pointing to your Nango webhook URL
- You call Gmail’s
watchendpoint for each connection to start receiving notifications - When a user’s mailbox changes, Google publishes a notification to your topic
- The Pub/Sub subscription pushes it to Nango
- Nango matches the notification to the correct connection and forwards it to your app
emailAddress from the notification payload. For new connections, Nango persists only a hash of the user’s email (connection_config.emailAddressHash) automatically at connection time — no manual setup required.
Setup
1. Create a Pub/Sub topic
Create a topic in the Google Cloud Console. You can use the default options Google sets. See Google’s documentation for details.2. Grant Gmail publish rights
Gmail needs permission to publish to your topic. You can do this in the Google Cloud Console or with the gcloud CLI. Option A — Google Cloud Console:- Open Pub/Sub Topics in the Google Cloud Console.
- Click on your topic, then go to the Permissions tab.
- Click Add principal. Set the principal to
gmail-api-push@system.gserviceaccount.comand assign the Pub/Sub Publisher role.
The topic ID is the short name (e.g.
my-gmail-topic), while the topic name is the fully qualified path (e.g. projects/my-project/topics/my-gmail-topic). You’ll need the topic name later when calling the watch endpoint.3. Create a push subscription
Click on the default subscription for your topic (if one was created) or create a new one. Select Edit and configure it as follows:- Delivery type: Set as push
- Endpoint URL: Copy the webhook URL from your Gmail integration page in the Nango dashboard, under the Webhook URL section.
- Authentication (recommended): Enable authentication and configure a service account for added security.
- Save the changes
4. Activate the watch subscription
Call Gmail’susers.watch endpoint for each connection you want to receive notifications for:
5. Renew the watch subscription daily
Google recommends calling thewatch endpoint at least once a day to keep the subscription active. Each call replaces any existing watch for the user, so no separate cleanup is needed. You can use a Nango sync with a 1d frequency for this:
6. Stop the watch subscription on connection deletion
If a connection is deleted in Nango but the Gmail watch remains active, Google may continue sending notifications for that mailbox until the watch expires. To stop notifications immediately for a deleted connection, call Gmail’susers.stop endpoint before deletion.
You can automate this with a pre-connection-deletion lifecycle event:
7. Handle forwarded webhooks
When a Gmail notification arrives, Nango matches it to the correct connection and forwards it to your system. The forwarded payload looks like this:message.data field is base64-encoded and contains:
1d/1h) just to act as a safety net for any missed webhooks.
If you prefer Nango to automatically run a sync when the webhook arrives (instead of forwarding it to your app), you can enable webhook processing in a sync script using
webhookSubscriptions and onWebhook. For Gmail, subscribe to '*' and decode payload.message.data to get historyId.See: Real-time syncsConnection matching
Nango uses theemailAddress from the webhook payload to find the matching connection (by hashing it internally). The lookup order is:
connection_config.emailAddressHash— automatically set by Nango for new connections (created after 2026-03-11)metadata.emailAddress— fallback (customer-controlled)metadata.email— fallback (customer-controlled)
- Re-authorize the connection so Nango’s post-connection hook runs and persists the email hash automatically
- Set the metadata manually via the API: