Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 IP

  • PORT: Grafana port

  • USER: Grafana username

  • PASS: Grafana password

  • DS_NAME: Datasource name

Script

Code Block
languagebash
#!/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
languagejson
{
  "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
languagetext
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
languagejson
{
  "message": "invalid username or password"
}

If the request is not authorized you will get an error like this:

Code Block
languagejson
{
  "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
languagebash
curl -

...

s -

...

u "$USER:$PASS" \
  "http://

...

$HOST:$PORT/api/datasources"

...

Filter by label (Content by label)
page
showLabelsfalse
max5
spacescom.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@957
showSpacefalse
sortmodified
showSpacetypefalsepage
reversetruetype
labelskb-how-to-article
cqllabel = "kb-how-to-article" and type = "page" and space = "KB"labelskb-how-to-article