# Start a workflow from Google Calendar events

> Run a workflow when Google Calendar reports that something on a calendar changed, with a Webhook block set to Google Calendar.

To run a workflow when Google Calendar reports that an event on a calendar was created, changed, or deleted, add a **Webhook** block whose **Source** is **Google Calendar** and name its URL in a Calendar notification channel. Calendar posts one message per request. Each message starts its own run.

A message has no body. Its headers say that something changed and on which channel. They do not say which event. The run asks Calendar what changed with an HTTP request block.

You need a way to call the Calendar API as the Google Account whose calendar you want to watch, and edit access to the workflow's project. Google has no screen for notification channels: 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 **Google Calendar webhook** in the **Start** group, which adds the block with its source already set.
2. Under **Source**, choose **Google Calendar**. The line under the setting reads "Checks the credentials the sender repeats · one run per event · 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 channel token and save it

Google does not sign a channel's messages. What shows that a message came from your channel is the channel token: a value you choose when you create the channel, which Google then repeats in every message of that channel. OutWin compares it with the value saved on the block.

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

Save the token before you create the channel. Google sends the channel's first message when the channel is created. Its documentation says that message can arrive before the call that creates the channel returns.

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

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

Without a saved token 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 check shows only that the sender knows the token. A message carries no signature and no time, so anyone who learns the token and the URL can start a run. A message holds no data, and the run reads from Calendar with the workflow's own connection, so such a run reads only what that connection can read.

## Create the channel with Calendar's API

1. Send a `POST` request to `https://www.googleapis.com/calendar/v3/calendars/{calendarId}/events/watch`, with the id of the calendar in place of `{calendarId}`.
2. In the request body, set `address` to the URL you copied, `token` to the value you saved, `type` to `web_hook`, and `id` to a value of your own that no other channel has used.
3. Under `params`, set `ttl` to the number of seconds the channel lasts.

For example, this body creates a channel that lasts seven days:

```json
{
  "id": "<a unique value of up to 64 characters>",
  "type": "web_hook",
  "address": "<the block's URL>",
  "token": "<the token you saved>",
  "params": {
    "ttl": "604800"
  }
}
```

Send the request from any tool that can call the Calendar API as that account. An HTTP request block can send it too, within the limit on its credential described under [Read the message and fetch the changes](https://outwin.ai/docs/build/connect-an-api/start-a-workflow-from-google-calendar-events/#read-the-message-and-fetch-the-changes). OutWin does not create the channel for you.

Google answers with the channel, including a `resourceId`. Keep the `id` and the `resourceId`. Google's call that stops a channel, `https://www.googleapis.com/calendar/v3/channels/stop`, asks for both.

This channel watches the events of one calendar. Google's API has a watch method for three more things: the list of an account's calendars, a calendar's access rules, and an account's settings. Each takes the same request body, and the block receives their messages in the same way.

When the channel is created, Google sends one `sync` message to the URL. **Recent deliveries** lists it as **Ping** with "A ping from the sender, not an event. Nothing ran." The last line under **Set up** then reads "Received sync. Ping." The ping shows that Google can reach the URL and that the token matches. It starts no run.

## Replace the channel before it expires

A Calendar channel ends when its `ttl` runs out. Google then sends nothing more, and no run starts. OutWin does not renew a channel. Google's documentation says a channel cannot be renewed, only replaced with a new one.

A channel created without `ttl` lasts 604,800 seconds, which is seven days. Google's reference gives that default and no longest lifetime.

To keep messages coming, create a new channel before the old one ends, on a schedule of your own. Give it a new `id`, the same `address`, and the same `token`. Google's documentation says the old and the new channel can both be active for a time. One change can then arrive on each channel. Each message starts a run.

When a channel has an expiry, each of its messages says when it ends, in the `x-goog-channel-expiration` header.

## Choose which events start a run

Under **Events that start a run**, the block offers two types, `exists` and `not_exists`. They are the states Google reports in a message. With none ticked, every message except `sync` starts a run.

Calendar sends `exists` for every change, whether something was created, changed, or deleted. The type does not tell these apart. Google lists `not_exists` as a possible state and does not say when it sends it, so tick neither unless you have a reason to.

A message whose type is not ticked is listed as **Filtered out** and starts no run. To narrow further, add a row under **Only when**. A message has no body, so a row reads a header. For example, a row with the field `headers["x-goog-channel-id"]` starts runs for one channel 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 channel watches. For example, create an event on that calendar.
2. In OutWin, look under **Recent deliveries** in the block's settings.

**Recent deliveries** lists every message that reached the URL, including while the workflow is a draft. On a draft each message other than `sync` 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 "Received sync. Ping." to "Received exists. Dropped."

With a token saved, a message 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 message 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 message and fetch the changes

Each run receives one message. 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}}` | Empty. A Calendar message has no body. |
| `{{input.event.type}}` | The state Google reports, such as `exists`. It is the request's `X-Goog-Resource-State` header. |
| `{{input.headers["x-goog-channel-id"]}}` | The `id` you gave the channel. Header names are lowercase. |
| `{{input.headers["x-goog-resource-id"]}}` | Google's id for what the channel watches. It is the `resourceId` in the answer to the watch request. |
| `{{input.headers["x-goog-resource-uri"]}}` | The address of what the channel watches in the Calendar API. |
| `{{input.event.id}}` | A value OutWin builds from the channel's id and the request's `X-Goog-Message-Number` header, because Google sends no single id for a message. |
| `{{input.event.time}}` | Empty. Google sends no time for a message. `{{input.delivery.receivedAt}}` holds the moment the request arrived. |
| `{{input.delivery.verified}}` | `true` only when the request carried the saved token. |

OutWin removes the token's header before it stores a message or passes it on.

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

To get the changes, 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/) whose credential Google accepts for that account. OutWin does not sign in to Google and does not refresh an access token. A Google access token expires after about an hour, so you need your own way to supply a current one, for example a service of your own in front of Google's API.

Set the block to send a `GET` request to `/calendar/v3/calendars/` followed by the calendar's id and `/events`, under the host `https://www.googleapis.com`. Without a query parameter, Google lists every event on the calendar. The last page of the answer holds `nextSyncToken`. Keep it between runs, for example in a database, and pass it as the `syncToken` query parameter on the next call. Google then lists only the events that changed since, including deleted ones.

