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.

function prescript(request) {
	request.headers["Authorization"] = "Bearer your-token";

	request.data = {
		...JSON.parse(request.data || "{}"),
		timestamp: Date.now(),
	};

	return request;
}

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:

function postscriptV2(response, request) {
	return {
		content: response.data.message,
		confidence: response.data.metadata?.confidence || 1.0,
	};
}

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:

function preSimulation() {
	return {
		sessionId: generateUUID(),
		startTime: Date.now(),
	};
}

function postSimulation() {
	return {
		status: "completed",
		endTime: Date.now(),
	};
}

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.

maxim.addVariable("variable-name", "value-to-set")

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.

// Returns string or undefined
var test = maxim.getVariable("test-variable")

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.

maxim.setVariable("variable-name", "value to set");

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.

var vaultVariable = maxim.getVaultVariable("variable-name");

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.