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 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
Code Block | ||
---|---|---|
| ||
#!/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/ |
...
Use the id
value as Datasource id.
If all credentials are valid, the output of the script is JSON like this:
Code Block | ||
---|---|---|
| ||
{
"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
} |
If the Grafana IP is incorrect you will get an error like this:
Code Block | ||
---|---|---|
| ||
curl: (7) Failed to connect to 10.2.7.100 port 80 after 6058 ms: Couldn't connect to server |
If user credentials are invalid you will get an error like this:
Code Block | ||
---|---|---|
| ||
{
"message": "invalid username or password"
} |
If the request is not authorized you will get an error like this:
Code Block | ||
---|---|---|
| ||
{
"message": "Unauthorized"
} |
...
$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:
Code Block | ||
---|---|---|
| ||
curl - |
...
s - |
...
u "$USER:$PASS" \ "http:// |
...
$HOST:$PORT/api/datasources" |
...
\uD83D\uDCCB Related articles
Filter by label (Content by label) | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|