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
将数据写入 InfluxDB,使用 HTTP 请求向 InfluxDB API /api/v2/write
端点发送请求。
查询 API
从 InfluxDB 查询数据,使用 HTTP 请求向 /api/v2/query
端点发送请求。
此页面是否对您有帮助?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 InfluxDB 和本文档的反馈和错误报告。如需获得支持,请使用以下资源
拥有年度或支持合同的客户可以 联系 InfluxData 支持。