Templating Grafana HTTP request to query a data source via Python

27 views Asked by At

I want to query a data source in Grafana via Python.

Following is the code I have come up with.

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()

The code returns {'message': 'id is invalid'}. The grafana_url and uid are correct as I have used it in another code. I want to query the data-source to get last 1 year data in Python as JSON/CSV/Table.

This is the official documentation I am using for Grafana HTTP-client.

0

There are 0 answers