Sitemap
Benjamin Aguilar

Benjamin Aguilar

I’m a software developer with more than 10 years of working for companies located in different countries using Java, Python, Django, PHP, Laravel.

Postman & Variables

5 min readMay 15, 2022

Postman is an API platform for building and using APIs.. blah blah.. If you are here it’s very likely you already know what it is for, so, let’s not waste time.

When using Postman to try and test any RestAPI endpoint we have a really cool stuff that can simplify our tasks, that really cool stuff is the ability to use variables. The most basic example is when you need to send a request to get an authentication token to send it in another endpoint request.

We’re going to learn how to automatically store that token value into a variable, which will be used in all other requests we want without needing to manually copy and paste the token every time we need it. In order to use variables, we need to know some important concepts like workspace, environment, collections, etc.

Let’s see a quick explanation of those concepts:

  • Workspace: is a place where all your operations on an API will occur. There, you can declare your global variables such as URL, token’s id and password, context keys, API keys, document keys, etc.
    It will depend on your needs, you can configure workspaces for projects separately.
  • Environment: is a set of variables you can use in your Postman requests. You can use environments to group related sets of values together and manage access to shared Postman data if you are working as part of a team.
  • Collection: is a repository where you save your bulk requests. Basically, a collection is a group of Postman requests.
  • Variable: like any other programming language, are nothing but placeholders to hold some value or result of some expressions.

Example

OK, that was too much talking, let’s get to the point. We’re going to do a basic example of how to use all these concepts.

  1. Let’s create our workspace (if we don’t have one already), by clicking the Workspace option at the top and then clicking the Create Workspace button.

2. Put a name and summary of the workspace you are creating. After that select the visibility you want for this workspace, in this example we’ll select Personal, as we want it just for our personal use. And finally, click the Create Workspace button.

3. Now, let’s create our environment, you can have as many environments as you want in your environment. This can be done by clicking the Environment button and then clicking the + button.

Put a name and that’s it, you have your first environment. Remember this is the place where our variables exit.

You can have different environments with different variables if you want.

4. Now, we are ready to start creating our requests, which can be stored in collections. Let’s create our collection for “Document Download” where we’re going to have all related endpoints. To create it we click the Collections button and then click the + button.

In order to try our configuration, we’re going to create some example endpoints using https://beeceptor.com/

5. Create our first request where we’re going to get a token, which is going to be used in another request.

In this case, the header parameters are just for demo.

Get Benjamin Aguilar’s stories in your inbox

Join Medium for free to get updates from this writer.

6. Let’s save our request into our collection by clicking the Save button above the URL field.

Press enter or click to view image in full size

You can put a name and select the collection we previously created. Then click Save.

7. We’re ready to hit the Send button to get the token we’ll use in another request. You should get something like this:

Press enter or click to view image in full size

8. This step is really important for the variables to properly work. Select the environment that you want to use for these requests, this is done by clicking in the environment selection dropdown at the top right.

Press enter or click to view image in full size

We’re going to use our DevEnv.

9. Next step is to create the variable in our environment by either, selecting edit on the Environment quick look button:

Or by clicking the Environments button in the left vertical bar and clicking the environment in the list:

We should get the following screen where we’re going to create our variable:

Press enter or click to view image in full size

Let’s give it a name and that should be enough.

Press enter or click to view image in full size

Make sure to hit the Save button after creating it.

10. After that, we can go back to our request window and select the token value we got in the response, then right-click it to get a menu and select the variable where we’re going to store our value.

Note: for this menu to show up with the Set: option you need to previously select an environment and create the variable on it as we did in step 9.

11. Let’s create another request where we’re going to use the variable we just created as a header parameter. To add it we type the key and then in the value we type { and select the variable we created.

Press enter or click to view image in full size

This is how it should look:

Press enter or click to view image in full size

Remember to hit the Save button to add it to our collection.

And that’s it. Whenever our token expires we can just hit the send button in the Authentication request and then the other request will automatically get the new token.

Thanks for reading!

Code happy!

--

--

Benjamin Aguilar
Benjamin Aguilar

Written by Benjamin Aguilar

I’m a software developer with more than 10 years of working for companies located in different countries using Java, Python, Django, PHP, Laravel.