# 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).&#x20;

## 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.&#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:

```
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="/files/ZATwHlWcvCm3tfHFea3B" 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="/files/eXfw6xyIfQ8PQcnmH2OF" alt=""><figcaption></figcaption></figure></div>

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

<details>

<summary><img src="/files/4iYjBn9HEfUXX4ZSP1vh" 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="/files/XpoWFA9TiLAUSRylRFdM" 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="/files/ajeZ3r9giRKGvQ9uziBe" alt=""><figcaption></figcaption></figure></div>

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

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

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

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

**Step 5**\
In the lower panel, select the **post-request script** tab:

<div align="left"><figure><img src="/files/QdeB7GORW9YUDDJO73ws" 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="/files/dAYSQRHXPB4zFN9IrQ38" alt=""><figcaption></figcaption></figure></div>

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

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

**Step 8**\
Save changes:

<div align="left"><figure><img src="/files/sc74kmECnQCwJ0G4gKVd" 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).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/patchworks-connectors/plytix-prebuilt-connector/plytix-post-request-script.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
