Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Submit a query request to a workspace by using the Azure Monitor Log Analytics endpoint https://api.loganalytics.azure.com. To access the endpoint, you must authenticate through Microsoft Entra ID.
Note
The api.loganalytics.io endpoint is being replaced by api.loganalytics.azure.com. The api.loganalytics.io endpoint will continue to be supported for the foreseeable future.
Authenticate with a demo API key
To quickly explore the API without Microsoft Entra authentication, use the demonstration workspace with sample data, which supports API key authentication.
To authenticate and run queries against the sample workspace, use DEMO_WORKSPACE as the {workspace-id} and pass in the API key DEMO_KEY.
If either the Application ID or the API key is incorrect, the API service returns a 403 (Forbidden) error.
The API key DEMO_KEY can be passed in three different ways, depending on whether you want to use a header, the URL, or basic authentication:
- Custom header: Provide the API key in the custom header
X-Api-Key. - Query parameter: Provide the API key in the URL parameter
api_key. - Basic authentication: Provide the API key as either username or password. If you provide both, the API key must be in the username.
This example uses the workspace ID and API key in the header:
POST https://api.loganalytics.azure.com/v1/workspaces/DEMO_WORKSPACE/query
X-Api-Key: DEMO_KEY
Content-Type: application/json
{
"query": "AzureActivity | summarize count() by Category"
}
Public API endpoint
The public API endpoint is:
https://api.loganalytics.azure.com/{api-version}/workspaces/{workspaceId}
Where:
- api-version: The API version. The current version is
v1. - workspaceId: Your workspace ID.
Note
The beta api-version has been deprecated. For more information, see Migrate from using batch and beta query APIs.
The query is passed in the request body.
For example:
https://api.loganalytics.azure.com/v1/workspaces/aaaabbbb-0000-cccc-1111-dddd2222eeee
Body:
{
"query": "Usage"
}
Set up authentication
To access the API, you need the right authentication token.
On the app's overview page, select API permissions.
Select Add a permission.
On the APIs my organization uses tab, search for Log Analytics and select Log Analytics API from the list.
Select Delegated permissions.
Select the Data.Read checkbox.
Select Add permissions.
Now that your app is registered and has permissions to use the API, grant your app access to your Log Analytics workspace.
From your Log Analytics workspace overview page, select Access control (IAM).
Select Add role assignment.
Select the Reader role and then select Members.
On the Members tab, choose Select members.
Enter the name of your app in the Select box.
Select your app and choose Select.
Select Review + assign.
After you finish the Active Directory setup and workspace permissions, request an authorization token.
Note
For this example, we applied the Reader role. This role is one of many built-in roles and might include more permissions than you require. More granular roles and permissions can be created. For more information, see Manage access to Log Analytics workspaces.
Request an authorization token
Before you begin, make sure you have the values the request requires. The exact set depends on the authentication method you chose in Authenticate to Azure Monitor APIs. A managed identity needs none of them, because the platform supplies the token.
For an app registration, all requests require:
- Your Microsoft Entra tenant ID.
- Your workspace ID.
- Your Microsoft Entra client ID for the app.
- The app's credential, preferably a certificate rather than a client secret.
The Log Analytics API supports these Microsoft Entra ID OAuth 2.0 flows:
- Client credentials, for daemons and background services
- Authorization code with PKCE, for apps that act on behalf of a signed-in user
The implicit grant flow is deprecated and isn't recommended for new work. Use authorization code with PKCE instead.
Client credentials flow
In the client credentials flow, you authenticate against the Log Analytics endpoint by using a token. A single request receives a token by using the identity you set up in Authenticate to Azure Monitor APIs.
Use https://api.loganalytics.azure.com as the token audience.
Get an authentication token by using any of the following methods. The Azure CLI and the Azure SDKs acquire and refresh tokens for you, so prefer them over a raw token request.
Get a token using Azure CLI
Use the az account get-access-token command, passing the token audience for the API you're calling.
# Set variables
tokenAudience="<TokenAudience>"
# Get an access token for the audience
az account get-access-token \
--scope "$tokenAudience/.default" \
--query accessToken \
--output tsv
Use the token in requests to the Log Analytics endpoint:
POST /v1/workspaces/your workspace id/query?timespan=P1D
Host: https://api.loganalytics.azure.com
Content-Type: application/json
Authorization: Bearer <your access token>
Body:
{
"query": "AzureActivity |summarize count() by Category"
}
Example response:
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "OperationName",
"type": "string"
},
{
"name": "Level",
"type": "string"
},
{
"name": "ActivityStatus",
"type": "string"
}
],
"rows": [
[
"Metric Alert",
"Informational",
"Resolved",
...
],
...
]
},
...
]
}
Authorization code flow
The main OAuth2 flow supported is through authorization codes. This method requires two HTTP requests to acquire a token with which to call the Azure Monitor Log Analytics API. There are two URLs, with one endpoint per request. Their formats are described in the following sections.
Authorization code URL (GET request)
GET https://login.microsoftonline.com/YOUR_Azure AD_TENANT/oauth2/authorize?
client_id=<app-client-id>
&response_type=code
&redirect_uri=<app-redirect-uri>
&resource=https://api.loganalytics.io
When a request is made to the authorize URL, the client_id is the application ID from your Microsoft Entra app, copied from the app's properties menu. The redirect_uri is the homepage/login URL from the same Microsoft Entra app. When a request is successful, this endpoint redirects you to the sign-in page you provided at sign-up with the authorization code appended to the URL. See the following example:
http://<app-client-id>/?code=AUTHORIZATION_CODE&session_state=STATE_GUID
At this point, you've obtained an authorization code, which you need now to request an access token.
Authorization code token URL (POST request)
POST /YOUR_Azure AD_TENANT/oauth2/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&client_id=<app client id>
&code=<auth code fom GET request>
&redirect_uri=<app-client-id>
&resource=https://api.loganalytics.io
&client_secret=<app-client-secret>
Use the same values as before, with some additions. The authorization code is the code you receive in the previous request after a successful redirect. Combine the code with the key you get from the Microsoft Entra app. If you don't save the key, delete it and create a new one from the keys tab of the Microsoft Entra app menu. The response is a JSON string that contains the token with the following schema. Types are indicated for the token values.
Example response:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJ.....Ax",
"expires_in": "3600",
"ext_expires_in": "1503641912",
"id_token": "not_needed_for_log_analytics",
"not_before": "1503638012",
"refresh_token": "eyJ0esdfiJKV1ljhgYF.....Az",
"resource": "https://api.loganalytics.io",
"scope": "Data.Read",
"token_type": "bearer"
}
The access token portion of this response is what you present to the Log Analytics API in the Authorization: Bearer header. Use the refresh token in the future to acquire a new access token and refresh token when yours go stale. For this request, use the following format and endpoint:
POST /YOUR_AAD_TENANT/oauth2/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
client_id=<app-client-id>
&refresh_token=<refresh-token>
&grant_type=refresh_token
&resource=https://api.loganalytics.io
&client_secret=<app-client-secret>
Example response:
{
"token_type": "Bearer",
"expires_in": "3600",
"expires_on": "1460404526",
"resource": "https://api.loganalytics.io",
"access_token": "eyJ0eXAiOiJKV1QiLCJ.....Ax",
"refresh_token": "eyJ0esdfiJKV1ljhgYF.....Az"
}
Client credentials flow
The Log Analytics API supports the OAuth2 client credentials flow. This flow is suitable for service-to-service calls where no user interaction is required.
Client credentials token request
POST https://login.microsoftonline.com/YOUR_AAD_TENANT/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
client_id=<app-client-id>
&scope=https://api.loganalytics.io/.default
&client_secret=<app-client-secret>
&grant_type=client_credentials
Example response:
{
"token_type": "Bearer",
"expires_in": 3599,
"access_token": "eyJ0eXAiOiJKV1QiLCJ.....Ax"
}
This access_token can be used as the Authorization: Bearer header value when it's passed to the Log Analytics API to authorize requests.
More information
Find documentation about OAuth2 with Microsoft Entra: