Skip to main content

How to Get an Access Token for Your Own API with the Azure CLI

· 10 min read
Jagdish Kumawat
Founder @ Dewiride

An API protected by Microsoft Entra ID answers 401 Unauthorized to anyone without an access token. While you build it, you need a real token to test it, and the Azure CLI you're already signed in to can get one in one command. This guide shows the command, what it returns, and how to send the token with a request.

Syntax​

Syntax
az account get-access-token --scope <application-id-uri>/<scope-name> [--tenant <tenant-id>] [--query <field>] [--output <format>]
PartWhat to put there
--scopeThe full name of the permission the token is for: your API's Application ID URI, a slash, and a scope the API defines, such as api://22222222-2222-2222-2222-222222222222/access_as_user. Without --scope, the token is for Azure Resource Manager, not your API
--tenantOptional. The ID of the tenant to get the token from, when you can sign in to more than one
--queryOptional. Picks fields from the answer. accessToken keeps just the token
--outputOptional. tsv prints a single value without quotes, ready for a variable

The command signs in as you: the token lets an app call the API with your identity. The Azure CLI also has --resource, the older form, which takes the Application ID URI alone: --resource "api://<client ID>". For the API on this page, both forms gave a token with the same audience and the same scope, access_as_user.

Prerequisites​

  • The Azure CLI, signed in to your API's tenant (az login).
  • Your API trusts the Azure CLI for the scope: it lists the Azure CLI's client ID, 04b07795-8ddb-461a-bbee-02f9e1bf7b46, under Authorized client applications on its Expose an API page. Without that, the command fails with AADSTS9002313 (see Troubleshooting).

The examples on this page use an API registered as Dewiride Service Desk API, with the scope access_as_user.

Step 1: Find Your API's Client ID​

The scope starts with api:// and your API's client ID. Rather than pasting it, look it up by the registration's name:

Terminal
API_CLIENT_ID=$(az ad app list --display-name "Dewiride Service Desk API" --query "[0].appId" --output tsv)

Step 2: Get a Token​

Ask for a token for the scope:

Terminal
az account get-access-token --scope "api://$API_CLIENT_ID/access_as_user"
Output
{
"accessToken": "eyJ0eXAiOiJKV1QiLC…",
"expiresOn": "2026-11-19 19:43:39.000000",
"expires_on": 1795097619,
"subscription": "00000000-0000-0000-0000-000000000000",
"tenant": "11111111-1111-1111-1111-111111111111",
"tokenType": "Bearer"
}

The real accessToken is about 2,200 characters long; here it's cut short on purpose. The other fields:

FieldWhat it is
accessTokenThe token itself
expiresOnWhen it expires, in your computer's time zone: this one, 19:43:39, is 14:13:39 UTC on a computer set to India Standard Time
expires_onThe same moment as a Unix timestamp, seconds since 1 January 1970 UTC
subscriptionThe Azure subscription the CLI has selected. The token has nothing to do with it
tenantThe tenant that issued the token
tokenTypeBearer: whoever holds the token can use it, which is why it goes in an Authorization: Bearer header
The token is a key to the API

Anyone who has the token can call the API as you until it expires. Never paste it into a chat, a ticket or a screenshot, and don't print it where others can see your screen.

Step 3: Keep the Token and Send It​

To use the token, keep only the token in a variable and put it in an Authorization: Bearer header. The examples send it to a small ASP.NET Core API on http://localhost:5301 that answers its /hello request only for valid tokens (How to Protect an ASP.NET Core Minimal API with Microsoft.Identity.Web):

Terminal
TOKEN=$(az account get-access-token --scope "api://$API_CLIENT_ID/access_as_user" --query accessToken --output tsv)
curl -i -H "Authorization: Bearer $TOKEN" http://localhost:5301/hello
Response
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Thu, 19 Nov 2026 13:37:24 GMT
Server: Kestrel
Transfer-Encoding: chunked

Hello, user@example.com!

The API read the signed-in person's name from the token. curl -i prints the response's headers before its body; in PowerShell, curl.exe makes sure you get curl and not a PowerShell command of the same name. The same request without the header gets nothing:

