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)

FAQs

How to run Azure API in Postman? ›

Connecting to Azure API Management
  1. Select APIs in the sidebar and select an API.
  2. Select Deployments, and then select Microsoft Azure.
  3. You'll be prompted to authorize Postman to access your Microsoft Azure account. ...
  4. Enter information about the Azure API Management service you want to connect to your API:
Sep 15, 2022

How to setup Azure API? ›

Steps to set up Azure API Management
  1. Step 1: Sign in to the Azure Portal. ...
  2. Step 2: Create an API Management Instance. ...
  3. Step 3: Configure API Management Instance. ...
  4. Step 4: Create or Import APIs. ...
  5. Step 5: Configure API Policies. ...
  6. Step 6: Configure API Products. ...
  7. Step 7: Publish APIs. ...
  8. Step 8: Monitor and Manage APIs.
May 17, 2024

How to use Azure DevOps Rest API Postman? ›

The high level steps to generate an access_token to authorize your REST API calls from Postman to Azure are:
  1. Register your app in Azure.
  2. Generate an auth code in any web browser.
  3. Create your access_token by sending a POST to Azure's /oauth2/token endpoint with data generated from the first two steps.

How to call rest API in Azure CLI? ›

To invoke a custom Azure REST API request with Azure CLI, use the az rest command, followed by the --url parameter. The --url parameter defines the URL of the requested resource, and is the only required parameter. The az rest command automatically authenticates using the logged-in credential.

How do I make REST API calls in Postman? ›

Create an API in Postman
  1. From the Postman sidebar, select APIs.
  2. Select +.
  3. Enter your API's name.
  4. Choose whether to connect a repository or continue without one. To learn more about repositories and version control, see Manage your API changes using version control.
  5. Define or import a definition:

How do I deploy API in Postman? ›

To connect to an API gateway in Postman, select APIs in the sidebar and select an API. Select Deployments, then select Apigee X, AWS API Gateway, or Microsoft Azure. Enter the required information to configure the API gateway integration.

How do I call Azure functions from Postman? ›

Configure Postman
  1. Open Postman and create a new request.
  2. Select the POST method.
  3. Enter the URL of your Azure Function. ...
  4. Select the Body tab and select raw and JSON from the dropdown. ...
  5. Select the Headers tab and add the following headers: ...
  6. Select the Send button to trigger the event.
Jan 17, 2024

How to make Azure API calls? ›

To make a REST API call to Azure, you first need to obtain an access token. Include this access token in the headers of your Azure REST API calls using the "Authorization" header and setting the value to "Bearer {access-token}".

What is the difference between CLI and API? ›

A CLI is typically an unfiltered mainline, for better or for worse, with both an exceptionally low barrier of entry and an extremely steep learning curve. APIs, on the other hand, can be equally powerful but often form a type of layer between the requester and the underlying system.

How should you complete the Azure CLI command? ›

This article introduces the CLI and helps you complete common tasks.
  1. Install or run in Azure Cloud Shell. ...
  2. Sign into the Azure CLI. ...
  3. Find commands. ...
  4. Find published samples and articles. ...
  5. Use tab completion. ...
  6. Be aware of globally available arguments. ...
  7. Use interactive mode. ...
  8. Learn Azure CLI basics with quickstarts and tutorials.
Mar 7, 2024

How do I run Azure function in Postman? ›

Configure Postman
  1. Open Postman and create a new request.
  2. Select the POST method.
  3. Enter the URL of your Azure Function. ...
  4. Select the Body tab and select raw and JSON from the dropdown. ...
  5. Select the Headers tab and add the following headers: ...
  6. Select the Send button to trigger the event.
Jan 17, 2024

How do I run an API file in Postman? ›

Create an API in Postman
  1. From the Postman sidebar, select APIs.
  2. Select +.
  3. Enter your API's name.
  4. Choose whether to connect a repository or continue without one. To learn more about repositories and version control, see Manage your API changes using version control.
  5. Define or import a definition:

How to call rest API from Azure? ›

To make a REST API call to Azure, you first need to obtain an access token. Include this access token in the headers of your Azure REST API calls using the "Authorization" header and setting the value to "Bearer {access-token}".

Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5845

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.