What is the scripting in Postman, and what languages are supported?
Last Updated :
25 Jul, 2024
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. It can make various types of HTTP requests(GET, POST, PUT, PATCH), save environments for later use, and convert the API to code for various languages(like JavaScript, and Python). In this article, we will learn What is the scripting in Postman, and what languages are supported.
Prerequisites
What is the scripting in Postman?
Scripts are the pieces of code that can be executed at a specific point of time in the Postman testing environment. There are two types of scripts -
- Pre-request Scripts: As the name suggests, these scripts are used to execute JavaScript before the request runs.
- Test Scripts: These scripts are executed during the testing environment.
Languages Supported?
Postman's runtime is based on Node JS and lets you add dynamic behavior to requests and collections. You can use pre-request and test scripts in various languages like Javascript/NodeJS. Postman provides JavaScript APIs that you can use in your request scripts. The pm object provides functionality for testing your request and response data, with the postman object providing additional workflow control.
Use of scripting languages
1. Automated Testing: Postman enables you to write check scripts that execute after sending an API request. JavaScript, being the primary scripting language supported through Postman, enables the creation of computerized checks to affirm API responses, data integrity, and server conduct.
2. Response Validation: JavaScript scripting in Postman is extensively used to validate responses from API endpoints. Test scripts can parse JSON or XML responses, extract specific information, and carry out assertions to ensure the correctness of the received facts.
3. Environment and Data Manipulation: Postman enables using variables and environments. JavaScript lets in manipulation of those variables, permitting customers to set, update, or extract values dynamically. This is specially useful for managing authentication tokens, dynamic endpoints, and statistics-driven attempting out.
4. Custom Workflow and Logic: With JavaScript, users can introduce custom good judgment and workflows into their exams. This consists of conditional statements, loops, and custom abilties, permitting complex test situations that mimic real-worldwide use times.
Execution order of Scripts
The sequence is Pre-request Script, Request, and then Tests. This order ensures that any necessary modifications to the request are made before it is sent, and validations are performed after receiving the response.
1. Pre-request Script: This script runs before the actual request is sent. It allows you to modify the request before it is sent, such as setting variables or headers.
2. Request: The actual HTTP request is executed.
3. Tests: After the request is sent and a response is received, the test script is executed. This script is used to validate the response, perform assertions, and capture data for future requests.
Post-Request Scripts
In postman, we can write test scripts to test API requests in Javascript. These are called Post-request scripts. Test script is used to test whether your API is working accordingly or not, to establish that integrations between the services are functioning properly, and to check that new developments have not affected any functionality of existing requests.
Steps to Execute Post-Request Scripts in Postman:
Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like "GFG". Here, we can see, multiple tabs like Authorization, Pre-request scripts, Tests , Variables.

Step 2: Click on Tests. Now in the right side pane, we can multiple snippets arrived. We can use any of the available snippet or create our own code based on our requirement. Code is written in Javascript.

We will write 2 snippets in Javascript from the already available snippets from the right-hand side pane.
First Script : This will check whether the response time is less then 200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Second Script: This will check whether status code is 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Step 3: Click on Save
Step 4: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on "Add new request"

Step 5: Now You can simply paste the API in the space provided and select the API type you are requesting from the dropdown like GET,POST, PUT, DELETE etc. Output will be shown in the body with the status code.
API Used:
https://jsonplaceholder.typicode.com/posts/1
Step 6: You can see the Post-execution scripts results in tab Test Results; as shown below

Output
Pre-request Scripts:
A pre-request script associated with a request will execute before the request is sent. You can write pre-request scripts for your Postman API requests in JavaScript. The Pre request tab allows for any pre- request processing before a request is sent.
Steps to use Javascript with pre-request scripts
Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”. Here, we can see, multiple tabs like Authorization, Pre-request scripts, Tests , Variables.

Step 2: Click on Pre-request scripts. Now in the right side pane, we can multiple snippets arrived. We can use any of the available snippet or create our own code based on our requirement. Code is written in Javascript.

Step 3: We will add a pre-request script that will generate random numbers. We will use those random numbers to set a variable ‘id’ that is already stored as a global variable in our environment as shown below

Pre-request Script:
var random=Math.floor(Math.random()*10);
pm.variables.set('id',random)
Step 4: Click on Save
Step 5: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on “Add new request”

Step 6: Now You can simply paste the API in the space provided and select the API type you are requesting from the dropdown like GET,POST, PUT, DELETE etc. Output will be shown in the body with the status code. We use global variable in our request using {{id}}
API USED:
https://reqres.in/api/user/{{id}}
Output:
Similar Reads
Getting started with Scripting in the Postman
Postman is a powerful tool for testing and automating APIs, and one of its most valuable features is the ability to write and execute scripts. With scripting in Postman, you can add dynamic behavior, automate repetitive tasks, and extract data from responses. In this article, we'll go through the ba
6 min read
Explain the use of scripting languages like JavaScript in Postman tests.
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see the use of scripting languages like JavaScript in Postman tests. Table of Content What are scripts?Use of scripting languagesSteps to use JavaScript with Po
3 min read
What is Json Schema and How to Validate it with Postman Scripting?
JSON stands for JavaScript Object Notation, is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is often used to transmit data between a server and a web application. It supports data structures like arrays and objects an
4 min read
What are Scripting Languages?
All the scripting languages are programming languages. It is a type of programming language in which there is no compilation and the execution takes place line by line. Generally, a programming language would be first compiled and then executed, but in a scripting language, the program will be execu
5 min read
Assertions in Postman and How to Use that in Scripting Window
Assertions are checks or validations we can include in our API requests to ensure that the response from the server meets certain criteria. These criteria could be based on the response's status code, headers, body content, or any other aspect of the response. Postman provides a variety of built-in
5 min read
What are pre-request scripts in Postman, and how are they used?
Postman is an API(application programming interface) development tool which helps to build, test and modify APIs. It has the ability to make various types of HTTP requests(GET, POST, PUT, PATCH), save environments for later use, converting the API to code for various languages(like JavaScript, Pytho
2 min read
What are Postman tests, and how to write them?
Postman is a API development and testing tool, which provides a feature called tests. These tests is used to automate the validation of API responses. Table of Content What are Postman Tests?Key Features of Postman TestsWriting Postman TestsWhat are Postman Tests?Postman tests are scripts written in
2 min read
What are authentication methods supported in Postman?
An API platform called Postman is used to create and use APIs. With Postman, you can design better APIs more quickly by streamlining collaboration and simplifying each step of the API lifecycle. Authentication in Postman verifies a user's identification. It includes sending a validated username and
4 min read
What are Postman Collections, and how do they organize requests?
Postman is a popular tool for working with APIs. It's user-friendly and has a cool feature called Collections. In this article, we will talk about what Collections are and how they help you organize and manage your API requests efficiently. PrerequisitesDownload and install PostmanBasic knowledge of
2 min read
What are Request Parameters in Postman ?
Postman, a powerful tool for API testing, offers many features that make it easier to test, share, and document APIs. One of the crucial aspects of API testing using Postman involves handling request parameters. In this article, we will see what request parameters are, how to send them using Postman
4 min read