Setting up Azure API on Postman and Azure CLI – Step-by-step guide

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.

Setting up Azure API on Postman and Azure CLI – Step-by-step guide

Dive into the World of Azure APIs on Postman - Step-by-Step Guide by Suzaril Shah

Hello tech enthusiasts! I'm Suzaril Shah, a Gold Microsoft Learn Student Ambassador, here to guide you through the exciting process of setting up Azure API on Postman and Azure CLI. Whether you're a student or a seasoned developer, our comprehensive guide is designed to enhance your skills in managing Azure resources effectively.

What You'll Learn:

  • Efficient Setup: Begin with the basics as we walk you through the Azure CLI setup and login process, crucial for interacting with Azure from your command line.
  • Hands-On Experience: Create and configure your Azure service principal credentials and dive into real-world API testing scenarios using Postman.
  • Practical Insights: Gain practical insights on how to leverage Azure APIs for managing resources, understanding CLI toolsets (Azure, Azure Developer, GitHub CLI’s), and much more.

Join us to not only enhance your technical skills but also to prepare yourself for a future in cloud services and infrastructure management. Let’s explore the vast capabilities of Azure together! In this step-by-step guide, I will guide you through setting up Azure API on Postman and Azure CLI.

Prerequisites:

Step 1 - Getting Started

  • On Azure CLI, run the “az login” command. You will be redirected to log in to your Azure account in a web browser, and upon successful login, you will be presented with your account detail, as shown below. Please take note of the “id” variable, as we need them later. The ‘id’ variable is our subscription ID on Azure.

suzarilshah_0-1715363478284.png

  • Select the subscription for the Azure Account using the az account set command. Use the -n parameter to specify the subscription name, i.e: az account set -n "MSDN Platforms Subscription"
  • After that, create a resource group using CLI using the command "az group create --location [Azure Location, i.e: westus] --resource-group [Resource Group]"

suzarilshah_1-1715363478288.png

  • Next, create a service principal credential on Azure using this command: 

 

az ad sp create-for-rbac -n [SP_Name] --role Owner --scope "/subscriptions/[Subscription_ID]/resourceGroups/[ Resource Group]"

 

The output should look like this:

suzarilshah_2-1715363478291.png

  • This command will provide the credentials we need to work on Postman to test some Azure API:
    • AppID
    • displayname
    • Password
    • Tenant
  • Copy the credentials to somewhere safe. Please do not expose the credentials! You can also explore other roles when creating a service principal by using the --role flag and specify the scope of the SP credentials with the --scope flag. Documentations included here.
  • Some built-in roles in Azure RBAC include
    • Owner - Total control of a Resource Group
    • Contributor - Has control over Actions on a Resource Group, like modifying a Resource Group (i.e. Deleting a VM) but cannot assign permission to the RG.
    • Reader - only has the ability to view the resource group. Learn More


Step 2 - Rocking with Postman! 

  • Create a new Collection on your current Workspace and Click on the collection name. Under the collection name, you should find "Variables" tab. Create variables as listed below and map the values from the Service Principle and Subscription ID output from earlier.

 

ClientId = AppID clientSecret = Password tenantId = tenant resource = https://management.azure.com/ subscriptionId = [Subscription ID] resourceGroup = [Resource Group] bearerToken = "leave it blank, we will programmatically fill the field later"

 

  • The configuration should look like this:

suzarilshah_3-1715363478292.png

  • Click on "Save" and head to the Pre-request Script Tab and copy and paste the script below:

 

pm.test("Check for collectionVariables", function () { let vars = ['clientId', 'clientSecret', 'tenantId', 'subscriptionId']; vars.forEach(function (item, index, array) { console.log(item, index); pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined; pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty; }); if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) { pm.sendRequest({ url: 'https://login.microsoftonline.com/' + pm.collectionVariables.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.collectionVariables.get("clientId"), disabled: false }, { key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false }, { key: "resource", value: pm.collectionVariables.get("resource") || "https://management.azure.com/", disabled: false } ] } }, function (err, res) { if (err) { console.log(err); } else { let resJson = res.json(); pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_on); pm.collectionVariables.set("bearerToken", resJson.access_token); } }); } });

 

  • This script will get the bearer Token use to authenticate to access Azure API and programmatically populate the token into the bearerToken variable we created earlier. Click on "Run" button to run the script. You should see the bearerToken is already generated and placed in the "Current Value" fields on the "Variable" tab.
  • Head to the Authorization tab, make sure to select the Authorization method to "Bearer Token", and set the token to the variable as displayed below:

suzarilshah_4-1715363478297.png

 

Step 3 – Testing Phase!

  • Right-Click on the ‘Collection’ name, and click on the “Add Request” option. Name the Request as “Get Resource Group Info”.

suzarilshah_5-1715363478298.png

  • On the Request Type, select “GET” request, and using the variable setup on the Collection folder, type:

 

{{resource}}/subscriptions/{{subscriptionId}}/resourcegroups/{{resourceGroup}}?api-version=2020-09-01

 

This GET request will fetch information about the specified “Resource Group” on Azure. Click on the “Save” and “Send” buttons.

suzarilshah_6-1715363478303.png

You should be seeing this output:

suzarilshah_7-1715363478304.png

  • Voila! You have successfully set up Azure API authentication and performed an Azure API GET Request on Postman!

Create API Requests on the Collection.

You can explore the list of Azure API from the documentation 

 

What’s next?

To kickstart your Azure API journey, you can find the Azure Cloud Onboarding collection on Postman that I have been working on from this postman collection.

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.