> 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/plytix-prebuilt-connector/plytix-post-request-script.md).

# Plytix post-request script

## Introduction

Having installed the Plytix connector, some setup is required before it can be used. You must:

* [Stage 1: Create a post-request script](#stage-1-creating-a-post-request-script)
* [Stage 2: Apply the post-request script to your Plytix connector](#stage-2-applying-the-post-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).

## Stage 1: Creating a post-request script

A post-request script is required to generate an authentication token from the given instance credentials. Follow the steps below to create this script.

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

**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&amp;token=52863079-2a5a-4acf-8477-1f9c0b9bc52e" alt=""><figcaption></figcaption></figure></div>

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

```
Plytix Post-Request Script
```

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

```
Plytix authorisation post-request script (retrieve 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%2FTXOS3SA9QNBHvwVN8LU3%2Fadd%20script%201.png?alt=media&amp;token=cab6f69d-7521-476b-85d9-bdfa4b8f5ed7" alt=""><figcaption></figcaption></figure></div>

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

**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%2Fcid0GTC4Pmri5QxiXVtn%2Fadd%20script%202.png?alt=media&amp;token=f2c18627-cb83-4e2b-b0f9-cc17e7bc5329" alt=""><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&amp;token=b4a6f8e5-8ebf-4ef8-ab24-a130d29b50c5" alt=""> Post-request script code</summary>

{% code lineNumbers="true" %}

```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
 *    ]
 */

function handle($data)
{
    $curl = curl_init();
    $body = [
        'api_key' => $data['variables']['api_key'],
        'api_password' => $data['variables']['api_password'],
    ];
    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://auth.plytix.com/auth/api/get-token',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode($body),
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json'
    ),
    ));

    curl_close($curl);
    $response = curl_exec($curl);
    
    if ($response === false) {
      $error = 'cURL Error: ' . curl_error($curl);
      echo $error;
      throw new Exception($error);
    }

    $responseData = json_decode($response, true);
    if ($responseData === null) {
        $error = 'JSON decoding error: ' . json_last_error_msg();
        echo $error;
        throw new Exception($error);
    }

    $data['variables']['access_token'] = $responseData['data'][0]['access_token'];

    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%2FKYOKCFQ8y8Yp4ElaY7Nf%2Fadd%20script%203.png?alt=media&amp;token=98697396-2be0-45bc-bee8-73affeaf21e9" alt="" width="563"><figcaption></figcaption></figure></div>

## Stage 2: Applying the post-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 Plytix connector and click the **settings** icon.

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F663RwwBMfO4OoV9H1Jlr%2Fapply%20script%201.png?alt=media&amp;token=026eb478-2265-4a87-9b29-66fa6dd72833" alt=""><figcaption></figcaption></figure></div>

**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&amp;token=54acb6f2-f86b-44ed-8f10-ff3c9c5f3a19" alt=""><figcaption></figcaption></figure></div>

**Step 4**\
Select the **API key** authentication method:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FL1nf5DFINjBRkdjvcBSv%2Fapply%20script%202.png?alt=media&amp;token=fc201af7-c685-4190-8e76-c8e65329e45f" alt=""><figcaption></figcaption></figure></div>

**Step 5**\
In the lower panel, select the **post-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%2FbgtxgjllgbH9H1lYlQYv%2Fapply%20script%203.png?alt=media&amp;token=2218fd7d-63b0-40d3-aca4-964b265a8c07" alt=""><figcaption></figcaption></figure></div>

**Step 6**\
Click in the **select script** field and select your **Plytix Post-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%2FVrYdV8kNsqeBUISkKJe6%2Fapply%20script%204.png?alt=media&amp;token=1b5d050a-e2d5-45f7-95f0-0f604cf9723b" 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%2F0DTSJCfaALPevwPkpAq5%2Fapply%20script%205.png?alt=media&amp;token=6d42336d-9121-4b5c-b7ac-5767a34ed01a" 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%2FW3d0rVHCeECwBtMzG86A%2Fapply%20script%206.png?alt=media&amp;token=c052a091-c1cd-44e3-ba13-659884658ebd" 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).
