pm API. Write JavaScript code that runs before sending a request or after receiving a response to automate workflows, set variables dynamically, and validate responses.
Overview
Scripts enable advanced automation and testing workflows:- Pre-request scripts: Run before a request is sent (set dynamic headers, compute signatures, etc.)
- Post-response scripts: Run after receiving a response (validate data, extract values, run tests)
Scripts run in a sandboxed Node.js
vm environment with a 2-second timeout and 100KB size limit.The pm Object
Thepm object provides the scripting API. It’s available globally in all scripts.
pm.environment
Access and modify environment variables.Get the value of an environment variable.Signature:
pm.environment.get(name: string): string | undefinedSet or create an environment variable. Changes are persisted to storage.Signature:
pm.environment.set(name: string, value: string): voidIf the variable doesn’t exist, it will be created automatically in the current environment.
Remove an environment variable.Signature:
pm.environment.unset(name: string): voidpm.globals
Access and modify global variables (shared across all environments).Get the value of a global variable.Signature:
pm.globals.get(name: string): string | undefinedSet or create a global variable. Changes are persisted to storage.Signature:
pm.globals.set(name: string, value: string): voidRemove a global variable.Signature:
pm.globals.unset(name: string): voidpm.variables
Read-only access to resolved variables (combines globals, collection, and environment).Get the resolved value of a variable following precedence order.Signature:
pm.variables.get(name: string): string | undefinedResolution order: Globals → Collection → Environment (narrowest wins)Use
pm.variables.get() when you don’t care which scope the variable comes from. Use pm.environment.get() or pm.globals.get() when you need a specific scope.pm.request
Access request data (available in both pre-request and post-response scripts).The request URL (after variable substitution).
The HTTP method (GET, POST, PUT, DELETE, etc.).
Request headers collection.Methods:
get(name: string): string | undefined- Get header valueadd(name: string, value: string): void- Add headerremove(name: string): void- Remove header
Request body content.Methods:
raw()- Get body as stringjson()- Parse body as JSON (throws if invalid)
pm.response
Access response data (only available in post-response scripts).HTTP status code.
HTTP status text (e.g., “OK”, “Not Found”).
Response headers collection.Methods:
get(name: string): string | undefined- Get header valuehas(name: string): boolean- Check if header exists
Raw response body as string.
Parse response body as JSON.Signature:
pm.response.json(): anyThrows an error if the response body is not valid JSON.
Response time in milliseconds.
pm.test()
Define a test assertion (post-response scripts only).Create a named test with an assertion function.Signature: Test results are displayed in the “Tests” tab of the response panel.
pm.test(name: string, fn: () => void): voidpm.expect()
ChaiJS-style assertions for testing.Create an assertion using Chai’s expect syntax.Signature:
pm.expect(actual): AssertionCommon assertions:- Equality
- Type Checking
- Comparison
- String Matching
- Array/Object
console
Log messages and debugging output.Log informational messages.Signature:
console.log(...args: any[]): voidLog error messages.Signature:
console.error(...args: any[]): voidLog warning messages.Signature:
console.warn(...args: any[]): voidConsole output is displayed in the “Tests” tab with a 200-entry limit. Logs are cleared between requests.
Script Examples
Pre-Request Script Examples
- Dynamic Headers
- Token Refresh
- Request Counter
Add computed headers before sending the request:
Post-Response Script Examples
- Extract Token
- Response Validation
- Extract & Chain
- Error Handling
Extract authentication token from response:
Script Limits
Scripts run in a sandboxed environment with the following limits:- Timeout: 2 seconds maximum execution time
- Size: 100KB maximum script size
- Console output: 200 entries maximum
- Test results: 200 tests maximum
- No external modules: Cannot use
require()orimport - No file system access: Cannot read/write files
- No network access: Cannot make HTTP requests (except via
pm.sendRequest()- coming soon)
Scripts that exceed the timeout or size limit will be terminated and an error will be displayed in the Tests tab.
Postman Compatibility
LiteClient’s scripting API is compatible with Postman Collection v2.1:- Import: Pre-request and test scripts are preserved when importing Postman collections
- Export: Scripts are included when exporting to Postman format
- Limitations: Collection-level and folder-level scripts are not supported (request-level only)
When importing a collection with collection-level or folder-level scripts, a warning will be displayed. You’ll need to manually copy those scripts to individual requests.
Best Practices
Script Organization
- Keep scripts focused on a single responsibility
- Use comments to explain complex logic
- Extract common logic to environment variables (as JSON strings)
- Use meaningful test names that describe what’s being validated
Performance
- Avoid expensive operations in pre-request scripts (they delay request sending)
- Cache computed values in variables instead of recalculating
- Use
console.log()sparingly in production scripts
Error Handling
- Always wrap JSON parsing in try-catch blocks
- Check for variable existence before using them
- Provide descriptive error messages in console output
- Use tests to validate assumptions about response structure
Variable Management
- Use globals for values shared across all requests (base URLs, API keys)
- Use environment variables for values specific to an environment (dev/prod tokens)
- Use descriptive variable names (avoid single letters)
- Clean up temporary variables with
unset()when done