Scripting
Astraio includes a built-in scripting engine for automating API workflows.
Script Types
| Type | When It Runs |
|---|---|
| Pre-request | Before sending the request |
| Post-response | After 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