Google's reference says a sync token can expire. Google then answers with status `410`, and the block fails unless **Fail on an error status** is off. List the events again without a sync token to get a new one.

## Publish and check the runs

[Publish the workflow](https://outwin.ai/docs/run/publish-and-start/). From then on, each message 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 token there.

| Outcome | What happened |
| --- | --- |
| **Ran** | The message started a run. |
| **Waiting** | 10 runs of the workflow are already active. The message starts a run when one of them ends. A message that waits longer than an hour is listed as **Dropped**. |
| **Duplicate** | A message with the same channel id and message number already ran in the last 72 hours. The line reads "This event was already received and run, so it did not run again." |
| **Ping** | Google's `sync` message when a channel was created. It starts no run. |

Google's documentation does not say whether a message it sends again keeps its message number. If it does not, the repeat starts a second run. A run that lists changes from the saved sync token reads nothing twice.

Google's documentation says a small share of messages is dropped in normal operation. A change whose message is dropped starts no run. The next run that lists changes from the saved sync token still reads it.

Receiving a message costs no credits, and neither does one that is filtered out, a duplicate, or a ping. 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 message.

| What you see | Cause | Fix |
| --- | --- | --- |
| "Nothing has arrived yet." | No request has reached the URL, not even `sync`. | Check that the watch request succeeded and that its `address` is the block's current URL. **Rotate token** on a published workflow changes the URL, and the old one stops working: create a new channel with the new one. |
| **Ping** and nothing else | Google created the channel, and nothing it watches has changed since. | Cause a change on the calendar the channel watches. |
| Messages arrived for some days and then stopped | The channel's lifetime ran out. A channel created without `ttl` ends after seven days. | Create a new channel with a new `id`, the same URL, and the same token, and replace it before it ends. |
| **Dropped** with "The workflow is not published, so nothing ran." | The workflow is a draft. | Publish the workflow. Messages that arrived before then are not run later. |
| **Dropped** with "The workflow is paused, so nothing ran." | The workflow is paused. | Resume the workflow. Messages that arrived while it was paused are not run later. |
| **Filtered out** with `Events of type "exists" are not chosen on this trigger.` | The message's type is not ticked under **Events that start a run**. | Tick the type, or clear every tick so that all messages 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." | The channel was created with a `token` that is not the saved value. | Select **Replace** and save the token the channel was created with, or create a new channel with the saved token. You do not republish the workflow. |
| **Refused** with "The request did not carry the value this trigger expects." | A token is saved in OutWin, and the request carried none. The channel was created without `token`, or the request was sent by hand to try the URL. | Create a new channel whose `token` is the saved value. |

A request listed as **Refused** is answered with status `401` and starts nothing. OutWin keeps none of its data. For every other outcome in the table, Google receives a success response and OutWin does not run the message later.
