API 快速入门
InfluxDB 提供了丰富的 API 和 客户端库,可以轻松与您的应用程序集成。使用像 Curl 和 Postman 这样的常用工具可以快速测试 API 请求。
本节将引导您了解最常用的 API 方法。
有关整个 API 的详细文档,请参阅 InfluxDB v2 API 参考。
如果您需要将 InfluxDB 2.7 与 InfluxDB 1.x API 客户端和集成一起使用,请参阅 1.x 兼容性 API。
引导您的应用程序
对于大多数 API 请求,您至少需要提供您的 InfluxDB URL 和授权令牌(API 令牌)。
安装 InfluxDB OSS v2.x 或升级到 InfluxDB Cloud 账户。
身份验证
InfluxDB 使用 API 令牌 来授权 API 请求。
在探索 API 之前,请使用 InfluxDB UI 为您的应用程序创建初始 API 令牌。
在每个请求中,将您的 API 令牌包含在
Authorization: Token YOUR_API_TOKEN
HTTP 标头中。
#######################################
# 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"
/**
* Use a token in the Authorization header
* to authenticate with the InfluxDB 2.x API.
*/
const https = require('https');
function queryWithToken() {
const options = {
host: 'localhost:8086',
path: "/api/v2",
headers: {
'Authorization': 'Token YOUR_API_TOKEN',
'Content-type': 'application/json'
},
};
const request = https.get(options, (response) => {
let rawData = '';
response.on('data', () => {
response.on('data', (chunk) => { rawData += chunk; });
})
response.on('end', () => {
console.log(rawData);
})
});
request.end();
}
Postman 是另一个用于探索 API 的常用工具。请参阅如何使用 Postman 发送经过身份验证的请求。
存储桶 API
在写入数据之前,您需要在 InfluxDB 中创建一个存储桶。使用 HTTP 请求向 InfluxDB API /buckets
端点创建存储桶。
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
}
]
}'
写入 API
使用 HTTP 请求向 InfluxDB API /api/v2/write
端点将数据写入 InfluxDB。
查询 API
使用 HTTP 请求向 /api/v2/query
端点从 InfluxDB 查询数据。
此页是否对您有帮助?
感谢您的反馈!