# Post-request script

## Introduction

Typically, a process flow run is triggered and a request for data is made via a connector shape - if the request is successful, data is retrieved and the flow continues.&#x20;

However, there may be scenarios where you need to control whether the connector shape or process flow run should fail or continue based on information returned from the connection request. To achieve this you can apply a `post-request` script to your endpoint.

{% hint style="info" %}
In the context of endpoints, `post-request` scripts and `response scripts` are the same thing.&#x20;
{% endhint %}

## Usage

When a `post-request` script is added to an endpoint, it is applied wherever that endpoint is used in process flows.&#x20;

If your `post-request` script should only be applied to specific flows, we recommend applying it as a response script for the connector shape in required flows. For more information please see: [Using connector shape response scripts](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape/using-connector-shape-response-scripts).&#x20;

## About post-request scripts

A `post-request` script receives the response code, headers, and body from connection requests and - utilising `response_code` actions - returns a value determining whether the connector shape/flow run continues or stops.

## Creating post-request scripts

`Post-request` scripts are written and deployed in the usual way, via the [custom scripts](https://doc.wearepatchworks.com/product-documentation/marketplace/marketplace-scripts) option. However, two additional options can be used: `response_code` and `message`. For more information about working with these options, please see: [Using connector shape response scripts](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape/using-connector-shape-response-scripts).  &#x20;

{% hint style="danger" %}
If a connector shape is configured for an endpoint associated with a `post-request` script AND a `response script` is applied in connector shape settings, **the connector shape takes precedence**. In this scenario, the `response script` applied in the connector shape is used and the `post-request` script applied to the endpoint is ignored.
{% endhint %}

## Applying a post-request script to an endpoint

{% hint style="info" %}
Any script you want to apply must be created as a [custom script](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting) first.
{% endhint %}

**Step 1**\
Select the `post-request script` tab for your endpoint:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FdzwR0AUxL3lQ2d6CbCmd%2Fpost%202a2.png?alt=media&#x26;token=f19424ee-0828-465b-87d9-aa1ea8b2c8cc" alt=""><figcaption></figcaption></figure></div>

**Step 2**\
Click in the `select a script` field and choose the script that you want to use - 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%2FNxCVE1Q9VK9s5QcXOTI9%2Fpost%202a1.png?alt=media&#x26;token=48b52ac2-16a8-4ba2-975d-b4d11e1614f9" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
All scripts created for your company are available for selection.
{% endhint %}

**Step 3**\
Choose the script version that you want to use:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fd76A2tzUpAo9RzyNKc5B%2Fpost%202b.png?alt=media&#x26;token=424c8955-57a7-483a-ad0c-a56ecace53e1" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
If your script is updated in future, remember to come back here and (if appropriate) select the later version.&#x20;
{% endhint %}

**Step 4**\
Review the script code to be applied, then click the `save and go back` button:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F8oFfhn8dlt0GwySzhtdn%2Fpost%202c.png?alt=media&#x26;token=7dba80d6-cb95-4ac2-bfa2-ee6f208e2df7" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
If you need to change the script code, this should be done in the [custom scripts](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting) area, then (if appropriate) come back in and update the script version here.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/4-endpoints/endpoint-options/post-request-script.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
