REST API

To support the development of various types of applications and platforms, TDengine provides an API that conforms to REST principles; namely REST API. To minimize the learning cost, unlike REST APIs for other database engines, TDengine allows insertion of SQL commands in the BODY of an HTTP POST request, to operate the database.

REST API - 图1note

One difference from the native connector is that the REST interface is stateless and so the USE db_name command has no effect. All references to table names and super table names need to specify the database name in the prefix. TDengine supports specification of the db_name in RESTful URL. If the database name prefix is not specified in the SQL command, the db_name specified in the URL will be used.

Installation

The REST interface does not rely on any TDengine native library, so the client application does not need to install any TDengine libraries. The client application’s development language only needs to support the HTTP protocol. The REST interface is provided by taosAdapter, to use REST interface you need to make sure taosAdapter is running properly.

Verification

If the TDengine server is already installed, it can be verified as follows:

The following example is in an Ubuntu environment and uses the curl tool to verify that the REST interface is working. Note that the curl tool may need to be installed in your environment.

The following example lists all databases on the host h1.tdengine.com. To use it in your environment, replace h1.tdengine.com and 6041 (the default port) with the actual running TDengine service FQDN and port number.

  1. curl -L -H "Authorization: Basic cm9vdDp0YW9zZGF0YQ==" \
  2. -d "select name, ntables, status from information_schema.ins_databases;" \
  3. h1.tdengine.com:6041/rest/sql

The following return value results indicate that the verification passed.

  1. {
  2. "code": 0,
  3. "column_meta": [
  4. [
  5. "name",
  6. "VARCHAR",
  7. 64
  8. ],
  9. [
  10. "ntables",
  11. "BIGINT",
  12. 8
  13. ],
  14. [
  15. "status",
  16. "VARCHAR",
  17. 10
  18. ]
  19. ],
  20. "data": [
  21. [
  22. "information_schema",
  23. 16,
  24. "ready"
  25. ],
  26. [
  27. "performance_schema",
  28. 9,
  29. "ready"
  30. ]
  31. ],
  32. "rows": 2
  33. }

HTTP request URL format

  1. http://<fqdn>:<port>/rest/sql/[db_name][?tz=timezone]

Parameter Description:

  • fqnd: FQDN or IP address of any host in the cluster.
  • port: httpPort configuration item in the configuration file, default is 6041.
  • db_name: Optional parameter that specifies the default database name for the executed SQL command.
  • tz: Optional parameter that specifies the timezone of the returned time, following the IANA Time Zone rules, e.g. America/New_York.

For example, http://h1.taos.com:6041/rest/sql/test is a URL to h1.taos.com:6041 and sets the default database name to test.

TDengine supports both Basic authentication and custom authentication mechanisms, and subsequent versions will provide a standard secure digital signature mechanism for authentication.

  • authentication information is shown below:

    1. Authorization: Taosd <TOKEN>
  • Basic authentication information is shown below:

    1. Authorization: Basic <TOKEN>

The HTTP request’s BODY is a complete SQL command, and the data table in the SQL statement should be provided with a database prefix, e.g., db_name.tb_name. If the table name does not have a database prefix and the database name is not specified in the URL, the system will response an error because the HTTP module is a simple forwarder and has no awareness of the current DB.

Use curl to initiate an HTTP request with a custom authentication method, with the following syntax.

  1. curl -L -H "Authorization: Basic <TOKEN>" -d "<SQL>" <ip>:<PORT>/rest/sql/[db_name][?tz=timezone]

or

  1. curl -L -u username:password -d "<SQL>" <ip>:<PORT>/rest/sql/[db_name][?tz=timezone]

where TOKEN is the string after Base64 encoding of {username}:{password}, e.g. root:taosdata is encoded as cm9vdDp0YW9zZGF0YQ==..

HTTP Return Format

HTTP Response Code

Response CodeDescription
200Success. (Also used for C interface errors.)
400Parameter error
401Authentication failure
404Interface not found
500Internal error
503Insufficient system resources

HTTP body structure

Successful Insert Operation

Example:

  1. {
  2. "code": 0,
  3. "column_meta": [["affected_rows", "INT", 4]],
  4. "data": [[0]],
  5. "rows": 1
  6. }

Description:

  • code: (int) 0 indicates success.
  • column_meta: ([1][3]any) Only returns [["affected_rows", "INT", 4]].
  • rows: (int) Only returns 1.
  • data: ([][]any) Returns the number of rows affected.

Successful Query Operation

