# 数据连接

> 工作区已连接的数据源、每个数据源当前能提供的内容，以及发起一次同步。

Source: https://imario.ai/zh/docs/api/reference/connections

基础地址为 `https://api.imario.ai/v1`。每次调用所用的密钥都要带有各接口标明的权限范围，密钥的角色也要达到标明的要求（参见[认证](https://imario.ai/zh/docs/api/authentication)）。错误的格式见 [API 错误](https://imario.ai/zh/docs/api/errors)。

## 列出工作区的数据连接
`GET /v1/connections`

**权限范围** `connections:read` · **角色** 任何成员

按时间倒序排列。`connection` 对象包含：`id`、`livemode`、`created_at`、`provider`（数据源的标识，例如 `hubspot`）、`provider_name`、`account_label`、`status`（`active` | `syncing` | `expired` | `revoked`）、`error_message`、`last_sync_at`、`stale`（同步已经停止报告进度）。

**参数**

| 名称 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| `limit` | 查询参数 | `integer` | 否 | 返回的对象个数（1 到 100）。默认值为 `20`。 |
| `cursor` | 查询参数 | `string` | 否 | 上一页返回的 `next_cursor`。最多 200 个字符。 |

**示例**

```bash
curl https://api.imario.ai/v1/connections \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

**响应**

返回一个列表：`{"object": "list", "data": [...], "has_more": false, "next_cursor": null}`。参见[分页](https://imario.ai/zh/docs/api/pagination)。

## 数据连接当前可提供的内容
`GET /v1/connections/{id}/items`

**权限范围** `connections:read` · **角色** 任何成员

现在从数据源读取到的条目（字段、问卷、录音等），与网页端确认页面显示的一致，附带 `notes` 和 `summary`。对于流式数据源（录音、讨论串），`watch`（JSON）可以预览一个持续生效的同步范围会收进哪些内容。不分页：`has_more` 始终为 false。

**参数**

| 名称 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| `watch` | 查询参数 | `string` | 否 | 流式数据源的同步范围，JSON 格式。最多 2,000 个字符。 |

**示例**

```bash
curl https://api.imario.ai/v1/connections/CONNECTION_ID/items \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## 同步数据连接
`POST /v1/connections/{id}/sync`

**权限范围** `connections:write` · **角色** 编辑者及以上 · 仅限正式密钥

把一次拉取加入队列，并立即返回数据连接（`status: syncing`）。不传 `items` 或 `watch`，就沿用上次同步的选择。已有同步在进行时返回 409 `sync_in_progress`。仅限正式密钥；需要编辑者角色。`connection` 对象包含：`id`、`livemode`、`created_at`、`provider`（数据源的标识，例如 `hubspot`）、`provider_name`、`account_label`、`status`（`active` | `syncing` | `expired` | `revoked`）、`error_message`、`last_sync_at`、`stale`（同步已经停止报告进度）。

**请求体**

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `items` | `string[]` | 否 | 要拉取的条目 ID（来自 /items）。默认：上次同步拉取的条目。最多 1,000 项。 |
| `fields` | `string[]` | 否 | 最多 1,000 项。 |
| `options` | `object` | 否 |  |
| `watch` | `object` | 否 | 流式数据源持续生效的同步范围。 |
| `audience_id` | `string` | 否 | 同步下来的文件要并入哪个用数据构建的人群。默认：上次同步并入的人群。最多 36 个字符。 |

**示例**

```bash
curl https://api.imario.ai/v1/connections/CONNECTION_ID/sync \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```
