Automate your workflow with pre-request and post-response scripts using the Postman-compatible pm API
LiteClient supports JavaScript scripting to automate API workflows. Write pre-request scripts to set variables or generate tokens before requests are sent, and post-response scripts to validate responses and extract data.
Pre-request scripts run before the request is sent. Use them to:
Set environment variables dynamically
Generate authentication tokens or signatures
Modify request state
Log pre-request information
Example:
Copy
Ask AI
// Set a timestamp variablepm.environment.set('timestamp', Date.now());// Generate a random request IDpm.environment.set('requestId', Math.random().toString(36).substring(7));console.log('Request ID:', pm.variables.get('requestId'));
Post-response scripts run after the response is received. Use them to:
Write assertions and tests
Extract data from responses
Set variables based on response data
Log response information
Example:
Copy
Ask AI
// Test status codepm.test('Status code is 200', function() { pm.response.to.have.status(200);});// Extract token from responseconst jsonData = pm.response.json();pm.environment.set('authToken', jsonData.token);console.log('Token saved:', jsonData.token);
// Get a variableconst baseUrl = pm.environment.get('baseUrl');// Set a variablepm.environment.set('userId', '12345');// Check if variable existsif (pm.environment.has('apiKey')) { console.log('API key is configured');}// Unset a variablepm.environment.unset('tempValue');
Variables set via pm.environment.set() are automatically persisted to the active environment. New variables are created if they don’t exist.
Manage global variables (available across all environments):
Copy
Ask AI
// Get a global variableconst apiVersion = pm.globals.get('apiVersion');// Set a global variablepm.globals.set('lastRequestTime', Date.now());// Check if global existsif (pm.globals.has('orgId')) { console.log('Organization ID is set globally');}// Unset a global variablepm.globals.unset('tempGlobal');
Access variables from all scopes with automatic resolution:
Copy
Ask AI
// Get variable (resolves Environment → Collection → Global)const baseUrl = pm.variables.get('baseUrl');// Set variable in current environmentpm.variables.set('requestCount', 42);
pm.variables.get() follows the layered resolution order: Environment → Collection → Global. The narrowest scope wins.
Access response data (only available in post-response scripts):
Copy
Ask AI
// Get status codeconst status = pm.response.code;console.log('Status:', status);// Get response timeconst time = pm.response.responseTime;console.log('Response time:', time, 'ms');// Parse JSON responseconst jsonData = pm.response.json();console.log('User ID:', jsonData.id);// Get response body as textconst bodyText = pm.response.text();// Access response headersconst contentType = pm.response.headers.get('Content-Type');
// Generate timestampconst timestamp = Date.now();pm.environment.set('timestamp', timestamp);// Create signature (pseudo-code - use crypto library in real implementation)const message = pm.request.method + pm.request.url + timestamp;const signature = generateHMAC(message, pm.environment.get('secretKey'));pm.environment.set('signature', signature);console.log('Signature generated for timestamp:', timestamp);
Set dynamic headers
Copy
Ask AI
// Set request ID headerconst requestId = Math.random().toString(36).substring(7);pm.environment.set('requestId', requestId);// Set timestamppm.environment.set('timestamp', new Date().toISOString());console.log('Request ID:', requestId);
Conditional variable setting
Copy
Ask AI
// Set different base URLs based on timeconst hour = new Date().getHours();if (hour >= 9 && hour < 17) { pm.environment.set('baseUrl', 'https://api.example.com');} else { pm.environment.set('baseUrl', 'https://api-night.example.com');}console.log('Using base URL:', pm.environment.get('baseUrl'));
pm.test('Response time is acceptable', function() { pm.expect(pm.response.responseTime).to.be.below(500);});pm.test('Response size is reasonable', function() { const bodySize = pm.response.text().length; pm.expect(bodySize).to.be.below(100000); // 100KB});console.log('Response time:', pm.response.responseTime, 'ms');console.log('Response size:', pm.response.text().length, 'bytes');
Chain requests
Copy
Ask AI
pm.test('User created successfully', function() { pm.response.to.have.status(201); const jsonData = pm.response.json(); // Save user ID for next request pm.environment.set('userId', jsonData.id); console.log('User created with ID:', jsonData.id); console.log('Use {{userId}} in the next request');});
Scripts run in a Node.js VM sandbox with the following constraints:
2-second timeout - Scripts that exceed 2 seconds are automatically terminated
100KB size limit - Scripts larger than 100KB are rejected
200 console entries - Console output is capped at 200 entries
200 test results - Test results are capped at 200 tests
Scripts have no access to the file system, network (except via the current request), or VS Code APIs. They run in an isolated environment for security.