Skip to main content

How to use scripts in your HTTP endpoint?

Maxim Workflows provide powerful scripting capabilities to modify requests and responses. These scripts help you transform data, add authentication, and handle complex response structures.
Scripts are optional - you’ll only need them when you want to modify your API’s behavior.

Request Modification

The prescript function runs before sending requests to your API. Use it to modify request parameters, add headers, or transform data.
All script hooks also support asynchronous operation. You can define them as async functions, for example: async function prescript(request) { ... }.

Response Processing

The postscriptV2 function processes API responses before displaying them. Use it to transform response data or extract specific fields:

Conversation Management

Simulation scripts only run for multi-turn conversations. They won’t execute for single-turn tests.
Use these scripts to set up and clean up multi-turn conversation tests:

Async Scripts

You can use async functions in any script hook to perform asynchronous operations such as fetching data or reading secrets before returning. Simply define the hook with the async keyword and await the required promises:

Viewing Logs

Scripts support standard JavaScript console methods for debugging and monitoring. You can use console.log(), console.error(), or any other console method to track execution flow and inspect data:

Variable Management

Maxim’s scripts flow providers a robust programmatic access to manage variable across the simulation course. Each sandbox has access to 4 methods

Add a new variable

Adds a variable to the simulation context using the given key and value. The variable remains accessible throughout the entire simulation.

Get a variable

Retrieves the value of a variable that was previously added to the simulation context, or provided by the dataset during test runs. Returns undefined if the variable is not found in the current context.

Set a variable

Sets the value of an existing variable in the simulation context, or creates it if it doesn’t exist. Use this to update the value of a variable at any point during the simulation.

Get a vault variable

This method allows you to securely retrieve a variable stored in the vault. Vault variables are typically used for secrets or sensitive data that should not be exposed in plain text. If the variable does not exist in the vault, this method will return undefined.

Pre-built Variables

Maxim provides several pre-built variables that are available in the script context for you to use:
  • input: The current input query or prompt from the dataset that is being sent to the endpoint. This represents the user’s message or query for the current turn of the conversation.
  • history: Contains all previous messages in a multi-turn conversation. This allows your scripts to access the complete conversation context, including all prior user messages and assistant responses.
  • simulationId: A unique identifier for the current simulation or test run. This can be used to track and correlate data across multiple interactions within the same simulation.

Available Libraries

You don’t need to use require or import to use these modules in scripts. These are directly available in the script environment.
  • axios: Axios is a popular HTTP client library for JavaScript. It provides a simple API for making HTTP requests and supports various features like request and response interceptors, request cancellation, and automatic JSON parsing.
  • fetch: Fetch is a modern web API for making HTTP requests in JavaScript. It provides a promise-based interface for fetching resources and supports features like streaming, request/response customization, and automatic JSON parsing.

Need access to more libraries in javascript sandbox?

Reach out to us if you need access to any other node libraries in the sandbox.