Terminal
curl -i http://localhost:5301/hello
Response
HTTP/1.1 401 Unauthorized
Content-Length: 0
Date: Thu, 19 Nov 2026 13:37:24 GMT
Server: Kestrel
WWW-Authenticate: Bearer

macOS-style terminal window running three commands: looking up the API's client ID by name, getting a token for api://$API_CLIENT_ID/access_as_user into the TOKEN variable, and curl with an Authorization Bearer header to http://localhost:5301/hello, which answers HTTP/1.1 200 OK and Hello, user@example.com!

For a .http file instead of curl, write the token into a .env file the editor reads (How to Send a Bearer Token from a .http File in VS Code and Visual Studio).

What the Token Says​

An access token is three Base64 parts joined by dots, and its middle part holds claims about who it's for and what it allows. Decoded, the token from this page carried:

ClaimValueMeaning
audapi://22222222-2222-2222-2222-222222222222The audience: the API it's for. Your API refuses tokens for any other audience
scpaccess_as_userThe scopes it grants
appid04b07795-8ddb-461a-bbee-02f9e1bf7b46The app that asked for it: the Azure CLI
ver1.0The token format version. The API's registration decides it, not the command

The token is valid for a limited time: Microsoft gives access tokens a random lifetime of 60 to 90 minutes, 75 on average. The Azure CLI keeps tokens in a cache, so it doesn't always ask Microsoft for a new one: the token it returned at 13:27 UTC had been issued for an earlier command, more than twenty minutes before. When the API starts answering 401 again, run the token command again.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out --scopeThe token is for Azure Resource Manager, and the API answers 401 with The audience '(null)' is invalid in its WWW-Authenticate headerAlways pass your API's full scope
Passing the scope name alone, access_as_userThe request doesn't name your APIPass the full scope, with api://<client ID>/ in front
Printing the token or pasting it somewhereAnyone who sees it can call the API as you until it expiresKeep it in a variable or a git-ignored file
Reading expiresOn as UTCYou expect the token to expire hours earlier or later than it doesexpiresOn is your computer's local time; expires_on is the exact moment

Troubleshooting​

SymptomLikely causeFix
AADSTS9002313: Invalid request. Request is malformed or invalid.Run the command again with --debug: AADSTS65001: The user or administrator has not consented to use the application … 'Microsoft Azure CLI' means the API doesn't trust the Azure CLI for that scopePre-authorize the Azure CLI for the scope (How to Pre-Authorize a Client Application for Your API in Microsoft Entra ID)
The same AADSTS9002313 although the Azure CLI is pre-authorizedThe scope in the command is misspelled or isn't the authorized one: a made-up access_as_admin failed exactly this wayCopy the scope from the API's Expose an API page
AADSTS9002313 for every token, even for AzureYour Azure CLI sign-in itself needs renewingFixed: AADSTS9002313 in Azure CLI
401 with The token expired at '…'The token's lifetime is overGet a new token
az ad app list returns nothingThe CLI is signed in to another tenant, or the name is differentaz account show --query tenantId; check the registration's name

FAQ​

Do I need a client secret to get the token?​

No. The Azure CLI signs in as you, with its own registration, the one Microsoft publishes for it. Your API only needs to trust that registration.

Can my app use the Azure CLI's token in production?​

No. The Azure CLI is a developer tool. An app gets its own tokens: a single-page app through MSAL, for example (How to Get an Access Token with MSAL React).

Why is the token a version 1.0 token when I asked with a v2.0 scope?​

The API's registration chooses the version of the tokens issued for it, not the app that asks. A new registration gets version 1.0 tokens unless its settings say otherwise, and Microsoft.Identity.Web accepts both.

Conclusion​

az account get-access-token --scope api://<client ID>/<scope> gets a token for your own API as you, once the API exposes the scope and trusts the Azure CLI. Keep just the token with --query accessToken --output tsv, send it in an Authorization: Bearer header, and get a fresh one when the API answers 401 again. When the command fails with AADSTS9002313, --debug shows the real cause.

Additional Resources​