Get Started

Theme switcher

Pre-Request Scripting

Pre-Request Scripts allow you to execute code before each HTTP request, making them ideal for tasks such as authentication, request headers manipulation, and setting up a consistent environment before fetching data from an external API. By incorporating pre-request scripts, you can streamline your request handling process and ensure consistent behavior across various endpoints.

1.Reading Environment Variables

You can access environment variables using the following syntax:

JavaScript
const myVariable = theneo.variables["variable-name"];

2. Reading Endpoints

To retrieve endpoints, use the following code:

JavaScript
const myEndpoint = theneo.endpoints;

3. Reading Parameters

Access parameters in your pre-request script using:

JavaScript
const myParameter = theneo.parameters;

4. Reading Query Parameters

To access query parameters, use the following syntax:

JavaScript
const myQueryParameter = theneo.queryParameters;

5. Reading Header Variables

Header variables can be accessed as follows:

JavaScript
const myHeader = theneo.headers["header-name"];

6. Setting Header Values

You can modify or set new header values with the following command:

JavaScript
theneo.headers["header-name"] = "header value";

7. Reading Request Body

To access the request body in JSON format, use:

JavaScript
const requestBody = theneo.requestBody.json;

8. Sending Requests within Pre-Request Scripts

You can send requests to other endpoints using the fetch API or similar methods. For instance, setting an Authorization header might look like this:

JavaScript
const token = theneo.variables["authToken"]; theneo.headers["Authorization"] = Bearer ${token};

9. Example Script

Here’s a complete example of a pre-request script that generates an HMAC signature and sets custom headers:

JavaScript
const apiKey = theneo.variables["apiKey"]; const apiSecret = theneo.variables["apiSecretKey"]; const timestamp = new Date().getTime();
console.log("API Key:", apiKey); console.log("API Secret:", apiSecret); console.log("Timestamp:", timestamp);
const requestData = JSON.stringify(theneo.requestBody.json); const rawSignature = ${apiKey}:${timestamp}:${requestData};
function generateHMAC(rawSignature, secret) { const signature = CryptoJS.HmacSHA256(rawSignature, secret); return CryptoJS.enc.Hex.stringify(signature); }
const signature = generateHMAC(rawSignature, apiSecret);
theneo.headers = { "API-Key": apiKey, "Timestamp": timestamp.toString(), "Signature": signature, "Content-Type": "application/json", "Accept": "/" };
console.log("Headers set for request:", theneo.headers);

Currently, async operations are limited.

Available Libraries

Currently, only the crypto library is available for use in your scripts. You can utilize any method provided by the crypto library.

If you would like to use any additional library, please contact our support team at hello@theneo.io.

Running Your Script

1

Enabling Pre-Request Scripts

To enable pre-request scripts:

  • Navigate to your project settings.
  • Go to Settings → Features → Pre-request script.
2

Running Your Script

After creating your script, you can run it using the API Explorer:

  • Press the Run Request button.

The pre-request script will execute first, followed by your regular request.

3

Verifying Your Script

Use the Network tab to check that the correct authorization values are sent during the request. This helps ensure that your pre-request script is functioning as expected.

Reading External Query Parameters

To authenticate users in your API explorer by reading and using externally passed query parameters, such as ?my_token=<token>and utilizing them in the pre-request script context.

Steps to Implement

1

Passing Query Parameters

When redirecting users from your website to the API explorer, include the token as a query parameter in the URL. For example:

Plain text
2

Using the Token in Pre-Request Script

Modify the pre-request script to access the token from local storage and use it as needed.

Setting up a Dynamic Base URL in API Explorer

The goal is to allow users to overwrite the default base URL from the API Explorer with a custom base URL set through a new field. This enables requests to be sent to a dynamic base URL specified by the user instead of a predetermined one.

1

Write the Pre-request Script

In the Pre-request Script add the following line of code:

JavaScript
theneo.baseUrl = theneo.variables["baseURL"];

This code will take the value entered in the baseURL field and assign it to the theneo.baseUrl variable, effectively setting the dynamic base URL for the API request.

2

Open API Explorer and Enter the Base URL

  • After saving, open the API Explorer again. You will now see a new field titled baseURL (the system will automatically create this field based on your custom "baseURL" configuration).
  • Enter the custom base URL you want to use for your requests in this baseURL field.
3

Sending Requests with Custom Base URL

  • Once the custom base URL is entered, all requests sent from the API Explorer will use this new base URL instead of the default one.
  • You can now send API requests to any dynamic endpoint by simply entering the base URL in the field, ensuring that the requests are sent to the right location.
Was this section helpful?

What made this section unhelpful for you?

On this page
  • Pre-Request Scripting