Identifying the unique identifier (ID) of a datasource in Grafana is essential for interacting with its configuration via the Grafana API. This process is particularly useful for automation and integration tasks where direct access to the datasource's ID is required. The following procedure demonstrates how to retrieve the datasource ID using its name, based on the official Grafana HTTP API documentation.
For more details, refer to the official Grafana HTTP API documentation.
\uD83D\uDCD8 Instructions
To find the ID of a Grafana datasource by its name, use the following script.
Prerequisites
Update these variables with your Grafana details:
HOST
: Grafana server IPPORT
: Grafana portUSER
: Grafana usernamePASS
: Grafana passwordDS_NAME
: Datasource name
Script
#!/bin/bash # Example values for Grafana connection HOST="10.2.7.100" PORT="80" USER="admin" PASS="adminz-password" DS_NAME="hivemq-prometheus-1" # Query Grafana API to get the datasource ID curl -s -u "$USER:$PASS" \ "http://$HOST:$PORT/api/datasources/name/$DS_NAME" | jq '.id'
Output
The script will return the datasource ID.
Error Messages
Connection error:
curl: (7) Failed to connect
Invalid credentials:
{"message": "invalid username or password"}
Unauthorized:
{"message": "Unauthorized"}
List All Datasources
To list all datasources:
curl -s -u "$USER:$PASS" \ "http://$HOST:$PORT/api/datasources"