Calling Azure REST API via curl

A straightforward post to invoke Azure REST API via simple HTTP calls

Davide Mauri
May 4, 2018 · 4 min read

In these days I needed to call Azure REST API directly, without having the possibility to use some nice wrapper like AZ CLI or .NET SDK or Python SDK or whatever, since the REST API I needed to call was not included in any of the mentioned tools.

Make sure you check out the latest updates at the bottom of the article!

I decided to use curl since it is one of the easiest way to issue HTTP requests. But it turned out to be a little more complex that I what I could have expected at the beginning, especially while dealing with the authentication phase. The entire process is pretty simple as you’ll see, documentation is just a bit scattered all it may be difficult to quickly understand the path you must follow to get everything working nicely.

Azure API security, and thus authentication (which is based on OAuth2) is a pretty broad topic as you can see from the long documentation available here:

I read throughout all the documentation, hyperlinks included and at the end I was still confused. There are so many options and each one have quite a number of prerequisites that requires even more reading. So, for my future reference and for all those who just need a straightforward way to solve the problem, here’s the list of all steps required.

Create a Service Principal

In order to access resources a Service Principal needs to be created in your Tenant. It is really convenient to do it via AZ CLI:

for much more details and options see the documentation:

What is happening here is that you’re registering your application in order to be able to be recognized by Azure (more precisely: from the AD tenant that is taking care of your subscription). Exactly like when you register your application to access Twitter or Facebook in order to be able to read and write posts/tweets/user data and so on.

Request the Access Token

As said before authentication used the OAuth2 protocol, and this means that we have to obtain a token in order to authenticate all subsequent request. We need to use the client_credential flow:

all the three required information:

  • APP_ID

can be obtained from the previous step. You already have the since you used it to create the Service Principal. The and the will be returned by the command you executed before. Otherwise you can execute the following command to find it the tenant id:

And the following to get the :

The result of the curl call will be an Authorization Token that looks like the following:

Call Azure REST API

The obtained token that needs to be used in the Authorization HTTP header as the to make sure your HTTP call will be authorized:

And that’s it. Is really easy at the end. And once you have the token it is also easy to use it in your preferred REST client tool, be it Postman or Insomnia.

If you want learn more on how to use the OAuth2 authentication protocol to access Azure, just go here:

July 2019 — Update 1: Just three steps

If you need a token just to run some test and you don’t want to go through Service Principal creation I just discovered that now you can just do

and you’re good to go, you’ll get your access token with a maximum validity of 1 hour, which is more than enough to do tests. Using curl is really easy now:

Step 1 : Get Your Subscription Id

Step 2: Get The Token

Step 3: Invoke curl

July 2019 — Update 2: Just ONE step

In the latest version of AZ (2.0.67, at moment of writing) there is new command that allows to call any azure REST API with just one command:

the will be automatically replaced with your active subscription Id. Great! Can’t be easier that this now :)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store