Skip to main content

How it works

  1. You create a Microsoft Graph subscription for each resource you want to watch, for example me/messages and me/events, with your Nango webhook URL as the notificationUrl.
  2. Graph sends a POST request to that URL when the resource changes. Every notification carries the subscription’s clientState and subscriptionId.
  3. Nango drops notifications whose clientState does not match the integration’s webhook secret, and removes clientState before routing.
  4. Nango routes each notification to the connection whose metadata.subscriptionIds contains its subscriptionId, using changeType (or lifecycleEvent) as the webhook type.
One Microsoft integration has one webhook URL. Mail, calendar, and other Graph subscriptions all use it, and are told apart by their subscriptionId.

Graph-based integrations

The same setup works for integrations built on Microsoft Graph whose resources support change notifications. Follow the steps below on that integration, and in step 3 use its resource, change types, and permission: Drive items, including Word, Excel, and PowerPoint files, only support updated, and a notification says that something changed in the drive, not which file. Use delta to fetch the changed files, for example from a sync triggered by the webhook. Outlook has its own webhooks guide, and Microsoft Teams verifies requests differently.

Setup

1. Get your Nango webhook URL

In the Nango dashboard, open your Microsoft integration and copy the Webhook URL.

2. Set the webhook secret in Nango

On the same page, set a Webhook secret, for example generated with openssl rand -hex 32. Nango requires it and rejects every notification while it is empty. You pass the same value as clientState in the next step.

3. Subscribe when a connection is created

Create one Graph subscription per resource you want to watch, and store each subscription id in the connection’s metadata.subscriptionIds. The examples watch me/messages. Repeat with me/events for calendar events, and give the connection the matching read permission, for example Mail.Read or Calendars.Read. Graph validates the URL with a validationToken request when you create a subscription, and Nango answers it automatically.
Subscribe new connections with a post-connection-creation function:
Setting subscriptionIds replaces the whole array. Include every active subscription ID for the connection, or notifications for the missing ones stop matching it.

4. Renew the subscriptions

Graph subscriptions expire, and Graph doesn’t renew them. Check the maximum expirationDateTime per resource in Microsoft’s docs, and extend each subscription before then.
Run a daily sync that extends every subscription in metadata.subscriptionIds:
Graph also sends a reauthorizationRequired lifecycle notification when a subscription needs attention. You can handle it in a sync’s onWebhook to renew right away.

5. Unsubscribe when a connection is deleted

Delete a connection’s subscriptions before the connection itself, so Graph stops sending its notifications.

Handle the webhook

Once routed, you have two options:
  • Forward it to your app: Nango forwards the notification to your webhook URL with connection attribution. See External webhook forwarding.
  • Process it in a sync: run a sync when the webhook arrives using webhookSubscriptions and onWebhook. Subscribe to created, updated or deleted, and to lifecycle events such as reauthorizationRequired to renew subscriptions. For change notifications, use the resource field, for example Users/<id>/Messages/<id> or Users/<id>/Events/<id>, to tell mail and calendar apart. Lifecycle notifications have no resource: use their subscriptionId to find the subscription. See Real-time syncs.
Graph can batch notifications from several subscriptions into one request. Nango forwards each connection only its own notifications. If no connection lists a notification’s subscriptionId, Nango still forwards it, but separately and without a connectionId. Subscription validation requests are answered but never forwarded.

Rollback strategy

Delete each subscription as in step 5, then remove its ID from the connection’s metadata.subscriptionIds.
Need help getting started? Join us in the community.