Grafana HTTP request through python to query data-source

I am using Grafana official documentation as reference to query a data source via python. Should queries be sent as payload or params when using Grafana

/api/ds/query

I have come up the following Python code but it returns me “{‘message’: ‘id is invalid’}”.

headers = {"Accept": "application/json",
           "Content-Type": "application/json",
           "X-Grafana-Org-Id": "<org-id>",
           "Authorization": "Bearer <your token here>"
           }

api_url = f"{grafana_url}/api/ds/query"
payload = {
    "queries": [
        {
            "refId": "A",
            "datasource": {
                "uid": "<your data-source uid here>"
            },
            "format": "table",
        }
    ],
    "from": "now-1y",
    "to": "now"
}
response = requests.post(api_url, json=payload, headers=headers,verify = False)
response.json()

I know the data-source uid I am using is correct because I am able to make calls through following api:

/api/datasources/uid/:uid

This doesn’t makes sense to me.

Welcome to forum

Why are you querying the datasource on top of grafana, why not query it directly and bypass grafana?

Thankyou!

The task requires me to do data-validation on Grafana as well as bypass it and do data-validation on the connected datasource.
So I am doing both. .

1 Like