Jon Gallant

Azure REST APIs with Postman (2019)

3 min read

This content is outdated

Please see the most up-to-date Azure REST APIs with Postman video and blog here:

This post will show you the fastest way to call the Azure REST APIs using Postman.

We’ll use:

  1. Postman
  2. Azure Cloud Shell - https://shell.azure.com

Create Postman Collection

Let’s create a Postman Collection, add a pre-request script, and set some variables.

  1. In Postman, click the “New” button in the upper left and select “Collection”.

000083

  1. Give it a name, but don’t click the “Create” button yet.

000098

Pre-Request Script

Postman allows you to assign a pre-request script to a collection, which is code that will run before every request. We’ll use that to generate a bearerToken that is required in the Authorization header of each Azure REST API request.

This script will do a POST to login.microsoftonline.com and put the response token in a global variable called bearerToken.

  1. Copy this code block into your collections pre-request script block.
pm.sendRequest({
url: 'https://login.microsoftonline.com/' + pm.variables.get("tenantId") + '/oauth2/token',
method: 'POST',
header: 'Content-Type: application/x-www-form-urlencoded',
body: {
mode: 'urlencoded',
urlencoded: [{key: "grant_type", value: "client_credentials", disabled: false},
{key: "client_id", value: pm.variables.get("clientId"), disabled: false},
{key: "client_secret", value: pm.variables.get("clientSecret"), disabled: false},
{key: "resource", value: pm.variables.get("resource"), disabled: false}]
}
}, function (err, res) {
pm.globals.set("bearerToken", res.json().access_token);
});
```sql
![000092](/images/azure-rest-apis-postman-in-no-time-flat/000092.png)
### Variables
Postman allows you to set variables at the collection level. We’ll put the variables required by the pre-request script and the Azure REST APIs in the “Variables” tab.
1. Create the following variables. We’ll add the variable values in the next section, so keep this form open.
> Make sure you add the variable values to the **CURRENT VALUE** column NOT the Initial Value column.
```bash
clientId
clientSecret
tenantId
subscriptionId
resource: https://management.azure.com/
```sql
![000102](/images/azure-rest-apis-postman-in-no-time-flat/000102.png)
Keep the “Create Collection” dialog open and continue to the next step.
## Get Azure Variables
1. Open Azure Cloud Shell - [https://shell.azure.com](https://shell.azure.com)
2. Create a Service Principal
Run the following command to create a service principal - which is a non-user account that can be used to call the Azure REST APIs.
> Make sure you change `sp1` with a unique name.
`az ad sp create-for-rbac -n "sp1" --role Contributor`
![000099](/images/azure-rest-apis-postman-in-no-time-flat/000099.png)
1. Copy the outputed variables to Postman Collection variables
```bash
clientId = appId
clientSecret = password
tenantId = tenant
```sql
![000100](/images/azure-rest-apis-postman-in-no-time-flat/000100.png)
1. Get Subscription Id
Run the following command to get your subscription Id.
`az account show --query id -otsv`
![000080](/images/azure-rest-apis-postman-in-no-time-flat/000080.png)
1. Copy the outputed Subscription Id to Postman Collection Variables tab
At this point your variables tab should look like this - with every variable filled out.
![000101](/images/azure-rest-apis-postman-in-no-time-flat/000101.png)
## Finish Creating Collection
1. Click the ‘Create’ Button on the Postman Collection form.
![000097](/images/azure-rest-apis-postman-in-no-time-flat/000097.png)
## Create Postman Request
1. Paste the following URI into the Postman Request URI field
`https://management.azure.com/subscriptions/{{subscriptionId}}/resourcegroups?api-version=2017-05-10`
1. Create an `Authorization` header and set value to `Bearer {{bearerToken}}`
2. Save the Request to the Postman Collection you created earlier.
![000088](/images/azure-rest-apis-postman-in-no-time-flat/000088.png)
The Postman Collection pane should now look like this:
![000094](/images/azure-rest-apis-postman-in-no-time-flat/000094.png)
## Execute Request
1. Click the ‘Send’ Button
2. Observe Request Output
You will see the REST request output in the bottom pane
```javascript
{
"value": [{
"id": "/subscriptions/f9766876-e50b-436f-9ad3-/resourceGroups/DefaultResourceGroup-EUS",
"name": "DefaultResourceGroup-EUS",
"location": "eastus",
"tags": {},
"properties": {
"provisioningState": "Succeeded"
}
}]
}

000089

And that is how to call the Azure REST APIs with Postman in no time flat!

Jon

Share:
Share on X