Skip to Content
UsageScripting

Scripting

Astraio includes a built-in scripting engine for automating API workflows.

Script Types

TypeWhen It Runs
Pre-requestBefore sending the request
Post-responseAfter receiving the response

Writing Scripts

Pre-request Script

// Set dynamic timestamp variables.set("timestamp", Date.now().toString()); // Generate random ID variables.set("request_id", crypto.randomUUID()); // Set authentication header headers.set("Authorization", `Bearer ${variables.get("token")}`);

Post-response Script

// Extract data from response const userId = response.body.data.id; variables.set("user_id", userId); // Log response console.log(`Status: ${response.status}`); console.log(`Duration: ${response.duration}ms`);

Available APIs

Variables

// Get a variable const value = variables.get("name"); // Set a variable variables.set("name", "value");

Headers

// Set request header headers.set("Content-Type", "application/json"); // Get response header const contentType = response.headers.get("Content-Type");

Console

console.log("Message"); console.error("Error"); console.warn("Warning");

Crypto

// Generate UUID const id = crypto.randomUUID(); // Generate random string const random = crypto.randomBytes(16).toString("hex");

Debugging

  • Use console.log() to output values
  • Check the Console tab in the response panel
  • Scripts run in a sandboxed environment
Last updated on