# Start a workflow from Postmark events

> Run a workflow when Postmark reports that an email was delivered, bounced, opened, or clicked, with a Webhook block set to Postmark.

To run a workflow when Postmark reports that an email was delivered, bounced, opened, or clicked, add a **Webhook** block whose **Source** is **Postmark** and give its URL to a webhook on a Postmark message stream. Postmark posts one event per request. Each event starts its own run.

Postmark does not sign its requests. What OutWin checks is a username and a password that you set on the webhook and that Postmark sends with every request.

You need a Postmark server whose webhooks you can edit, and edit access to the workflow's project. The Postmark steps happen in Postmark's own console.

## Add the Webhook block and generate its URL

1. In a draft workflow, add a **Webhook** block. The block palette also lists **Postmark webhook** in the **Start** group, which adds the block with its source already set.
2. Under **Source**, choose **Postmark**. 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.

## Create the webhook in Postmark with a username and a password

Choose a username and a password for this webhook before you start. Make the password long and random, and keep a copy of both until the next section.

1. In Postmark, open the server, then the message stream whose events you want, then its webhooks, and add a webhook.
2. Paste the URL you copied as the webhook's URL.
3. Set the username and the password as the webhook's Basic auth credentials.
4. Choose the events Postmark sends to this URL, and save the webhook.

Postmark's documentation also accepts the credentials inside the URL: put the username, a colon, the password, and `@` between `https://` and the rest of the block's URL.

When you create or edit a webhook, Postmark tests the URL for each event type you turned on and expects status `200`. The block answers with `200`, also while the workflow is a draft. A test request that Postmark posts is listed under **Recent deliveries**.

The Webhook block offers these Postmark event types: `Delivery`, `Bounce`, `SpamComplaint`, `Open`, `Click`, and `SubscriptionChange`.

## Save the Basic auth credentials

1. In OutWin, type the username, a colon, and the password into **Basic auth credentials** in the block's settings, as in `username:password`.
2. Select **Save**.

After you save, the settings show only the last four characters of what you saved, with **Replace** and **Remove**. Under **Set up**, the second step is marked as done.

From then on, a request that does not carry the same username and password is listed as **Refused**. OutWin does not pass the credentials on to the workflow and does not keep them in **Recent deliveries**.

The credentials are the same in every request. A match shows that the sender knows them. It does not show when the request was made or that its body is unchanged, as a signature would.

The credentials belong to one webhook, so OutWin saves them on the block's URL. They are not saved on a connection. A second Webhook block has its own URL and needs its own Postmark webhook and credentials.

Without saved credentials 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.

## Choose which events start a run

Under **Events that start a run**, tick the event types that start a run. With none ticked, every event Postmark 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**.

An event whose type is not ticked is listed as **Filtered out** and starts no run. To narrow further by a field of the event, add a row under **Only when**. [Webhook settings](https://outwin.ai/docs/reference/triggers-approvals-and-waits/#webhook-settings) lists the comparisons.

The event type is the `RecordType` field of what Postmark sends. An inbound email that Postmark forwards has no `RecordType`. With any type ticked, it is listed as **Filtered out** with "The event has no type, and this trigger runs only for chosen types."

## Send a test and check what arrived

1. In Postmark, cause one of the events the webhook sends, for example by sending an email through the message stream.
2. In OutWin, look under **Recent deliveries** in the block's settings.

**Recent deliveries** lists every event that reached the URL, including while the workflow is a draft. On a draft each event 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 "Waiting for the first event…" to, for example, "Received Delivery. Dropped."

With credentials saved, an event listed as **Dropped** on a draft has passed the check. A delivery that fails the check is listed as **Refused**.

If you have edit access, select an event 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 event in the next block

Each run receives one Postmark event. 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}}` | The event as Postmark sent it, with fields such as `RecordType` and `MessageID`. Other fields depend on the event type. |
| `{{input.body.MessageID}}` | One field of the event. Read any other field the same way. |
| `{{input.event.type}}` | The event's type, such as `Delivery`. It is the event's `RecordType` field. |
| `{{input.event.id}}` | The event's id. It is the request's `X-PM-Webhook-Trace-Id` header, and the body does not hold it. It has no value when Postmark sends the request without that header. |
| `{{input.event.time}}` | The event's time as an ISO 8601 date and time. It is read from the event's `DeliveredAt`, `BouncedAt`, `ReceivedAt`, or `ChangedAt` field, whichever the event has. |
| `{{input.delivery.verified}}` | `true` only when the credentials were checked and matched. |

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

## Publish and check the runs

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

| Outcome | What happened |
| --- | --- |
| **Ran** | The event started a run. |
| **Waiting** | 10 runs of the workflow are already active. The event starts a run when one of them ends. An event that waits longer than an hour is listed as **Dropped**. |
| **Duplicate** | A request with the same `X-PM-Webhook-Trace-Id` header already ran in the last 72 hours. The line reads "This event was already received and run, so it did not run again." A request without that header is not compared with earlier ones and runs each time it arrives. |

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

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

| What you see | Cause | Fix |
| --- | --- | --- |
| "Nothing has arrived yet." | No request has reached the URL. | Check that the webhook is on the message stream that sends the email, that it sends the event you caused, and that its URL is the block's current URL. **Rotate token** on a published workflow changes the URL, and the old one stops working. |
| **Dropped** with "The workflow is not published, so nothing ran." | The workflow is a draft. | Publish the workflow. Events that arrived before then are not run later. |
| **Dropped** with "The workflow is paused, so nothing ran." | The workflow is paused. | Resume the workflow. Events that arrived while it was paused are not run later. |
| **Filtered out** with `Events of type "Open" are not chosen on this trigger.` | The event's type is not ticked under **Events that start a run**. | Tick the type, or clear every tick so that all events 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 username and password on the webhook in Postmark are not the ones saved in OutWin. This also happens when they were saved in OutWin in another form than `username:password`. Postmark's own test of the URL gets `401` while this is the case. | Select **Replace** and save the credentials again as `username:password`, exactly as they are set on the webhook in Postmark. You do not republish the workflow. |
| **Refused** with "The request did not carry the value this trigger expects." | Credentials are saved in OutWin, and the request carried none. The webhook in Postmark has no username and password, or the request was sent by hand to try the URL. | Set the username and password on the webhook in Postmark. To receive the events unchecked, select **Remove** in OutWin. |

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