# Pre-request script

## Introduction

Pre-request scripts allow you to modify request parameters *before* sending the actual request. For example, you may wish to set dynamic values, add headers, or adjust the standard request body based on specific conditions.&#x20;

{% hint style="warning" %}
Pre-request scripts cannot be used for endpoints configured with the `body format` set to `form` and sending data as files (i.e. defining a file as a key value).
{% endhint %}

## Creating pre-request scripts&#x20;

`Pre-request` scripts are written and deployed in the usual way, via the [custom scripts](https://doc.wearepatchworks.com/product-documentation/marketplace/marketplace-scripts) option.&#x20;

## Usage

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

If your `pre-request` script should only be applied to specific flows, we recommend creating a new iteration of the endpoint. For more information please see: [Using a pre-request script for some process flows but not others](https://app.gitbook.com/o/QK9606D86GQKTsWinNMs/s/LYNcUBVQwSkOMG6KjZfz/~/changes/1596/process-flows/troubleshooting-process-flows/using-a-pre-request-script-for-some-process-flows-but-not-others).

## Authentication variables

`Pre-request` scripts can access authentication variables. For example, your system may require that a new signature (comprised of authentication variables) is generated for every request.

When a `pre-request` script is present, all defined authentication variables are passed in automatically. These are ignored if not actively used in the `pre-request` script.

<details>

<summary><img src="/files/4iYjBn9HEfUXX4ZSP1vh" alt=""> Sample script using authentication variables</summary>

The script below generates a signature for every request. This signature is comprised of authentication variables and the current timestamp.

```php
<?php

/**
 * Handler function.
 *
 * @param array $data [
 *      'payload'   => (string|null) the payload as a string|null
 *      'variables' => (array[string]string) any variables as key/value
 *      'meta'      => (array[string]string) any meta as key/value
 *      'flow'      => (array[mixed]) current flow data, including variables
 *    ]
 *
 * @return array $data Structure as above, plus 'logs' => (array[string]) Logs to be written to flow run log after script execution
 */

function processSign($data)
{
    $variables = $data['variables'];
    $body = json_decode($data['body'], true);
    
    $timestamp = round(microtime(true));
    $app_key = $variables["app_key"];
    $app_secret = $variables["app_secret"];
    $access_token = $variables["access_token"];
    $data_type = $body["data_type"];
    $type = $body["type"];

    $temp = "";
    $temp .= "access_token". preg_replace('/^"|"$/' , '', $access_token);
    $temp .= "app_key" . preg_replace('/^"|"$/', '', $app_key);
    $temp .= "data_type" . preg_replace('/^"|"$/', '', $data_type);
    $temp .= "timestamp" . preg_replace('/^"|"$/', '', $timestamp);
    $temp .= "type" . preg_replace('/^"|"$/', '', $type);

    $un_sign = $app_secret . $temp . $app_secret;
    $sign = MD5($un_sign);
    $sign = strtoupper($sign);
    $body['timestamp'] = $timestamp;
    $body['sign'] = $sign;
    $body['app_key'] = $app_key;
    $body['access_token'] = $access_token;

    $data['body'] = json_encode($body);

    return $data;
}
function handle($data)
{
    $data = processSign($data);

    return $data;
}
```

</details>

## Applying a pre-request script to an endpoint

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

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

<div align="left"><figure><img src="/files/aqQYqvqhi6H7lPnYBUJg" 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/knm0UHMYwCypp2E39K8j" 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="/files/cektaqNaimCcCI6zASsu" 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="/files/0r1QwsoXUW5q8B0dEKo1" 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 (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/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.
