> For the complete documentation index, see [llms.txt](https://doc.wearepatchworks.com/product-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/patchworks-connectors/emarsys-prebuilt-connector/emarsys-pre-request-script.md).

# Emarsys pre request script

## Introduction

Having [installed the Emarsys connector](/product-documentation/connectors-and-instances/working-with-connectors/installing-a-connector.md), some setup is required before it can be used. You must:

* [Stage 1: Create a pre-request script](#stage-1-creating-a-pre-request-script)
* [Stage 2: Apply the pre-request script to your Emarsys connector](#stage-2-applying-the-pre-request-script)

Once this setup is complete, you can [add instances](/product-documentation/connectors-and-instances/working-with-instances/adding-an-instance.md) for use in [process flows](/product-documentation/process-flows/building-process-flows.md).&#x20;

## Stage 1: Creating a pre-request script

A pre-request script is required to generate an authentication token from username and password credentials. Follow the steps below to create this script.

**Step 1**\
From the Patchworks dashboard, select **scripts** from the left-hand navigation menu.&#x20;

**Step 2**\
Click the **create script** button:

<div align="left"><figure><img src="/files/dkPKvXj5eziNnGeJAVpf" alt=""><figcaption></figcaption></figure></div>

**Step 3**\
In the **name** field, type the following:

```
Emarsys Pre-Request Script
```

**Step 4**\
In the **description** field, type the following:

```
Generate token
```

**Step 5**\
Click in the **language** field and select **PHP 8.1**:

<div align="left"><figure><img src="/files/adlESnGjTl3ALX9zpfRg" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 6**\
Click the **create** button:

<div align="left"><figure><img src="/files/6r6FW1giFHpaOUhqHiCA" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 7**\
The script is created and opened in edit mode - select and remove any placeholder code:

<div align="left"><figure><img src="/files/WZPq7cW672F3Twxua616" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 8**\
Paste in the code below.

<details>

<summary><img src="/files/4iYjBn9HEfUXX4ZSP1vh" alt=""> Pre-request script code</summary>

{% code lineNumbers="true" %}

```php
<?php

function handle($data) {

  $username = $data['variables']['username'];
  $nonce = md5(mt_rand());
  $timestamp = gmdate('c');
  $secret = $data['variables']['password'];

  $passwordDigest = base64_encode(sha1($nonce . $timestamp . $secret, false));

  $data['variables']['token'] = 'UsernameToken ' .
                'Username="'.$username.'", ' .
                'PasswordDigest="'.$passwordDigest.'", ' .
                'Nonce="'.$nonce.'", ' .
                'Created="'.$timestamp.'"';

  return $data;
};
```

{% endcode %}

</details>

**Step 9**\
Click the **save and deploy** button:

<div align="left"><figure><img src="/files/PlnHDNLttSiG7BGt0soC" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 10**\
Click the **scripts** element of the breadcrumb trail to exit back to your list of scripts and go to the next stage:

<div align="left"><figure><img src="/files/xrDu2mowWi3Hvra5XYFm" alt=""><figcaption></figcaption></figure></div>

## Stage 2: Applying the pre-request script

**Step 1**\
From the Patchworks dashboard, select **connectors and instances** from the left-hand navigation menu to access your installed connectors.

**Step 2**\
Find your Emarsys connector and click the **settings** icon:

<figure><img src="/files/16u1xCGmQmtrfRrbJ5Dl" alt=""><figcaption></figcaption></figure>

**Step 3**\
Click the **authentication** option:

<div align="left"><figure><img src="/files/uoPbie0xOCdeetFbsHPj" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 4**\
Select the **token** authentication method.

**Step 5**\
Select the **pre-request script** tab:

<div align="left"><figure><img src="/files/tilJd49MeEOuY8LsAmX6" alt=""><figcaption></figcaption></figure></div>

**Step 6**\
Click in the **select script** field and select your **Emarsys Pre-Request Script**:

<div align="left"><figure><img src="/files/GmH0HDU9F1ywTKTgW1rs" alt=""><figcaption></figcaption></figure></div>

**Step 7**\
Select the latest version:

<div align="left"><figure><img src="/files/xkHvOpmJ3M4MGQV3HFHm" alt=""><figcaption></figcaption></figure></div>

**Step 8**\
Save changes:

<div align="left"><figure><img src="/files/G4V6dNbI4EvfJ44T58x5" alt=""><figcaption></figcaption></figure></div>

**Step 9**\
You can now [add instances ](/product-documentation/connectors-and-instances/working-with-instances/adding-an-instance.md)of this connector for use in [process flows](/product-documentation/process-flows/building-process-flows.md).
