# Emarsys pre request script

## Introduction

Having [installed the Emarsys connector](https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/working-with-connectors/installing-a-connector), 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](https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/working-with-instances/adding-an-instance) for use in [process flows](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows).&#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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FSsP5wy7Blf4EK2hj0DxQ%2Femarsys%201.png?alt=media&#x26;token=52863079-2a5a-4acf-8477-1f9c0b9bc52e" 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FBs8rsd3ZVqTfX1Xby0nO%2Femarsys%202.png?alt=media&#x26;token=cbd5a8b7-ce09-4b3b-ab70-e8eda9c08ca0" alt="" width="563"><figcaption></figcaption></figure></div>

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

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FJ3rikAAqWRQviRwrKJLW%2Femarsys%203.png?alt=media&#x26;token=8d088626-e874-474c-9f63-5b73f347d716" 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FtWRsTbnTdd9WVLW1J9wx%2Femarsys%204.png?alt=media&#x26;token=f094c494-4e7d-40b8-b419-c6c3dd9ba0f9" alt="" width="563"><figcaption></figcaption></figure></div>

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

<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=""> 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FMaCiGeORJFC4e5GhUagb%2Femarsys%205.png?alt=media&#x26;token=470a41d3-0c71-4529-af83-e1df51c4788d" 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FXDdpI0vuFyYoVWYQxGWa%2Femarsys%206.png?alt=media&#x26;token=f0f33915-20eb-4667-90c4-10e471cb45eb" 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FO4H0Hm2EIrsk4EOkLljZ%2Femarsys%20apply%20script%201.png?alt=media&#x26;token=3ea3fd2c-2acd-42b3-a0c9-427277fc6b97" alt=""><figcaption></figcaption></figure>

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

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FYHBbceyjjCOjgi6JN30X%2Femarsys%20apply%20script%202.png?alt=media&#x26;token=54acb6f2-f86b-44ed-8f10-ff3c9c5f3a19" 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FvVUfqR7PGKlrmdH8WMDt%2Femarsys%20apply%20script%203.png?alt=media&#x26;token=a2940a84-0a8f-411a-9bf4-ccd0b28ba5a2" 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="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F5AJebr70DtyhWz0sDoG7%2Femarsys%20apply%20script%204.png?alt=media&#x26;token=509d538a-6adb-4aff-a04c-497de4db7c64" alt=""><figcaption></figcaption></figure></div>

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

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FrqO0KSEzZiVot1hi27Zs%2Femarsys%20apply%20script%205.png?alt=media&#x26;token=ba5567d9-9898-42ef-8110-434e4c96ef90" alt=""><figcaption></figcaption></figure></div>

**Step 8**\
Save changes:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FKstn1XHVU0mKlIjycNcp%2Femarsys%20apply%20script%206.png?alt=media&#x26;token=2e61d264-a7af-4f43-bb75-03521b21a09e" alt=""><figcaption></figcaption></figure></div>

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