# Post-request script

## Introduction

Post-request scripts are useful for manipulating the response data before displaying or using it. You can extract specific values, format data, or even perform calculations. This helps in preparing the response data for further requests, or for displaying in a more user-friendly manner.

For example, we might have a connector which expects API requests to be authenticated with a `{{token}}` value received in response to an authentication request. However, suppose that the authentication request response includes the required **token** value AND a **client\_id** value in the same `detail` field - somehow we need to ensure that only the **token** element is injected into a `{{token}}` variable, to be used for authenticating subsequent API requests.

To achieve this, we could create a custom script to extract the the **token** element from the `detail` field and then apply this to the **post-request script** tab for the required authentication method.

## How it works

Let's use the example scenario above to break down how **post-request scripts** work:

1. A [custom script should be created](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts.md) which extracts the **token** element of the `detail` field and applies this as the `{{variable}}` value.
2. This script is [applied to the post-request script tab](#applying-a-post-request-script) for the appropriate authentication method.
3. A user [adds an instance](/product-documentation/connectors-and-instances/working-with-instances/adding-an-instance.md) using this authentication method, and enters their username and password. &#x20;
4. The authentication request is sent and a successful response is returned, with **client id** and **token** values in the same `detail` field.
5. The post-request script defined for this authentication method runs, extracting the **token** element from the `detail` field and injecting it into the `{{token}}` variable for the authentication method.   &#x20;

{% hint style="info" %}
Both **pre** and **post** request scripts have an expected format, where the input expects two different keys:

* `payload` - most commonly used, and contains the payload for the script.
* `variables` - contains all [authorisation variables](/product-documentation/developer-hub/connector-builder/building-your-own-connector/3-authentication-methods/authentication-method-options/auth-variables.md), together with a hidden `{{token}}` variable (which is created after the authentication request is sent).
  {% endhint %}

## Applying a post-request script for an authentication method

{% hint style="info" %}
Any script that you want to apply must first be created as a [custom script](/product-documentation/developer-hub/custom-scripting.md).&#x20;
{% endhint %}

**Step 1**\
Select the **post-request script** tab for your authentication method:

<div align="left"><figure><img src="/files/Zk4r1cWe63x23hRVK8OM" 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="/files/0jVP5yb78zhYd4lrERds" alt=""><figcaption></figcaption></figure></div>

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

<figure><img src="/files/pLgxGUZQYj8GHBDi4a6p" alt=""><figcaption></figcaption></figure>

**Step 4**\
Now you can review the script code to be applied and save changes:

<figure><img src="/files/2BilNZSUQRjCF0i6F3Nf" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
If you need to change the script code, this should be done in the [custom scripts](/product-documentation/developer-hub/custom-scripting.md) area, then the updated script must be deployed.
{% 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/3-authentication-methods/authentication-method-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.
