Postman scripting allows you to customize API requests and automate testing workflows using JavaScript. Scripts can generate dynamic data, manage variables, validate responses, and connect multiple API requests.
- JavaScript is the primary scripting language used for Postman scripts.
- Scripts can run before a request or after a response is received.
- Postman provides built-in APIs, such as the pm object, for interacting with requests, responses, variables, and tests.
Postman Scripting Environment
Postman provides a JavaScript-based scripting environment for executing scripts during the API request lifecycle. It includes built-in APIs that simplify interaction with request and response data.
The pm object provides methods for working with Postman data and functionality.
console.log(pm.response.json());
Postman scripts can access variables, inspect responses, create assertions, and perform other operations using the available pm APIs.
Postman Scripting Execution Flow
Postman executes JavaScript-based scripts at different stages of the request lifecycle. These scripts can prepare request data before sending the request and validate or process the response after it is received.

- Pre-request Scripts run before the API request is sent.
- Post-response Scripts run after the API response is received.
- Values generated or extracted by scripts can be used in subsequent requests.
Pre-request Scripts
A Pre-request Script runs before an API request is sent. Postman uses JavaScript for Pre-request Scripts, which can generate dynamic values, set variables, and prepare data required for request execution.
Steps to Use JavaScript with Pre-request Scripts
Step 1. Create an API Request: Open Postman and create a new HTTP request. Select the required HTTP method, such as GET, POST, PUT, or DELETE, and enter the API endpoint.
Step 2. Add a Pre-request Script: Open the Scripts section and select Pre-request. You can write JavaScript code manually or use one of the available snippets.

Step 3. Generate a Dynamic Value: The following script generates a random number between 0 and 9 and stores it in a variable named id.

const random = Math.floor(Math.random() * 10);
pm.variables.set("id", random);
Step 4. Use the Variable in the Request: The id variable can be referenced in the request using Postman's variable syntax:
Example: https://reqres.in/api/users/{{id}}
Step 5. Send the Request: Click Send. Postman executes the Pre-request Script first and replaces {{id}} with the generated value before sending the request.
Example: if the generated value is 7:
https://reqres.in/api/users/7
This allows dynamic values to be used in API requests without hardcoding them.
Post-response Scripts
Post-response scripts in Postman are JavaScript-based scripts that run after an API response is received. They are used to validate response data, perform assertions, extract values, and verify API behavior.
Steps to Execute Post-response Scripts in Postman
Step 1. Create an API Request: Open Postman and create a new HTTP request. Select the required method, such as GET, POST, PUT, or DELETE, and enter the API endpoint.
Step 2. Add a Post-response Script: Open the Scripts section and select Post-response. You can write JavaScript code manually or use the available code snippets.
Example: To check whether the response time is less than 200 milliseconds:
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
To verify that the API returns a status code of 200:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Step 3. Save the Request: Click Save to save the request and its associated Post-response Script.
Step 5. Send the Request: Click Send to execute the API request. Postman sends the request and receives the response from the server.
Example: https://jsonplaceholder.typicode.com/posts/1
Step 5. View the Test Results: After receiving the response, Postman executes the Post-response Script. The results of the pm.test() assertions are displayed in the Test Results section.
A successful assertion is shown as PASS, while a failed assertion is shown as FAIL.
Troubleshooting Postman Scripts
Postman scripts may fail because of syntax errors, incorrect variable scopes, unexpected API responses, or incorrect script placement. The following checks can help identify and resolve common scripting issues.
- Check the Postman Console for errors, logs, and debugging information.
- Verify that variable names, scopes, and values are correct.
- Ensure the script is added under the correct Pre-request or Post-response section.
- Use console.log() to inspect variables and response data.
- Check the API response and status code when a test assertion fails.
Best Practices for Postman Scripting
Following good scripting practices makes Postman scripts easier to maintain, debug, and reuse across API testing workflows.
- Keep scripts short, focused, and relevant to the request.
- Use meaningful names for variables, tests, and functions.
- Avoid hardcoding values; use Postman variables for dynamic data.
- Add clear assertions to validate important API behavior.
- Use console.log() for debugging instead of unnecessary output.
- Reuse common logic through collection-level scripts when appropriate.
- Handle missing or unexpected response data to prevent script failures.
Limitations of Postman Scripting
Postman scripting is useful for API automation and validation, but it has some limitations that can affect complex testing workflows.
- Scripts are primarily written in JavaScript and depend on Postman’s scripting environment.
- Complex scripts can become difficult to maintain and debug.
- Scripts have access to a limited runtime compared with a full Node.js application.
- Managing large amounts of test data or complex workflows can become cumbersome.
- Script execution can be affected by Postman-specific APIs and variable scopes.