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:
const myVariable = theneo.variables["variable-name"];2. Reading Endpoints
To retrieve endpoints, use the following code:
const myEndpoint = theneo.endpoints;3. Reading Parameters
Access parameters in your pre-request script using:
const myParameter = theneo.parameters;4. Reading Query Parameters
To access query parameters, use the following syntax:
const myQueryParameter = theneo.queryParameters;5. Reading Header Variables
Header variables can be accessed as follows:
const myHeader = theneo.headers["header-name"];6. Setting Header Values
You can modify or set new header values with the following command:
theneo.headers["header-name"] = "header value";7. Reading Request Body
To access the request body in JSON format, use:
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:
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:
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);Available Libraries
Running Your Script
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
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.
What made this section unhelpful for you?
On this page
- Pre-Request Scripting