# Trigger shape (webhook)

## Introduction

Webhooks are real-time triggers for your process flows. Instead of Patchworks checking a third-party system for updates every few minutes, that system 'pings' Patchworks whenever an event occurs, scheduling the associated process flow to run.

When you choose to add a webhook to a process flow [trigger shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/trigger-shape), a[ Patchworks webhook URL with built-in authentication](#about-patchworks-webhook-urls) is auto-generated. This URL must be added to your third-party application, so it knows where to send event data.

How you use webhooks is driven by your business requirements and the capabilities of your third-party application. For example, your third-party application might send a `POST` request to a webhook which includes a batch of orders to be processed in the request `body`, or the webhook `body` might simply contain a notification message indicating that orders are ready for you to pull.&#x20;

{% hint style="warning" %}
For webhooks to be received, a process flow must be [deployed](https://doc.wearepatchworks.com/product-documentation/process-flows/process-flow-versioning#deploying-a-draft-or-inactive-version) and [enabled](https://doc.wearepatchworks.com/product-documentation/process-flows/managing-process-flows/enabling-and-disabling-a-process-flow/enabling-and-disabling-a-process-flow-without-virtual-enviroments).
{% endhint %}

{% hint style="info" %}
Information on this page is based on standard webhooks. *HMAC* (*Hash-based Message Authentication Code*) webhooks are also supported; for more information, please refer to [Using HMAC-verified webhooks](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/trigger-shape/trigger-shape-webhook/using-hmac-verified-webhooks).
{% endhint %}

## About Patchworks webhook URLs

### URLs

The structure of a webhook URL includes multiple elements. Some elements are always the same, some are unique to your organisation and/or environment, and some are unique for every webook:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FhJaOsxKW3pJZF7pGMUrx%2Fwebhook%20structure%20-%20with%20ve%201c.png?alt=media&#x26;token=db8338de-9cb0-4bf6-a547-deb6fe0770e1" alt=""><figcaption></figcaption></figure></div>

For example:

{% code overflow="wrap" %}

```
https://webhooks.wearepatchworks.com/api/v1/docs_demo/01k44sfgt56vbm266ct5xcmyr8ze5/2?patchworks_signature=0bzmguhjf767m236b9yye14cftmc4fvvsrs0x85kex7c76444m64
```

{% endcode %}

These elements are summarised below:

### Default response

The default response for a successful webhook trigger is a status code of `200`, with the following body:

{% code lineNumbers="true" %}

```json
{
  "message": "Flow initialised."
}
```

{% endcode %}

If required, you can [customise this response](#customising-your-webhook-response).

## GET or POST?

You can send both GET and POST requests to a webhook.&#x20;

{% hint style="info" %}
If a request includes both `body` content and `URL parameters`, the `body` content takes precedence (`URL parameters` are ignored).
{% endhint %}

## Adding a trigger webhook

Follow the steps below to add a new webhook trigger.

{% stepper %}
{% step %}
**Access trigger shape settings**

Click the `settings` icon associated with the `trigger` shape in your process flow:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FvPbltVCeMZVj0ZtnYRwY%2Ftrigger%20webhook%20settings%201.png?alt=media&#x26;token=81b4c548-efe9-4085-8456-4883c470fcd9" alt="" width="367"><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**(Optional) Select a virtual environment**

If your webhook needs to initialise a process flow that runs in a [virtual environment](https://doc.wearepatchworks.com/product-documentation/virtual-environments/about-virtual-environments), use the `customise URLs for virtual environment` selector to choose the required environment. For example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F8zoHktflwEtqtwZukXeJ%2Ftrigger%20webhook%20settings%202.png?alt=media&#x26;token=612ac003-cf38-4593-8841-476aa81bab53" alt="" width="375"><figcaption></figcaption></figure></div>

{% hint style="info" %}
If you don't use [virtual environments](https://doc.wearepatchworks.com/product-documentation/virtual-environments/about-virtual-environments), ignore this option.
{% endhint %}
{% endstep %}

{% step %}
**Generate a new webook**&#x20;

Move down to the `webhooks` section and click the `add new webhook` button. For example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FcDKXkgUJ8AgjRbjPMMx3%2Ftrigger%20webhook%20settings%203.png?alt=media&#x26;token=d34dceec-97b0-494c-afab-1f298167e306" alt="" width="375"><figcaption></figcaption></figure></div>

...a [unique Patchworks webhook URL](#about-patchworks-webhook-urls) is generated - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FUyJQdEXTPTKp4alIDwRO%2Ftrigger%20webhook%20settings%204.png?alt=media&#x26;token=cc4220e7-0ffc-466c-a32d-b755685106cd" alt="" width="375"><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**Copy the webhook URL**

Use `copy url` OR `copy ve url` buttons to copy this URL, ready to paste into your third-party application.&#x20;

{% hint style="warning" %}
If you selected a virtual environment in step 2, you should use the `copy ve url` option - this ensures that the webhook triggers this process flow in the correct virtual environment.
{% endhint %}
{% endstep %}

{% step %}
**(Optional) customise webhook behaviour**

If you want to customise the response for your webhook, click the `edit` icon associated with the URL and make the required changes. For more information, please see [Customising your webhook response](#customising-your-webhook-response).
{% endstep %}

{% step %}
**Complete process flow development**

Build the rest of your process flow as required.&#x20;

{% hint style="info" %}
When you're ready, ensure that your process flow is [deployed](https://doc.wearepatchworks.com/product-documentation/process-flows/process-flow-versioning#deploying-a-draft-or-inactive-version) and [enabled](https://doc.wearepatchworks.com/product-documentation/process-flows/managing-process-flows/enabling-and-disabling-a-process-flow/enabling-and-disabling-a-process-flow-without-virtual-enviroments). Webhooks will not be received if this isn't done.&#x20;
{% endhint %}
{% endstep %}
{% endstepper %}

## Customising your webhook response

If required, you can change the default response for your webhook by selecting the 'edit' icon associated with the URL - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FIWJlRtg8q06OACgcEITo%2Ftrigger%202a.png?alt=media&#x26;token=62287588-1e2f-4c5c-8e7a-01aeb8b9af32" alt="" width="375"><figcaption></figcaption></figure></div>

Here, you'll find options to select an alternative status code and specify new body text:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F4F0oqoFXEmXTNNuXhhfz%2Fwebhok%20options.png?alt=media&#x26;token=167f3b8d-d579-406d-8466-94179fff7f8b" alt="" width="375"><figcaption></figcaption></figure></div>

Here you can:

* Use the **status code** dropdown field to select the required response code.
* Enter the required text in the body field.
* Select the required format for your body content (choose from JSON, XML or Plain text).&#x20;
* Add [HMAC security](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/trigger-shape/trigger-shape-webhook/using-hmac-verified-webhooks).