Example:

  1. {
  2. "code": 0,
  3. "column_meta": [
  4. ["ts", "TIMESTAMP", 8],
  5. ["count", "BIGINT", 8],
  6. ["endpoint", "VARCHAR", 45],
  7. ["status_code", "INT", 4],
  8. ["client_ip", "VARCHAR", 40],
  9. ["request_method", "VARCHAR", 15],
  10. ["request_uri", "VARCHAR", 128]
  11. ],
  12. "data": [
  13. [
  14. "2022-06-29T05:50:55.401Z",
  15. 2,
  16. "LAPTOP-NNKFTLTG:6041",
  17. 200,
  18. "172.23.208.1",
  19. "POST",
  20. "/rest/sql"
  21. ],
  22. [
  23. "2022-06-29T05:52:16.603Z",
  24. 1,
  25. "LAPTOP-NNKFTLTG:6041",
  26. 200,
  27. "172.23.208.1",
  28. "POST",
  29. "/rest/sql"
  30. ],
  31. [
  32. "2022-06-29T06:28:14.118Z",
  33. 1,
  34. "LAPTOP-NNKFTLTG:6041",
  35. 200,
  36. "172.23.208.1",
  37. "POST",
  38. "/rest/sql"
  39. ],
  40. [
  41. "2022-06-29T05:52:16.603Z",
  42. 2,
  43. "LAPTOP-NNKFTLTG:6041",
  44. 401,
  45. "172.23.208.1",
  46. "POST",
  47. "/rest/sql"
  48. ]
  49. ],
  50. "rows": 4
  51. }

Description:

  • code: int 0 indicates success.
  • column_meta: ([][3]any) Column information. Each column is described with three values: column name (string), column type (string), and type length (int).
  • rows: (int) The number of rows returned.
  • data: ([][]any)

The following types may be returned:

  • “NULL”
  • “BOOL”
  • “TINYINT”
  • “SMALLINT”
  • “INT”
  • “BIGINT”
  • “FLOAT”
  • “DOUBLE”
  • “VARCHAR”
  • “TIMESTAMP”
  • “NCHAR”
  • “TINYINT UNSIGNED”
  • “SMALLINT UNSIGNED”
  • “INT UNSIGNED”
  • “BIGINT UNSIGNED”
  • “JSON”

Errors

Example:

  1. {
  2. "code": 9728,
  3. "desc": "syntax error near \"1\""
  4. }

Description:

  • code: (int) Error code.
  • desc: (string): Error code description.

Custom Authorization Code

HTTP requests require an authorization code <TOKEN> for identification purposes. The administrator usually provides the authorization code, and it can be obtained simply by sending an HTTP GET request as follows:

  1. curl http://<fqnd>:<port>/rest/login/<username>/<password>

Where fqdn is the FQDN or IP address of the TDengine database. port is the port number of the TDengine service. username is the database username. password is the database password. The return value is in JSON format, and the meaning of each field is as follows.

  • status: flag bit of the request result.
  • code: return value code.
  • desc: authorization code.

Example of getting authorization code.

  1. curl http://192.168.0.1:6041/rest/login/root/taosdata

Response body:

  1. {
  2. "status": "succ",
  3. "code": 0,
  4. "desc": "/KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04"
  5. }

Usage examples

  • query all records from table d1001 of database demo

    1. curl -L -H "Authorization: Basic cm9vdDp0YW9zZGF0YQ==" -d "select * from demo.d1001" 192.168.0.1:6041/rest/sql

    Response body:

    1. {
    2. "code": 0,
    3. "column_meta": [
    4. [
    5. "ts",
    6. "TIMESTAMP",
    7. 8
    8. ],
    9. [
    10. "current",
    11. "FLOAT",
    12. 4
    13. ],
    14. [
    15. "voltage",
    16. "INT",
    17. 4
    18. ],
    19. [
    20. "phase",
    21. "FLOAT",
    22. 4
    23. ]
    24. ],
    25. "data": [
    26. [
    27. "2022-07-30T06:44:40.32Z",
    28. 10.3,
    29. 219,
    30. 0.31
    31. ],
    32. [
    33. "2022-07-30T06:44:41.32Z",
    34. 12.6,
    35. 218,
    36. 0.33
    37. ]
    38. ],
    39. "rows": 2
    40. }
  • Create database demo:

    1. curl -L -H "Authorization: Basic cm9vdDp0YW9zZGF0YQ==" -d "create database demo" 192.168.0.1:6041/rest/sql

    Response body:

    1. {
    2. "code": 0,
    3. "column_meta": [
    4. [
    5. "affected_rows",
    6. "INT",
    7. 4
    8. ]
    9. ],
    10. "data": [
    11. [
    12. 0
    13. ]
    14. ],
    15. "rows": 1
    16. }

Reference

taosAdapter