Accessing the Analytics API through Azure API Management

Modified on Wed, 25 Nov at 1:35 PM

This guide shows how to publish the Analytics API through Azure API Management (APIM), so that your data platform and other consumers call APIM and APIM calls Analytics with its own managed identity. There are no secrets to store or rotate, and Analytics itself does not change. It is for the Azure, APIM and Entra administrators who set up the integration, and assumes you have an APIM instance and permission to assign app roles in Entra.


How it works

Consumer ──(APIM subscription key or Entra token)──▶ APIM
APIM ──(Entra token for APIM's own managed identity, role DataReader)──▶ Analytics API
  1. The consumer authenticates to APIM in whatever way you already use for APIM: a subscription key, Entra (validate-jwt), or both.
  2. APIM's authentication-managed-identity policy gets a token for APIM's own managed identity, for the Analytics API's Application ID URI. APIM caches it and renews it before it expires.
  3. APIM replaces the Authorization header with that token and forwards the call.
  4. Analytics validates the token and checks the role in it, exactly as for any other application.

This is additive. Analytics keeps accepting the web application's user sign-in, the IDE and SchedEx connectors, and any service tokens you use today. Nothing else changes when you add APIM.

Two consequences to plan for:

  • Everyone APIM lets through acts as APIM. Analytics sees one application, with one role. If different consumers need different access (one read-only, one that imports), publish them as separate APIM APIs, each with its own user-assigned managed identity and its own role.
  • APIM appears in Analytics as a user without a name. Analytics records each caller the first time it is seen. A managed identity has no display name, so anything APIM does, such as imports with DataWriter, shows a blank user.

Before you start

Check How
The Analytics API app registration and its Application ID URI Entra ID → App registrations → the Analytics API → Expose an API. This is azureAd.audience in your Analytics deployment, normally api://<analytics-api-client-id>.
DataReader and DataWriter accept applications az ad app show --id <analytics-api-client-id> --query "appRoles[].{value:value, types:join(',',allowedMemberTypes), id:id}" -o table. Both must list Application. Administrator is users only and must stay that way.
APIM can reach the API If the Analytics API is public, nothing to do. If it is behind private endpoints, APIM must be in the same or a peered VNet: VNet-injected (Developer or Premium) or with outbound VNet integration (Standard v2 or Premium v2), and must resolve the API hostname through your private DNS zone.
You can assign app roles Entra role Cloud Application Administrator or Application Administrator.

Start in a test environment, with DataReader and one read operation.


1. Turn on APIM's managed identity

In the APIM instance, open Security → Managed identities, set System assigned to On and select Save. Then copy the Object (principal) ID:

az apim show -g <apim-resource-group> -n <apim-name> --query identity.principalId -o tsv

Use a user-assigned identity instead if several APIM APIs need different Analytics roles, or the identity must survive the APIM instance being replaced.

2. Assign APIM an Analytics role

The Azure portal cannot assign an app role to a managed identity (it is not offered under the enterprise application's Users and groups), so this is done with Microsoft Graph:

APIM_PRINCIPAL_ID=<from step 1>
API_SP_ID=$(az ad sp show --id <analytics-api-client-id> --query id -o tsv)
ROLE_ID=$(az ad app show --id <analytics-api-client-id> --query "appRoles[?value=='DataReader'].id" -o tsv)

az rest --method POST \
  --uri "https://graph.microsoft.com/v1.0/servicePrincipals/$APIM_PRINCIPAL_ID/appRoleAssignments" \
  --body "{\"principalId\":\"$APIM_PRINCIPAL_ID\",\"resourceId\":\"$API_SP_ID\",\"appRoleId\":\"$ROLE_ID\"}"

Do this before APIM calls Analytics for the first time. Managed identity tokens are valid for 24 hours and Azure caches them. If APIM requests a token before the role exists, it keeps getting that token without the role for up to 24 hours, and every call returns 401. Restarting APIM or editing the policy does not clear it. If it happens, wait, or switch the policy to a new user-assigned identity that has the role before its first call (step 4, client-id).

This is an Entra app role assignment, not an Azure RBAC role. APIM needs no Azure role (such as Contributor or Website Contributor) on the Analytics web app.

3. Add the API to APIM

Create an API in APIM whose Web service URL is the Analytics API base URL including /api, for example https://analytics-api.contoso.com/api. With an API URL suffix of analytics, a consumer then calls:

https://<apim-name>.azure-api.net/analytics/ReportScheduleType

and APIM forwards it to https://analytics-api.contoso.com/api/ReportScheduleType. Check that /api appears once only: a backend URL ending in /api plus operations that start with /api gives /api/api/... and a 404.

Add only the operations your consumers need. You can import the API's OpenAPI document from https://<analytics-api-host>/swagger/v1/swagger.json, or create operations by hand. Exposing only GET operations at the gateway is a sensible extra control on top of the DataReader role.

4. Add the managed identity policy

At API scope (or operation scope, if only some operations should use it):

<policies>
  <inbound>
    <base />
    <set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />
    <authentication-managed-identity resource="api://<analytics-api-client-id>" />
  </inbound>
  <backend>
    <base />
  </backend>
  <outbound>
    <base />
  </outbound>
  <on-error>
    <base />
  </on-error>
</policies>
  • resource is the Application ID URI exactly as shown under Expose an API. Not the web app URL, not APIM's client ID, and without /.default.
  • For a user-assigned identity, add client-id="<identity-client-id>".
  • Removing the subscription key header keeps it out of the backend's request logs.
  • Do not log the Authorization header in APIM diagnostics or Application Insights.

5. Test

Test Expected
GET /analytics/ReportScheduleType with a valid APIM subscription key 200 and the schedule types
The same call before step 2 (no role assigned) 401 from Analytics, with WWW-Authenticate: Bearer error="invalid_token". The API log shows IDW10201: Neither scope or roles claim was found. If the Analytics enterprise application has Assignment required on, Entra refuses the token instead, and APIM itself returns 500 from the policy.
A write, such as POST /analytics/ReportScheduleType, with only DataReader 403. Nothing is written.
The web application, the IDE connector and existing API clients Unchanged

If you run the negative test, do it with a separate identity. Otherwise APIM's own identity is stuck with a role-less token for up to 24 hours, as described in step 2. The same delay applies when you later change or remove the role. Plan role changes, or use a new user-assigned identity. Turning the system-assigned identity off and on creates a new identity with no roles, so only do that as a last resort.


Responses and what they mean

Response Cause Check
401 from Analytics, Bearer error="invalid_token" The token has no Analytics role. Step 2, the role ID, and whether APIM requested a token before the role existed (cached up to 24 hours).
401 from Analytics, audience or issuer resource in the policy is not the Application ID URI, or APIM is in another tenant. Step 4.
403 from Analytics The role is too low for the operation. Grant DataWriter only if the consumers really write.
500 from APIM, authentication-managed-identity error The identity is off, or Entra refused the token (AADSTS501051: assignment required and none made). Steps 1 and 2.
502 or 504 from APIM APIM cannot reach the API: DNS, private endpoint, NSG or certificate. Resolve the API hostname from APIM's network. It must return the private endpoint IP when the API is private.
404 Wrong backend URL or a duplicated /api. Step 3.

Using Event Hub notifications with APIM

If Analytics publishes import and revert notifications to Azure Event Hub, a consumer (for example an Azure Function with an Event Hub trigger) can react to them and fetch the changed data through APIM. Use the notification format Analytics actually sends (see the Event Hub Notification Configuration Guide in IT Documentation):

  • Type 2 (default) carries the report schedule ID and the IDs of the activities that changed. The consumer builds the APIM URLs itself.
  • Type 3 carries full URLs built from CallbackHostUrl. Set CallbackHostUrl to your APIM base URL, for example https://<apim-name>.azure-api.net/analytics, so that every link in the notification goes through APIM. This setting is only used to build those links. Nothing else in Analytics calls it.

Give the consumer Azure Event Hubs Data Receiver on the event hub and its own consumer group, and let it authenticate to APIM like any other consumer. It does not need access to Analytics itself.


Reference

Azure resources that demonstrate this setup with a private Analytics API, including the VNet-injected APIM, NSG rules and policy, are in the Analytics source repository under Azure.IaC/Reference-Security (Modules/apiManagement.bicep, turned on with deployApiManagement). Like the rest of the reference security example, it is an example to adapt, not a finished production configuration.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article