# Start a workflow from Microsoft Graph events

> Run a workflow when Microsoft Graph reports a new or changed message, event, file, or other item, with a Webhook block set to Microsoft Graph.

To run a workflow when Microsoft Graph reports that a message, a calendar event, a file, or another item was created, changed, or deleted, add a **Webhook** block whose **Source** is **Microsoft Graph** and name its URL in a Graph subscription. Graph posts change notifications in a list, and one request can hold several. Each notification in the list starts its own run.

A notification says what changed and where. It does not contain the item. The run fetches the item from Graph with an HTTP request block.

You need an app that can call Microsoft Graph with the permissions the resource asks for, a way to send the call that creates a subscription, and edit access to the workflow's project. Graph has no screen for subscriptions: you create one with an API call.

## Add the Webhook block and generate its URL

1. In a draft workflow, add a **Webhook** block. The block palette also lists **Microsoft Graph webhook** in the **Start** group, which adds the block with its source already set.
2. Under **Source**, choose **Microsoft Graph**. The line under the setting reads "Checks the credentials the sender repeats · one run per event in a batch · skips duplicates".
3. Under **Webhook URL**, select **Generate URL**, then copy the URL.

The badge beside the URL reads "Runs start after publish". The URL contains a token, and anyone who has the URL can send requests to it. Keep it out of shared documents.

## Choose a clientState and save it

Graph does not sign a change notification. What shows that a notification came from Graph is `clientState`: a value you choose when you create the subscription, which Graph then repeats in every notification of that subscription. OutWin compares it with the value saved on the block.

1. Make up a long random value of up to 128 characters. Treat it as a password.
2. In OutWin, paste it into **clientState** in the block's settings and select **Save**.
3. Keep the value for the next section.

After you save, the settings show only the last four characters of the value, with **Replace** and **Remove**. Under **Set up**, the second line changes to "The clientState is saved: deliveries are verified."

OutWin saves the value on the block's URL. It is not saved on a connection. A second Webhook block has its own URL and its own value.

Without a saved value the block still starts runs, and nothing is checked: anyone who has the URL can start the workflow. Publishing then shows a warning that ends "has no signing secret saved, so its deliveries are not verified: anyone who has this URL can start this workflow". The warning does not block publishing.

The value is all that is checked. It carries no signature and no time, so a copy of a notification that is sent to the URL again passes the check. OutWin skips it only when the notification has an `id` that already ran.

## Create the subscription with Graph's API

1. Send a `POST` request to `https://graph.microsoft.com/v1.0/subscriptions`.
2. In the request body, set `notificationUrl` to the URL you copied and `clientState` to the value you saved.
3. Set `resource` to what you want to watch, `changeType` to the changes you want, and `expirationDateTime` to the moment the subscription ends.

For example, this body watches your own mailbox for new messages:

```json
{
  "changeType": "created",
  "notificationUrl": "<the block's URL>",
  "resource": "/me/mailfolders('inbox')/messages",
  "expirationDateTime": "2026-11-25T11:00:00.0000000Z",
  "clientState": "<the value you saved>"
}
```

Before it creates the subscription, Graph sends the URL a validation token and expects the same token back within 10 seconds. The block answers it, also while the workflow is a draft. **Recent deliveries** lists the request as **Handshake** with "Microsoft Graph confirmed the URL.", and the last line under **Set up** reads the same. It starts no run. If the block's URL has not been generated, the validation fails and Graph does not create the subscription.

Use one `clientState` for every subscription that names this URL. Graph can put notifications of several subscriptions in one request, and if any notification in a request carries a different value, OutWin turns the whole request away.

A subscription that asks Graph to include the changed item in the notification gets encrypted content and signed tokens. OutWin does not decrypt that content or check those tokens. Create subscriptions for this block without that option.

## Renew the subscription before it expires

A Graph subscription ends at its `expirationDateTime`. Graph then deletes it and sends nothing more, and no run starts. OutWin does not renew a subscription.

To renew one, send a `PATCH` request to `https://graph.microsoft.com/v1.0/subscriptions/{id}` with a later `expirationDateTime`, on a schedule of your own. Microsoft sets the longest lifetime by the kind of resource: 60 minutes for presence, three days for Teams resources, under seven days for Outlook mail and calendar items, and under 30 days for OneDrive and SharePoint items. Each notification says when its subscription ends, in `subscriptionExpirationDateTime`.

Graph can also warn you. A subscription created with `lifecycleNotificationUrl` receives notices about itself, and that URL can be the block's own URL. The notices arrive as events of the types `reauthorizationRequired`, `subscriptionRemoved`, and `missed`.

## Choose which events start a run

Under **Events that start a run**, tick the types that start a run. The block offers Graph's three change types, `created`, `updated`, and `deleted`, and the three notices about a subscription, `reauthorizationRequired`, `subscriptionRemoved`, and `missed`. With none ticked, every notification Graph sends starts a run. To start runs for a type that is not listed, type it into the box under the list and select **Add**.

A notification whose type is not ticked is listed as **Filtered out** and starts no run. To narrow further by a field of the notification, add a row under **Only when**. For example, a row with the field `body.subscriptionId` starts runs for one subscription only. [Webhook settings](https://outwin.ai/docs/reference/triggers-approvals-and-waits/#webhook-settings) lists the comparisons.

## Send a test and check what arrived

1. Cause a change the subscription watches. For the mail example, send a message to that mailbox.
2. In OutWin, look under **Recent deliveries** in the block's settings.

**Recent deliveries** lists every notification that reached the URL, including while the workflow is a draft. Notifications that arrived in one request are listed one by one, each marked with its place in the request, such as "2 of 5". On a draft each is listed as **Dropped** with "The workflow is not published, so nothing ran." It starts no run and is not run later. The last line under **Set up** changes from "Microsoft Graph confirmed the URL." to, for example, "Received created. Dropped."

With a value saved, a notification listed as **Dropped** on a draft has passed the check. A request that fails the check is listed as **Refused**.

If you have edit access, select a notification to see what the block passed on. The list is kept for seven days, and the data of the newest 20 events is kept.

## Read the notification and fetch the item

Each run receives one notification. An HTTP request block or a database block directly after the Webhook block reads it with these expressions. An AI block such as **Think** receives the whole value as its input.

| Expression | What it holds |
| --- | --- |
| `{{input.body}}` | One notification as Graph sent it, with fields such as `subscriptionId`, `changeType`, `resource`, `resourceData`, and `tenantId`. OutWin removes `clientState` before it stores the notification or passes it on. |
| `{{input.body.resource}}` | The path of the item that changed, as Graph writes it. |
| `{{input.body.resourceData.id}}` | The id of the item that changed. Read any other field the same way. |
| `{{input.event.type}}` | The notification's type, such as `created`. It is the `changeType` field, or the `lifecycleEvent` field for a notice about the subscription. |
| `{{input.event.id}}` | The notification's `id` field. It is empty when Graph sends none. |
| `{{input.event.time}}` | Empty. Graph sends no time for a notification. `{{input.delivery.receivedAt}}` holds the moment the request arrived. |
| `{{input.delivery.verified}}` | `true` only when every notification in the request carried the saved value. |

A block further along reads the same values through the Webhook block's name, as in `{{steps["Microsoft Graph"].body.resourceData.id}}` for a block named `Microsoft Graph`. [Data and context](https://outwin.ai/docs/reference/data-and-context/) describes what each block passes on.

To get the item, add an [HTTP request](https://outwin.ai/docs/reference/blocks/http-request/) block after the Webhook block. It needs an [API connection](https://outwin.ai/docs/build/connect-an-api/) with the base URL `https://graph.microsoft.com` and a credential Graph accepts. Graph takes OAuth access tokens that last about an hour, and an API connection holds a fixed credential with no OAuth sign-in. You supply the token and keep it current yourself. OutWin does not sign in to Microsoft or renew an access token. For the mail example, set the block to send a `GET` request to `/v1.0/me/messages/{{input.body.resourceData.id}}`. Build the path around the id. A value that an expression puts in a path cannot hold a `/`, so `{{input.body.resource}}` cannot serve as the path.

## Publish and check the runs

[Publish the workflow](https://outwin.ai/docs/run/publish-and-start/). From then on, each notification that passes the filter starts a run. On a published workflow the block's settings are read-only, and **Recent deliveries** is in the block's inspector. You can still replace the saved value there.

| Outcome | What happened |
| --- | --- |
| **Ran** | The notification started a run. |
| **Waiting** | 10 runs of the workflow are already active. The notification starts a run when one of them ends. A notification that waits longer than an hour is listed as **Dropped**. |
| **Duplicate** | A notification with the same `id` already ran in the last 72 hours. The line reads "This event was already received and run, so it did not run again." A notification that Graph sends without an `id` is not checked for duplicates. |
| **Handshake** | Graph's validation of the URL when a subscription was created. It starts no run. |

Runs start in the order notifications arrive. That is not always the order things happened, so do not build a workflow that relies on `created` arriving before `updated`.

Receiving a notification costs no credits, and neither does one that is filtered out, a duplicate, or a handshake. A run that starts uses credits for the blocks that run in it.

## If nothing runs

Open **Recent deliveries** and read the outcome and the sentence beside the newest notification.

| What you see | Cause | Fix |
| --- | --- | --- |
| "Nothing has arrived yet." | No request has reached the URL, not even the validation. | Check that the subscription's `notificationUrl` is the block's current URL. **Rotate token** on a published workflow changes the URL, and the old one stops working: create the subscription again with the new one. |
| **Handshake** and nothing else | Graph created the subscription, and nothing it watches has changed since. | Cause a change of a kind the subscription's `changeType` covers, in the place its `resource` names. |
| Notifications arrived for some days and then stopped | The subscription reached its `expirationDateTime` and Graph deleted it. | Create the subscription again with the same URL and `clientState`, and renew it before it ends. |
| **Dropped** with "The workflow is not published, so nothing ran." | The workflow is a draft. | Publish the workflow. Notifications that arrived before then are not run later. |
| **Dropped** with "The workflow is paused, so nothing ran." | The workflow is paused. | Resume the workflow. Notifications that arrived while it was paused are not run later. |
| **Filtered out** with `Events of type "updated" are not chosen on this trigger.` | The notification's type is not ticked under **Events that start a run**. | Tick the type, or clear every tick so that all notifications start a run. The settings are read-only on a published workflow. |
| **Refused** with "The value the request carried did not match the one saved here." | At least one notification in the request carried a `clientState` that is not the saved value. This can happen when the subscription was created with another value, or when a second subscription with another value names the same URL. | Select **Replace** and save the value the subscription was created with, or create the subscription again with the saved value. Give every subscription that names this URL the same value. You do not republish the workflow. |
| **Refused** with "The request did not carry the value this trigger expects." | A value is saved in OutWin, and the request was not a list of Graph notifications. A request sent by hand to try the URL is not one. | Cause a change in what the subscription watches so that Graph sends the request. |

A request listed as **Refused** is answered with status `401` and starts nothing. OutWin keeps none of its data. Microsoft's documentation says Graph keeps trying a notification that is not accepted for up to four hours. For every other outcome in the table, Graph receives a success response and OutWin does not run the notification later.
