How to create Scripts?
Let’s get our hands dirty and write the pre-request script.
‘https://simple-books-api.glitch.me’ is a Simple Book API which allows you to reserve books.
In this example we will be using ‘https://simple-books-api.glitch.me/books?type=fiction’ request, which is getting all the fiction books.
🚀 To write pre-request scripts go to the pre-request tab which is below the GET box.

🚀 Let’s display a message 'Hey, Ninja Coder! You are running pre-request script' before running any script.
🚀 To do this write a pre-request script -
console.log('Hey, Ninja Coder! You are running pre-request script') in your pre-request tab.

🚀 Now, open your console, which is at the bottom left of your screen.

🚀Click on send.

✅ In the console screen you can see that the pre-request script was executed before the GET request.
Yay! 🎊 You just learned how to use pre-request scripts.
🚀 Just like pre-request scripts you can create test scripts by writing the code in the ‘Tests’ tab.

🚀 Open the console and then send a request ‘https://simple-books-api.glitch.me/books?type=fiction’

✅ The console screen shows that the pre-request script was added before the request was sent while the Test script was added after the request was sent.
🎊 You just learned how to use Test Scripts.
Execution order of Scripts
As mentioned before, there can be individual scripts as well as scripts in a collection.

In postman, for a single request script, the execution order looks like this-
⭐ Pre-request script associated with a request will execute before the request is
sent.
⭐ A test script associated with a request will execute after the request is sent.
In the collection, the request script execution order looks like this-
⭐ A pre-request script associated with a collection will run prior to every request in
the collection.
⭐ A pre-request script associated with a folder will run prior to every direct child
request in the folder.
⭐ A test script associated with a collection will run after every request in collection.
⭐ A test script associated with a folder will run after every direct child request in the
folder.

Frequently asked questions
For what purpose is Postman used?
Postman is an API client that makes it easy for developers to create, share, test and document APIs. Users are allowed to create and save simple and complex HTTP/s requests, as well as read their responses.
What is GET request in Postman?
A GET request gets the information from the server. The server responds to the GET request without affecting any data on the server, that is, there is no creation, updation, addition, or deletion of data on the server when you are making a GET request.
What is an endpoint in Postman?
An API endpoint is a point at which an API connects with the software program. APIs work by sending requests for information from a web application or web server and receiving a response.
Conclusion
We are so proud of you for finishing this article🤩. In this article, we discussed what are scripts. The two types of scripts are pre-request and test scripts. Then we discussed how to create these scripts by taking an example of both pre-request and test scripts.
After reading this article, if you are interested in knowing more about Postman, we recommend you to read some of our articles -
🔥 Web Testing
🔥 API
🔥 API Testing
You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, System Design, and many more!
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!
We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.