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

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:

  • Postman Client
  • Azure CLI
    • Steps to install Azure CLI
  • Azure subscription
    • If you don’t already have one, you can sign up for anAzure free account.
    • For Students, you can use the freeAzure for Students offerwhich doesn’t require a credit card only your school email.

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.

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

  • 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]"

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

  • 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:

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

  • 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 = AppIDclientSecret = PasswordtenantId = tenantresource = 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:

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

  • 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:

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

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”.

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

  • 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.

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

You should be seeing this output:

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

  • 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.

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

References

Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 6247

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.