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"
Prerequisites
Before executing the script to retrieve a Grafana datasource ID, ensure the following requirements are met:
Grafana Server Access: You must have network access to the Grafana instance, including the correct IP address and port number.
User Credentials: A valid Grafana username and password with sufficient permissions to access the API and retrieve datasource information.
Datasource Name: You must know the exact name of the datasource whose ID you wish to find.
Command-line Tools:
curl
: Used to send HTTP requests to the Grafana API.jq
: (Optional) A command-line tool for parsing JSON responses. If not installed, the script will return the full JSON response, but thejq
tool is recommended to extract theid
more cleanly.
Ensure these prerequisites are configured before running the script to ensure a smooth process.