This is a beginner's introduction to HTTP APIs in Postman. Complete this entire course to get started on your API journey with Postman. Spelling and capitalization matter, so make sure to follow the instructions precisely.
People communicate with each other through natural language. Software communicates via application programming interfaces (APIs). Web APIs operate behind the scenes, enabling you to browse your favorite social network's updates or turn on a smartlight. This is done through an API client, which does the sending, retrieving, or rendering of data.
A web browser is an example of an API client. When you log in to an online store, the browser passes along your login credentials to a server, and upon confirmation that you're an authorized user, displays a history of your orders. Other examples of API clients include mobile applications, developer terminals, and tools like Postman.
Postman is an API platform used by over 20 million people. While Postman does much more, it's most frequently used as an API client. Developers use Postman to send API calls and inspect what comes back. Nowadays people in nontechnical roles are using APIs too, and they're discovering that a graphical user interface allows them to very easily interact with APIs.
In this module, you explore HTTP APIs using Postman as an API client. To save your work, sign up for a free Postman account and log in. If you already have an account, just ensure that your profile is set to public, then move to the next section.
https://identity.getpostman.com/signup
to create a new Postman account.In the next unit, you navigate the essentials of the Postman API client, including setting up a workspace and sending API calls.
After completing this unit, you'll be able to:
You can set up workspaces to organize your work in Postman, at various levels of privacy. Personal workspaces are visible only to you. Team workspaces can be used to collaborate with others on a project.
For this module, set up a public workspace, which enables you to collaborate with anyone around the world.
My First Workspace
.To use Postman in a web browser, as we do in this module, you're prompted to download and run the Postman desktop agent on your local machine to overcome cross-origin resource sharing (CORS) limitations. Follow the prompts to set up the Postman desktop agent. Check out the documentation for more information about the agent.
In this unit, let's use Postman as an API client to send and retrieve data from NASA. You can manually build an API call in Postman. But for this module, let's import some data.
curl --location --request GET 'https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY'
Now that you've imported a request into your Postman workspace, take a look at the various parts, including your HTTP method and request, as well as query parameters.
Click the Send button and inspect the response received from the server. In one place, you'll find the response Body (1), Headers (2), Status (3), Time (4), and Size (5).
Let's update your parameters.
KEY: date
VALUE: 2022-11-30
Review the NASA Astronomy Picture of the Day API documentation to see what other parameters are supported by the API.
Once your API call is working the way you want it, you can generate code to copy and paste into your own client codebase. The code snippet generated by Postman allows you to make the same API call in the language of your choice.
Highlight the request. Expand the context bar on the right, and click the Code icon. In the dropdown, select a programming language and framework like JavaScript - Fetch.
If the generated code isn't formatted the way you like, additional settings can be tweaked under the gear icon. You can also make inline edits to the code snippet. When you're ready, this is code that can be copied and pasted into your own applications.
In the next unit, let's test in Postman to assert what we're getting back from the server is what we expect.
After completing this unit, you'll be able to:
You can write Postman tests in JavaScript to ensure an API is functioning as expected. Under the Tests tab of your request, you can input JavaScript manually or use the Snippets to the right of the code editor. Clicking one of these popular use cases automatically inserts a bit of code into the editor for you to modify.
A Postman test consists of the pm.test()
function, which accepts:
true
or false
) value to indicate whether the test passes or failsIn our example, the name of our test is "Status code is 200". The function contains an assertion about our response, that we will receive an HTTP status code of 200
, formatted using Chai BDD syntax.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Since tests are assertions about a server response, tests execute after you run the request and receive a response from the API.
200
HTTP status code, the test will pass, otherwise it will fail. Try changing the expected status code in your test script to 404
, and run the request again. You'll see that the Test Result gives you a fail and error message. Revert your test to be 200
for it to pass.The History tab on the left sidebar maintains a record of the most recent requests you send from Postman. Combing through your history to find and reuse requests can be time-consuming. Instead, you can save a collection of requests to organize your work. Besides improving productivity, collections are the foundation of more advanced features in Postman, like automation, monitoring, and mock servers.
+
. This should be the request you made before adding a date parameter.Intro to HTTP APIs Module
.+
. The destination should already be Intro to HTTP APIs Module
. Go ahead and click Save.Update the name of the first call to Picture of the Day and the name of the second call to Pictures from November 2022.
Intro to HTTP APIs Module
.Picture of the Day
.Pictures from November 2022
. Ensure that the request retrieves all pictures from that month.Edit the second call to retrieve all the images from the month of November in 2022.
KEY: start_date
VALUE : 2022-11-01
KEY: end_date
VALUE : 2022-11-30
Check out the NASA Astronomy Picture of the Day API documentation for more information about its query parameters.
DRY stands for don't repeat yourself. Imagine you always expect JSON objects to be returned from all your endpoints. Instead of adding the same test to every request individually, you can add the test assertion at the collection level to run after every request in the collection. With Postman, you can add tests to individual requests, folders, or collections.
Intro to HTTP APIs Module
.Intro to HTTP APIs Module
and click the more actions icon.In the next unit, let's learn about another way to stay DRY, using variables to reduce redundant code.
After completing this unit, you'll be able to:
In programming, variables allow you to store and reuse values throughout your code. Doing this allows you to reference the value throughout your collections, environments, and requests in Postman. And if you need to update the value, you only have to change it in one place.
There are a variety of variable scopes suited to different tasks in Postman.
Variable | Scope | When they're used |
Global variables | Access data between Postman requests, collections, scripts, and environments—all within a single workspace | Used commonly, but may not change frequently, like a |
Collection variables | Access data throughout requests within a collection | Limited to a specific collection |
Environment variables | Access data to be used in a single collection at a time, but can be used portably with other collections | Configuration data for server environments like a URL; for example, |
Local variables | Access within a single request or collection run | Temporary values that do not persist once execution has ended |
Data variables | Access from external CSV or JSON file during a collection run via Newman or the Collection Runner | Loop through hundreds of test scenarios |
If a variable with the same name is declared in two different scopes, the value stored in the variable with the narrowest scope will be used. For example, if there is a global and a local variable both called username
, the local value will be used when the request runs.
In text areas of the Postman request builder, like the request URL or parameters, you can use double curly braces to reference variables. Let's learn a shortcut for creating new variables at any scope.
DEMO_KEY
.nasa_api_key
.You can also define and edit variables by navigating directly to the variable editors.
new_variable
.NEW_KEY
. You see that the CURRENT VALUE cell populates automatically.Instead of manually editing variables, you can also define and reference variables programmatically using JavaScript in request scripts under the Pre-request Script and Tests tabs.
Intro to HTTP APIs Module
collection, click the Pre-request Script tab.Postman stores variables as strings. If you're storing objects or arrays, remember to JSON.stringify()
the value before storing, and JSON.parse()
when you retrieve it.
Now that you know how to define and reference variables programmatically in Postman, you can chain requests. In other words, extract data from one response to use in other requests by using variables.
Add a third request to your collection called "Retrieve image" to make a GET request to a request URL of {{nasa_image_url}}
.
Retrieve image
.{{nasa_image_url}}
. This variable doesn't mean anything at the moment. You'll change that in a few steps.Pictures from November 2022
.pm.collectionVariables.set("nasa_image_url", pm.response.json()[0].url)
in the editor. This script defines the variable nasa_image_url
once you get a response from the NASA server.We are using the pm.collectionVariables.set()
function to set a collection variable. The function accepts two parameters. The first parameter is the variable key as a string, like nasa_image_url. The second parameter is the variable value. In this example, we are accessing the response data with the pm.response
object.
Take a look at the collection variable editor to see that Postman has set the new variable. In this case, it's the first image URL from the array of Pictures from November 2022
.
Retrieve image
.{{nasa_image_url}}
and see that your new variable now works.There was a lot of information in this unit, but working with variables is an important skill. Using variables increases your ability to work efficiently and minimizes the likelihood of error. You just learned that!
In the next unit, let's dig deeper into authorizing our API calls.
After completing this unit, you'll be able to:
APIs may require authorization to ensure that client requests access data securely. This can involve authenticating the sender of a request and verifying that they have permission to access relevant data. The authorization model is specified by the API provider.
You can pass auth details along with any request you send from Postman. Auth data can be included in a request header, body, or as parameters to a request.
The NASA Astronomy Picture of the Day API requires an API key be included with each request as a query parameter. Let's see this in action.
api_key
query parameter.Go ahead and deselect the api_key
query parameter in Pictures from November 2022
as well.
Make sure you Save your update.
Let's learn a new way to handle authorization in Postman.
Under the Authorization tab of the request builder, you can enter and configure your auth details for Postman to automatically populate the relevant parts of the request. You can use an authorization helper for a request, folder, or collection.
Since all of the requests in the Intro to HTTP APIs module collection require the same authorization type, let's add an authorization helper to the collection.
Key: api_key
Value: {{nasa_api_key}}
Add to
settings to Query Params.Remember to use variables and collections to define authorization details more safely and efficiently, letting you reuse the same information in multiple places. If you group your requests in collections and folders, you can specify auth details to reuse throughout a group.
In the next unit, let's learn some ways to debug and troubleshoot when things aren't going as expected.
After completing this unit, you'll be able to:
The Postman console can help with debugging when an API is not behaving as you expect.
Open the console from the icon on the bottom left of Postman [alt: Console]. Every request sent from Postman is logged in the console in both its raw and pretty form. In the console, you can inspect headers, certificates, requests, and responses from these network calls. Let's see it in action. Send the second request in your collection and inspect the console output.
Using log statements at appropriate locations in your scripts helps identify the source of any issues. You can verify code is executing in the right order and inspect the values of variables at a certain moment in time.
For example, we previously extracted data from a response to pass along information to another request as a collection variable. We can log response data to inspect the data type and ensure that we are parsing an object properly.
console.log(`From the Tests tab, number of pictures is: ${pm.response.json().length}`)
Send the request and inspect the console output. You'll see the statement response.
You've gone through a lot in this module, from setting up your Postman workspace to testing, variables, and debugging. Congratulations!
To continue your journey with Postman, here are some additional resources to check out.