InfluxDB提供了丰富的API和客户端库,可以与您的应用程序集成。使用流行的工具如Curl和Postman快速测试API请求。
本节将指导您了解最常用的API方法。
有关整个API的详细文档,请参阅InfluxDBv2 API参考。
如果您需要将InfluxDB 2.7与InfluxDB 1.x API客户端和集成一起使用,请参阅1.x兼容性API。
对于大多数API请求,您需要提供最少的InfluxDB URL和授权令牌(API令牌)。
安装InfluxDB OSS v2.x或升级到 InfluxDB Cloud账户
InfluxDB使用API令牌来授权API请求。
Authorization: Token YOUR_API_TOKEN
HTTP头中包含您的API令牌。卷曲 Node.js
####################################### # Use a token in the Authorization header # to authenticate with the InfluxDB 2.x API. ####################################### curl --get "http://localhost:8086/api/v2" \ --header "Authorization: Token YOUR_API_TOKEN" \ --header 'Content-type: application/json' \ --data-urlencode "db=mydb" \ --data-urlencode "q=SELECT * FROM cpu_usage"
更改InfluxDB URL
Postman是另一个用于探索API的流行工具。查看如何使用Postman发送经过身份验证的请求。
在写入数据之前,您需要在InfluxDB中创建一个Bucket。 使用对InfluxDB API /buckets
端点的HTTP请求创建一个存储桶。
INFLUX_TOKEN=YOUR_API_TOKEN INFLUX_ORG_ID=YOUR_ORG_ID curl --request POST \ "http://localhost:8086/api/v2/buckets" \ --header "Authorization: Token ${INFLUX_TOKEN}" \ --header "Content-type: application/json" \ --data '{ "orgID": "'"${INFLUX_ORG_ID}"'", "name": "iot-center", "retentionRules": [ { "type": "expire", "everySeconds": 86400, "shardGroupDurationSeconds": 0 } ] }'
更改InfluxDB URL
使用对InfluxDB API /api/v2/write
端点的HTTP请求将数据写入InfluxDB。
使用HTTP请求从InfluxDB查询/api/v2/query
端点。