How to get the panel query or queries from the api

Hello,
My use case:

I have grafana dashboards with panels.
Each panel has 1 or more queries (4 max)

The idea is to fetch all the queries that panel is using and execute them in another web application.

I would like to avoid lookup by ids (both dashboards and panels) and use some business name: like “Monthly Sales” panel inside “Sales” dashboard.
Are there json fields like: name, tag, alias or any other that could be used?

Thanks & best,
Milan

Welcome

What kind of queries are they, what data source?

Infinity Data Source
GRAPHQL queries

play around with this using python :fish:

import requests
import json
from dotenv import load_dotenv
load_dotenv()
import os


GRAFANA_TOKEN=os.getenv('GRAFANA_TOKEN')
headers= {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(GRAFANA_TOKEN)
}

uid = 'zDN35Wd4k'
db = requests.get(url = 'http://localhost:3923/api/dashboards/uid/' + uid, headers = headers)
data = db.json()

panels = data['dashboard']['panels']
targets = panels[0]['targets'][0]
print(targets['query'])

Thanks,
but I would need to fetch the panel by some humanly readable (semantically meaningful) identifier; I can’t really rely on its position on the dashboard.

1 Like

can be done dynamically but it gets hairy and grafana should really not be the source of queries for other systems. this feels uncomfortable

r = requests.get(url = 'http://localhost:3923/api/search?query=%', headers = headers)

for d in r.json():
    uid = d.get('uid')
    db = requests.get(url = 'http://localhost:3923/api/dashboards/uid/' + uid, headers = headers)
    data = db.json()
    
    panels = data['dashboard']['panels']
    
    targets = panels[0]['targets'][0]
    type = targets['datasource']['type']
    
    if type == 'mysql':