Backend API to get query result

Is there a backend API to execute the queries and get the queries results? The use case is, I want to add e2e test for some self-defined dashboards. Basically, given some existing data in the datasource, I want to verify the query results are expected.

By browsing the HTTP API | Grafana documentation, I don’t see there’s such an API. Any help would be much appreciated.

1 Like

Dear Anlan,

Your use case resonates with us.

Introduction

The Grafana frontend is making requests to its backend server, that definitively qualifies as an API. You can inspect the requests using your favorite Browser’s developer tools.

However, while the HTTP endpoints are standardized, the payloads are not. The queries sent to the corresponding datasource endpoints will differ based on the type of database you are talking to. That is probably why the corresponding endpoints, for example https://datahub.example.org/grafana/api/datasources/proxy/42/query, are not part of the official HTTP API reference.

A check program from our pen

We don’t do sophisticated e2e tests, but we are running a check program on our Icinga instance which verifies given data sources through the HTTP API against becoming stale and alerts us when they do, taking configurable freshness time thresholds into account.

You might want to use it as a starting point for inspiration.

Guidelines

At the usage-by-example documentation, you can inspect the options to use for the program. Those options are necessary to direct it to the right data source:

--uri https://datahub.example.org/grafana/api/datasources/proxy/42/query
--database testdrive
--table temperature

At check-grafana-datasource-stale.sh#L213-L214, you can see that the program is sending a query that is specifically suited for InfluxDB 1.x. With other datasource types, you would need to send a different query. In order to make the program more universal, it could be improved so that the query expression would also be obtained as command line option.

HTH.

With kind regards,
Andreas.

Thanks for the detailed explanation, Andreas. It is very helpful!

A quick question on the api/ds/query. Isn’t it used to execute and retrieve query result?

https://grafana.com/docs/grafana/latest/developers/http_api/data_source/#query-a-data-source