# Pre-request script

## Introduction

Pre-request scripts allow you to modify authentication request parameters before sending the actual request. You can set dynamic values, compute signatures, or add headers based on specific conditions.&#x20;

For example, you may have a third-party application which only accepts user passwords which are base 64 encoded. Expecting your users to know how to encode their password when they need to add connector instance for this application isn't always realistic, so a pre-request script can take care of this automatically.&#x20;

## How it works

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

1. A [custom script should be created](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts.md) which takes a given password and performs base 64 encoding.
2. This script is [applied to the pre-request script tab](#applying-a-pre-request-script) for the appropriate authentication method.
3. A user chooses to [add an instance](/product-documentation/connectors-and-instances/working-with-instances/adding-an-instance.md) using this authentication method and enters their username and password. Typically, an authentication request would be sent at this point - but NOT if a pre-request script is associated with the authentication method.
4. The pre-request script defined for this authentication method runs - any required values from authentication variables provided by the user are taken to manipulate the authentication request [body](/product-documentation/developer-hub/connector-builder/building-your-own-connector/3-authentication-methods/authentication-method-options/body.md) as needed.
5. The authentication request is sent to the given endpoint, including the manipulated [body](/product-documentation/developer-hub/connector-builder/building-your-own-connector/3-authentication-methods/authentication-method-options/body.md) content.&#x20;
6. Once the script has run, the authentication request is made, including the encoded password. &#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 pre-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 **pre-request script** tab for your authentication method:

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

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

<figure><img src="/files/47D88aLf2ZlS4V4Zgbdp" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/h2Bt3eGAfVX8fI1h7OC2" 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/pre-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.
