How to use JavaScript in Postman ?
Last Updated :
31 Jul, 2024
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how to use JavaScript in Postman.
Prerequisites:
We will discuss the following two methods to use JavaScript in Postman.
Postman provides a feature for writing JavaScript code for requests, collections, and pre-request or post-request scripts. This helps you to manipulate request and response data, set dynamic variables, perform validations, and so on.
Steps to use JavaScript with Post-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 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
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
How to use Native JavaScript Promises in Postman ? Postman is a popular tool used by developers for testing APIs. While Postman primarily offers a graphical user interface (GUI) for making HTTP requests, it also supports scripting capabilities using JavaScript. One common use case for scripting in Postman is making asynchronous requests and handling
3 min read
How to Run JavaScript in Visual Studio? To run JavaScript in Visual Studio, you can either use Node.js in the Terminal or the Code Runner extension. Both methods allow you to execute JavaScript code easily and efficiently within the Visual Studio environment.Using Node.js in TerminalNode.js is a JavaScript runtime that allows you to execu
2 min read
How to Count Records in JSON array using JavaScript and Postman ? In this article, we are going to explore how JavaScript and Postman are used to count the number of records returned in a JSON array. Send a Request: Send a request to fetch the JSON array from your API endpoint in Postman.Access JSON Response: In the Postman test scripts tab, you can access the JSO
2 min read
How to Pass variables to JavaScript in Express JS ? Express is a lightweight framework that sits on top of Node.jsâs web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. When working with Express.js you may encounter scenarios where you
2 min read
How to Use API Keys authentication in Postman Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how to use API Keys authentication in Postman. The API key is a unique identifier that authenticates requests and if several users are there, their username
2 min read
How to pass parameters in Postman requests? 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
2 min read