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.

Creating pre-request scripts

Pre-request scripts are written and deployed in the usual way, via the custom scripts option.

Usage

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

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.

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.

Sample script using authentication variables

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

<?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;
}

Applying a pre-request script to an endpoint

Any script you want to apply must be created as a custom script first.

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

Step 2 Click in the select a script field and choose the script that you want to use - for example:

All scripts created for your company are available for selection.

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

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

If you need to change the script code, this should be done in the custom scripts area, then (if appropriate) come back in and update the script version here.

Last updated