All Products
Search
Document Center

Use the HTTP API to query data

Last Updated: Aug 07, 2020

The HTTP API is the primary means for querying data in TSDB for InfluxDB®. For information about other means to query data, see Command-line interface and Clients.

Note: The examples in this topic are based on curl, which is a command-line tool that uses URLs to transmit data.

To run a query, send a GET request to the /query enpoint. Then, set the URL parameter db to the target database, and set the URL parameter q to the query statement. You can also send a POST request to query data. You can set the same URL parameters as the GET request or set parameters to be part of application/x-www-form-urlencoded. The following example describes how to use the HTTP API to query the databases that are introduced in Use the HTTP API to write data.

  1. curl -G 'https://<Domain name>:3242/query?u=<Username>&p=<Password>&pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"

The response from TSDB for InfluxDB® is in JSON format. The query result appears in the results array. If an error occurs, TSDB for InfluxDB® introduces the error details in the error field.

  1. {
  2. "results": [
  3. {
  4. "statement_id": 0,
  5. "series": [
  6. {
  7. "name": "cpu_load_short",
  8. "columns": [
  9. "time",
  10. "value"
  11. ],
  12. "values": [
  13. [
  14. "2015-01-29T21:55:43.702900257Z",
  15. 2
  16. ],
  17. [
  18. "2015-01-29T21:55:43.702900257Z",
  19. 0.55
  20. ],
  21. [
  22. "2015-06-11T20:46:02Z",
  23. 0.64
  24. ]
  25. ]
  26. }
  27. ]
  28. }
  29. ]
  30. }

Note: When you debug or use curl to query data, you can append pretty=true to the URL to enable well formatted and readable JSON output. In actual production environments, we do not recommend that you use pretty=true because it consumes extra network bandwidth.

Multiple queries

When you send multiple queries to TSDB for InfluxDB® in a single API call, separate the queries with semicolons (;).

  1. curl -G 'https://<Domain name>:3242/query?u=<Username>&p=<Password>&pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west';SELECT count(\"value\") FROM \"cpu_load_short\" WHERE \"region\"='us-west'"

The system returns:

  1. {
  2. "results": [
  3. {
  4. "statement_id": 0,
  5. "series": [
  6. {
  7. "name": "cpu_load_short",
  8. "columns": [
  9. "time",
  10. "value"
  11. ],
  12. "values": [
  13. [
  14. "2015-01-29T21:55:43.702900257Z",
  15. 2
  16. ],
  17. [
  18. "2015-01-29T21:55:43.702900257Z",
  19. 0.55
  20. ],
  21. [
  22. "2015-06-11T20:46:02Z",
  23. 0.64
  24. ]
  25. ]
  26. }
  27. ]
  28. },
  29. {
  30. "statement_id": 1,
  31. "series": [
  32. {
  33. "name": "cpu_load_short",
  34. "columns": [
  35. "time",
  36. "count"
  37. ],
  38. "values": [
  39. [
  40. "1970-01-01T00:00:00Z",
  41. 3
  42. ]
  43. ]
  44. }
  45. ]
  46. }
  47. ]
  48. }

Other options in a query

Timestamp format

All data in TSDB for InfluxDB® is stored and displayed based on the UTC+0 time zone. By default, timestamps use the UTC+0 time zone, comply with RFC 3339, and are accurate to nanoseconds. For example, 2015-08-04T19:05:14.318570484Z. If you want to return a UNIX timestamp, set the epoch parameter in the request. The value of epoch is a string and can be [h, m, s, ms, u, or ns], which indicate hours, minutes, seconds, milliseconds, microseconds, and nanoseconds. The following example describes how to return a UNIX timestamp that is accurate to seconds.

  1. curl -G 'https://<Domain name>:3242/query?u=<Username>&p=<Password>' --data-urlencode "db=mydb" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"

Chunking

If you set chunked=true in a query, the system returns responses in streamed batches instead of a single response. The system chunks responses by series or by every 10,000 points, whichever occurs first. You can set chunk_size in a query to change the maximum chunk size. The following example describes how to set parameters to chunk responses by every 20,000 points.

  1. curl -G 'https://<Domain name>:3242/query?u=<Username>&p=<Password>' --data-urlencode "db=deluge" --data-urlencode "chunked=true" --data-urlencode "chunk_size=20000" --data-urlencode "q=SELECT * FROM liters"

InfluxQL

After you understand how to query data, you can learn how to use InfluxQL. For more information, see Data exploration. For more information about how to query data by using the HTTP API, see HTTP API.


InfluxDB® is a trademark registered by InfluxData, which is not affiliated with, and does not endorse, TSDB for InfluxDB®.