# 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FoTQs7eEfsfqZUPfRoiT7%2Ficon_code.svg?alt=media&#x26;token=b4a6f8e5-8ebf-4ef8-ab24-a130d29b50c5" 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](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting) first.
{% endhint %}

**Step 1**\
Select the `pre-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%2FdVFU1Ncl2qPMApLQwMIH%2Fpre-script%201.png?alt=media&#x26;token=13630c08-7ce1-48a0-93ad-43a755538e32" 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%2FvUathV7zpMKUa8X2e1Vr%2Fpre-script%202a.png?alt=media&#x26;token=850b5277-87d8-43fa-ada8-1db316e61d85" 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%2FoCSGLIw3n7p3Ok2Jhfzf%2Fpre-script%203.png?alt=media&#x26;token=3baab7c3-5cfd-4151-88a4-bb9d63885392" 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%2F1SbcVDbCb16B3ckIpAVj%2Fpre-script%204.png?alt=media&#x26;token=b263e236-f961-482d-996d-57169333007c" 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 %}
