Leveraging the power of Postman for API Development
Tags: api, postman, restful, graphql
Published: 2020-05-20 03:23:35

While developing API, either RESTful or GraphQL or SOAP, testing and debugging are crucial. But creating a form to consume those API, just for the sake of debugging, is not very efficient and effective. Hence, some kind of API client, capable of consuming or querying GET/POST or any other kind of HTTP request to the endpoint, is required.

Among the vast selection of GUI tools available for this purpose, I find  [Postman](https://www.getpostman.com/)  quite easy and really powerful. Here, I will list some of the advanced features of Postman to increase productivity as an API developer.

# Postman Collections

Every time we send a request to some API endpoint, the request information (its URL, request parameters and response data ) is stored in the History Tab of the postman. It makes it easier to re-send the same request from history later as required. But as the number of requests increases, finding a particular request in the history tab will be time-consuming.

Postman Collections are a group of saved requests, organized in folders and sub-folders structure. This way we can manage API requests. Here is  [a guide](https://learning.getpostman.com/docs/postman/launching-postman/creating-the-first-collection/)  on how we can create postman collections.

Besides this, there are other benefits to creating postman collections.

## Sharing a Postman Collection

Postman collections we created can be shared with other teammates and people who are concerned with our API.

![Sharing and publishing postman collection.](https://dev-to-uploads.s3.amazonaws.com/i/ext6bkwg0jaw1hitcc4w.png)

**Sharing with Teams and other workspaces**

Our workspaces can be shared with other teammates by inviting them in the workspace or by creating another common workspace. This way, the collections will always stay in sync for all teammates.

**Sharing as a JSON file or URL**

If you do not want people to add in your team, but still need to share postman collections, you can export collections as a JSON file or generate a URL of JSON data which afterward can be imported in postman as a collection.

[Here](https://learning.getpostman.com/docs/postman/collections/sharing-collections/)  is a more detailed guide to share a postman collection.

## Creating and Publishing API Documentation

Postman generates and hosts browser-based API documentation from the collection which can be shared to privately as well publicly. This API documentation is beautifully formatted on a web page which can reduce extra efforts to document API separately. More about API documentation can be read from  [here](https://learning.getpostman.com/docs/postman/api-documentation/intro-to-api-documentation/).

---

# Environment and Global Variables

![Using the Postman variable for dynamic URL.](https://dev-to-uploads.s3.amazonaws.com/i/s4ikzspd2885lrsmfbcn.png)

Variables in Postman are static identifiers used to store dynamic value to be used in requests and responses of API calls. A straightforward use-case of the variable is to define an API base URL for local, staging, and production servers. This way, we do not need to create three different API requests or change the URL manually. As shown in the figure above, we can use  `{{url}}`, a postman variable storing different URLs for different environments.

Another useful use-case of the variable is to store the user authentication token. Storing user token in variable enables us to request another request by automatically sending an updated token from the variable.

To store authentication token from response to an environment variable  `token`, the following scripts should be written in  `Tests`  tab of API request panels.

```javascript
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);
```

More about the environment and global variables are in  [postman’s documentation](https://learning.getpostman.com/docs/postman/environments-and-globals/intro-to-environments-and-globals/).

---

# Test Scripts

A very cool and useful feature of Postman is Test Scripts where we can write test cases for every API request. Postman supports javascript, hence we can use any javascript test library (such as jasmine, mocha, etc.) or postman’s native test library.

![Test scripts in postman](https://dev-to-uploads.s3.amazonaws.com/i/yoihe0dbzhjqfypq2a39.png)

A sample test scripts using the  [PostmanBDD library](https://documenter.getpostman.com/view/220187/postman-bdd-examples/6Z3uY71?version=latest). Here, I stored PostmanBDD in a global variable with key  `postmanBDD`. Below the Test Scripts to test a typical API response with  [Chai assertions](https://www.chaijs.com/api/assert/).

```javascript
var jsonData = JSON.parse(responseBody);
eval(globals.postmanBDD);
describe("response", () => {
    
    it("should return a 200 response", () => {
        response.should.have.status(200);
    });
    
    it("should have the json content type header", () => {
        response.should.have.header('content-type', 'application/json');
        response.should.be.json;
    });
    
    it('should return keys: title, content, id', () => {
        assert.containsAllKeys(jsonData,['title', 'content', 'id']);
    });
    
    it("'title' key should be provided title", () => {
        jsonData.title.should.be.an('string').and.equal(environment.create_title);
    });
    
    it("'content' key should be as provided", () => {
        jsonData.content.should.be.an('string').and.equal(environment.create_content);
    });
    
    it("should have 'id' property", () => {
        jsonData.id.should.be.an('number');
    });
    
});
```

This way we can test our API response to avoid manual testing. Test results can be seen in the Response panels of Postman. Read more at  [documentation](https://learning.getpostman.com/docs/postman/scripts/test-scripts/).

---

# Collection Runner

A collection runner is another useful feature of Postman. This allows us to run a group of API requests together within a collection with just a single click. This is useful when we have test scripts written and want to automate API testing. This will save a lot of time testing each and every request one by one.  [Read more](https://learning.getpostman.com/docs/postman/collection-runs/intro-to-collection-runs/).

![postman runner](https://dev-to-uploads.s3.amazonaws.com/i/gar9lwiagxxaiz2ngd4t.png)

![Collection runner in postman](https://dev-to-uploads.s3.amazonaws.com/i/m9f0rsj8cq8osvstezld.png)

---

# Postman monitoring

[Postman monitoring](https://learning.getpostman.com/docs/postman/monitors/intro-monitors/)  is another cool feature of the postman, which runs a collection just like postman runner does but periodically to check for its performance and response. We can set up a monitor to run as frequently as 5 minutes to check if all the requests in your collection are up and healthy.

Monitoring is priced per request made with some limited number of free monitoring calls per month.

---

# Mock servers

[Postman’s mock servers](https://learning.getpostman.com/docs/postman/mock-servers/intro-to-mock-servers/)  is a feature, especially useful for API consumer (front enders) to simulate each endpoint and its corresponding response and work with API before back enders provide actual API. Not just front-enders, API developers can also use mock servers to design and plan their API before diving into the actual development.

---

For more tips and tricks about Postman, follow these links

-   [https://blog.getpostman.com/](https://blog.getpostman.com/)

---

That's all about Postman. If you are using it in your API development share how are you using it? If you have any interesting tips and tricks about the postman, do share :) 

Also if you have any other alternatives, share that as well in the comment.

---

This article was originally published at [YoungInnovations Blog](https://blog.yipl.com.np/leveraging-the-power-of-postman-for-api-development-daaa64be9e49)