Skip to content

How to use an API with Postman?

Today, all web services are accessible through APIs. These are data providers that can be used with Postman. Postman is the most important tool in the world of web development. Postman is a reference tool that you can use for PHP, Node.js, Ruby on Rails or Python development. All you have to do is interact with a APIand that's it. How to use an API with Postman? Find out here.

turned on black Android smartphone

HTTP requests

Postman is a tool for making HTTP requests on a graphical interface. The latter makes the exercise much easier. All you have to do is enter the URL and the HTTP method. There are several HTTP methods that differ according to certain factors. These are the operation to be performed and the API used for it.

The most commonly used methods include:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

GET requests are those made by a browser when a URL is inserted in the navigation bar. Their purpose is to search for data or a page. As for POST requests, their purpose is to send the information contained in the body of the request. It is transmitted to the server. 

PUT requests are used to overwrite a resource with new data. They allow the data to be updated. As for PATCH requests, they are useful for updating a resource while modifying only the element sent in the body of the request.

Finally, the DELETE request is used to delete a resource. The uses of the requests vary depending on the server and how it was developed. These are very common methods for REST APIs. 

HTTP requests sometimes have queryams. For those who know, these are data sent to the server by the URL, usually in GET requests. It is easy to define and modify these query params directly in the Postman interface. With the tool, you can quickly modify the query params or assign them to variables.

It is possible for HTTP requests to contain request headers. As with query params, Postman makes it easy to modify headers via its graphical interface. 

The interest of some requests lies in the fact that they send data to the server thanks to the request body. These are POST, PUT and PATCH. The data in the body that must be sent to the server via Postman can have different formats. One of the most commonly used is the JSON format. For SOAP APIs, there is XML. 

With Postman, you can copy and paste a body into a text box. You can also choose the format you need. For a POST, PUT or PATCH request, the tool allows you to edit the request body. 

There are HTTP calls that require caller authentication. It is possible to set up the necessary authorisations for your calls. To do this, you need to access the Authorization tab in Postman. On the tool, you will find various forms of authentication such as the simple API key, Basic Auth or OAuth. 

The collections

As you develop, your API grows. In addition, the URLs to be tested by Postman increase. To help you organise and store requests, the tool provides you with collections. Thus, you can save and classify a series of HTTP requests.

How the calls are ordered in your collection is completely up to you. In addition, Postman offers you the possibility to automatically execute all calls in a collection in an order. It is therefore very important to define the order. 

Using variables

During your development, you will have calls that depend on variables from other requests. For example, you may need to retrieve an id or token from a first call in order to verify the second. 

To avoid copying and pasting every time you want to make a call, you can use Postman variables. These allow you to store data received from a request so that it can be reused in future calls. 

In addition, Postman variables are arranged in scopes. There are several of these, the main ones being: 

  • Global variable
  • Environment variable
  • collection variable
  • Local variable
  • given.

Variable scopes can be used in different collections. They can also be used to create environments with specific variables. 

In order to define a variable, Postman provides you with the pm object which contains the SET and GET functions. Depending on the scope of the variable to be defined, it is possible to use various formulas. 

Writing automated tests

In any API development, unit and functional tests should not be neglected. They ensure that the API works properly. Postman also provides you with a test function. They allow you to "mock" any call to an external API. 

For your backend development, the tests should cover some logic. For example, if you use the Twitter API, you don't have to actually make calls to that service every time you do integration tests. You'll need to mock those calls.

You can have a suite of tests in Postman that will be able to call on all external services. 

Building an API in Postman

In addition to the ability to manipulate and test an API, Postman also allows you to build one. To do this, you need to use its API Builder. The API tab available on the tool is the focal point of your API. It contains tests, collections and much more. 

With this tab you can centralise all resources related to your API, i.e. the documentation. When the schema of each resource is defined, Postman is able to generate the collections automatically. In addition, Postman offers a Mock Server to allow you to test a consumer of an API. 

Leave a Reply

Your email address will not be published. Required fields are marked *