This article explains how to find out the id of a Datasource in Grafana when you know its name.
The article is based on the following documentation: https://grafana.com/docs/grafana/latest/developers/http_api/data_source/#get-a-single-data-source-by-name Identifying the unique identifier (ID) of a datasource in Grafana is essential for interacting with its configuration via the Grafana API. This process is useful for automation and integration tasks where direct access to the datasource's ID is required.
For more details, refer to the official Grafana HTTP API documentation.
Table of Contents | ||
---|---|---|
|
\uD83D\uDCD8 Instructions
...
Use the following script to reach Grafana API by IP with username and password.
Note: values in this script are only an example. Before running this script on your machine, first update grafana_host
, grafana_port
, grafana_username
, grafana_password,
and datasource_name
with your actual values.
...
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 (
grafana_port=80 grafana_username='admin' grafana_password='admin
,10.2.7.100
,
' datasource_name='adminz-password
,
'80
,hivemq-prometheus-1
) with your actual Grafana details and run the following command:
Code Block language bash curl --location \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ s -u "admin:adminz-password" "http://${grafana_username}:${grafana_password}@${grafana_host}:${grafana_port}10.2.7.100:80/api/datasources/name/${datasource_name}"
Use the
id
value as Datasource id.If all credentials are valid, the output of the script is JSON like this:
If the Grafana IP is incorrect you will get an error like thisCode Block language json { "id": 11, "uid": "abcd1efgh", "orgId": 1, "name": "hivemq-prometheus-1", | "type": "prometheus", "typeName": "Prometheus", "typeLogoUrl": "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg", "access": "proxy", "url": "http://prometheus-kube-prometheus-prometheus:9090/", "password": "", "user": "", "database": "", "basicAuth": false, "isDefault": true, "jsonData": {}, "readOnly": true }
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
If user credentials are invalid you will get an error like this:
Code Block {language json Invalid Credentials:
{"message":
"invalid
username
or
password"
}
If the request is not authorized you will get an error like this:
Code Block {language json Unauthorized:
{"message":
"Unauthorized"
}
List All Datasources: To list all Datasourcesavailable datasources, use the following command:
Code Block language bash curl --location \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ s -u "admin:adminz-password" "http://${grafana_username}:${grafana_password}@${grafana_host}:${grafana_port}10.2.7.100:80/api/datasources"
...
\uD83D\uDCCB Related articles
Filter by label (Content by label) | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|