Getting started with Scripting in the Postman (original) (raw)

Last Updated : 28 May, 2026

Postman scripting allows you to add JavaScript code to automate API testing, handle dynamic data, and validate responses. It helps make API workflows more efficient and reusable.

Types of Scripts in Postman

Postman scripting is mainly divided into two types based on when they run in the request lifecycle.

Writing Scripts in Postman

Writing scripts in Postman is simple because it uses JavaScript. Pre-request scripts are written in the Pre-request Script tab, and test scripts are written in the Tests tab.

Variables in Postman Test Scripts

Postman has four types of variables:

**Pre request scripts examples: To access the request properties in postman you can pm.request

JavaScript `

// Set a custom header pm.request.headers.upsert({ key: 'Authorization', value: 'Bearer ' });

// Set a query parameter pm.request.url.addQueryParams([ { key: "page", value: "1" } ]);

// Generating fake data for request body let obj = { fname: pm.variables.replaceIn('{{$randomFirstName}}'), lname: pm.variables.replaceIn('{{$randomLastName}}'), country: pm.variables.replaceIn('{{$randomCountry}}') };

pm.request.body.raw = JSON.stringify(obj);

// Setting environment and collection variables pm.environment.set('id', 14234233); pm.collectionVariables.set('uid', 93782937);

// Getting variables (example usage) pm.environment.get('id'); pm.collectionVariables.get('uid');

// Sending a request pm.sendRequest({ url: 'https://api.example.com/some-endpoint', method: 'GET' }, function (err, res) { console.log(res ? res.json() : err); });

`

Test scripts examples

A test scripts can contain multiple pm.test() calls. Each call to pm.test() is for testing one parameter of the response.

**Validate response status code

JavaScript `

// Check if the response status code is 200 (OK) pm.test('Status code is 200', function () { // assertion to check if status code is 200 or not // if assertion is true then test passes else test fails pm.response.to.have.status(200); });

`

**Check response body JSON has a certain field

JavaScript `

// Parse the response JSON const responseBody = pm.response.json();

// Check if a property exists in the response pm.test('Response contains the "username" property', function () { pm.expect(responseBody).to.have.property('username'); });

`

**Response body JSON property value validation

JavaScript `

// Parse the response JSON const responseBody = pm.response.json();

// Check if a property has an expected value pm.test('Username is correct', function () { pm.expect(responseBody.username).to.eql('expected_username'); });

`

**Response time validation

JavaScript `

// Check if the response time is less than 500 ms pm.test('Response time is acceptable', function () { pm.expect(pm.response.responseTime).to.be.below(500); });

`

**Handling Authentication

JavaScript ``

// Obtain an access token from a previous response and set it as a variable const accessToken = pm.response.json().access_token; pm.environment.set('access_token', accessToken);

// Set the access token as an authorization header pm.request.headers.add({ key: 'Authorization', value: Bearer ${accessToken} });

``

There are many snippets available in Postman test scripts. They can be found on the right side of the test script editor.

Postman has built in functionality of generating fake data. You can know more about it here.

A simple test script in Postman

In Postman, you can write test scripts in the Tests tab to validate API responses. Below is a simple example for a GET request that checks status code and response body properties.

**Example: Method - GET

JavaScript `

// Check if the response status code is 200 (OK) pm.test("Status code is 200", function () { pm.response.to.have.status(200); });

// Parse the response JSON const jsonData = pm.response.json();

// Check if the response contains specific properties pm.test("Response contains userId", function () { pm.expect(jsonData.userId).to.exist; });

pm.test("Response contains id", function () { pm.expect(jsonData.id).to.exist; });

pm.test("Response contains title", function () { pm.expect(jsonData.title).to.exist; });

pm.test("Response contains body", function () { pm.expect(jsonData.body).to.exist; });

`

Now hit the send button and look the tests passing successfully in the result bar. At last, our efforts paid off.

test-results

Reasons to Use Scripting in Postman

Scripting in Postman enhances API testing and automation by adding dynamic and intelligent behavior to requests and responses.