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.
Table of Contents | ||
---|---|---|
|
\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
...
language | bash |
---|
...
Prerequisites
Before retrieving the Grafana datasource ID, ensure the following:
Grafana Server Access: Ensure network access to the Grafana instance, with the correct IP and port.
User Credentials: A valid username and password with permission to access the API.
Datasource Name: You must know the exact name of the datasource whose ID you wish to retrieve.
Command-line Tools:
curl
: To send HTTP requests.jq
: (Optional) A tool to parse JSON responses. If not installed, the full response will be returned.
Instructions
Get the Datasource ID by Name: Replace the variables (
admin
,10.2.7.100
...
,
adminz-password
...
,
80
,hivemq-prometheus-1
...
) with your actual Grafana details and run the following command:
Code Block language bash curl -s -u "
...
admin:
...
adminz-password"
...
"http://
...
10.2.7.100:80/api/datasources/name/
...
hivemq-prometheus-1" | jq '.id'
Expected Output
...
: The
...
command will return the datasource ID as a JSON response. For example:
Code Block language text 11
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 available datasources, use the following command:
Code Block language bash curl -s -u "
...
admin:
...
adminz-password"
...
"http://
...
10.2.7.100:80/api/datasources"
...
\uD83D\uDCCB Related articles
...