Documentation

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 请求。

  1. 在探索 API 之前,请使用 InfluxDB UI 为你的应用程序创建一个初始 API 令牌

  2. 在每个请求中,将你的 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 端点发送请求。


此页面是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。你可以继续像现在这样使用它,而无需对你的代码进行任何更改。

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速的近实时数据引擎,可实时收集和处理数据,并将其持久化到本地磁盘或对象存储。InfluxDB 3 Enterprise 基于 Core 的基础构建,增加了高可用性、读取副本、增强的安全性以及数据压缩功能,以实现更快的查询和优化的存储。InfluxDB 3 Enterprise 的免费层可供非商业家庭或业余爱好者使用。

有关更多信息,请查看