Tutorial: Call Grafana APIs programmatically
In this tutorial, you learn how to:
- Assign an Azure Managed Grafana role to the service principal of your application
- Retrieve application details
- Get an access token
- Call Grafana APIs
Prerequisites
- An Azure account with an active subscription. Create an account for free.
- An Azure Managed Grafana workspace. Create an Azure Managed Grafana instance.
- An Azure Active Directory (Azure AD) application with a service principal. Create an Azure AD application and service principal. For simplicity, use an application located in the same Azure AD tenant as your Managed Grafana instance.
Sign in to Azure
Sign in to the Azure portal at https://portal.azure.com/ with your Azure account.
Assign an Azure Managed Grafana role to the service principal of your application
In the Azure portal, open your Managed Grafana instance.
Select Access control (IAM) in the navigation menu.
Select Add, then Add role assignment.
Select the Grafana Editor role and then Next.
Under Assign access to, select User,group, or service principal.
Select Select members, select your service principal, and hit Select.
Select Review + assign.
Retrieve application details
You now need to gather some information, which you'll use to get a Grafana API access token, and call Grafana APIs.
Find your tenant ID:
- In the Azure portal, enter Azure Active Directory in the Search resources, services, and docs (G+ /).
- Select Azure Active Directory.
- Select Properties from the left menu.
- Locate the field Tenant ID and save its value.
Find your client ID:
- In the Azure portal, in Azure Active Directory, select App registrations from the left menu.
- Select your app.
- In Overview, find the Application (client) ID field and save its value.
Create an application secret:
- In the Azure portal, in Azure Active Directory, select App registrations from the left menu.
- Select your app.
- Select Certificates & secrets from the left menu.
- Select New client secret.
- Create a new client secret and save its value.
Note
You can only access a secret's value immediately after creating it. Copy the value before leaving the page to use it in the next step of this tutorial.
Find your Grafana endpoint URL:
- In the Azure portal, enter Azure Managed Grafana in the Search resources, services, and docs (G+ /) bar.
- Select Azure Managed Grafana and open your Managed Grafana workspace.
- Select Overview from the left menu and save the Endpoint value.
Get an access token
To access Grafana APIs, you need to get an access token. Follow the example below to call Azure AD and retrieve a token. Replace <tenant-id>
, <client-id>
, and <client-secret>
with the tenant ID, application (client) ID, and client secret collected in the previous step.
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id=<client-id>&client_secret=<client-secret>&resource=ce34e7e5-485f-4d76-964f-b3d2b16d1e4f' \
https://login.microsoftonline.com/<tenant-id>/oauth2/token
Here's an example of response:
{
"token_type": "Bearer",
"expires_in": "599",
"ext_expires_in": "599",
"expires_on": "1575500555",
"not_before": "1575499766",
"resource": "ce34...1e4f",
"access_token": "eyJ0eXAiOiJ......AARUQ"
}
Call Grafana APIs
You can now call Grafana APIs using the access token retrieved in the previous step as the Authorization header. For example:
curl -X GET \
-H 'Authorization: Bearer <access-token>' \
https://<grafana-url>/api/user
Replace <access-token>
and <grafana-url>
with the access token retrieved in the previous step and the endpoint URL of your Grafana instance. For example https://my-grafana-abcd.cuse.grafana.azure.com
.
Clean up resources
If you're not going to continue to use these resources, delete them with the following steps:
Delete Azure Managed Grafana:
- In the Azure portal, in Azure Managed Grafana, select Overview from the left menu.
- Select Delete.
- Enter the resource name to confirm deletion and select Delete.
Delete the Azure AD application:
- In the Azure portal, in Azure Active Directory, select App registrations from the left menu.
- Select your app.
- In the Overview tab, select Delete.
- Select Delete.
Next steps
Feedback
Submit and view feedback for