# 报告

> 基于一项或多项调研生成报告、读取报告、下载为 PDF、PowerPoint 或 Markdown，以及通过链接分享。

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

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

## 估算一组调研的报告费用
`POST /v1/reports/estimate`

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

在确定生成之前，查看基于 `study_ids` 生成报告要花多少积分。

**请求体**

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `study_ids` | `string[]` | 是 | 报告涵盖的调研（调研运行的 ID），必须全部来自同一个对话。最多 50 项。 |

**示例**

```bash
curl https://api.imario.ai/v1/reports/estimate \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "study_ids": ["STUDY_ID"]
  }'
```

**响应**

返回 `credits`（生成一份报告的价格：按所涵盖调研的运行积分的一定比例计算，见 `tier_split`，且不低于 `minimum`）、`credits_due`（现在不带 `rewrite` 调用 `POST /reports` 会扣多少：如果 `existing_report_id` 指向一份正好涵盖这些调研的已完成报告，则为 0，调用时会直接打开那份报告）、`per_study`（每项调研的运行积分）、付费方的 `balance`、`blocked`（工作区当天的 API 预算会拒绝这次请求时为 `daily_budget_exceeded`）和 `sufficient`。测试密钥看到的也是正式价格。

## 基于一项或多项调研生成报告
`POST /v1/reports`

**权限范围** `reports:write` · **角色** 任何成员

开始生成报告（任何成员都可以，包括查看者）。请求立即返回报告：生成期间 `status` 为 `queued` 或 `running`（可以轮询 `GET /reports/{id}`，或订阅 `report.completed` / `report.failed`）；如果已经有一份正好涵盖这些调研的已完成报告，而且 `rewrite` 为 false，则直接返回 `completed`（打开它不收费）。新生成的报告在开始前扣费，金额与 `POST /reports/estimate` 给出的完全一致，失败时退还；`credits.charged` 是这份报告的净扣费。

**请求体**

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `study_ids` | `string[]` | 是 | 报告涵盖的调研（调研运行的 ID），必须全部来自同一个对话。最多 50 项。 |
| `rewrite` | `boolean` | 否 | 即使已经有一份正好涵盖这些调研的已完成报告，也重新生成（生成一份新报告，再次扣费）。默认值为 `false`。 |

**示例**

```bash
curl https://api.imario.ai/v1/reports \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "study_ids": ["STUDY_ID"]
  }'
```

## 列出报告（按时间倒序）
`GET /v1/reports`

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

工作区的报告（已完成、生成中和失败的），不含正文（`sections`、`findings`）：需要正文时，获取单份报告。测试密钥能看到正式报告和它自己的沙盒报告。

**参数**

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

**示例**

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

**响应**

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

## 获取报告
`GET /v1/reports/{report_id}`

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

返回报告：`status`（queued | running | completed | failed）、它涵盖的 `study_ids`、`title`、`language`、`credits.charged`、`web_url`；完成后还有 `summary`、`sections`（`[{title, markdown}]`，按顺序排列的报告章节）、`findings`（`[{title, implication, recommendation, priority, themes}]`）、`themes`、`respondents` 和 `questions`。被范围更大的报告取代的报告，仍然可以按 ID 读取，并在 `superseded_by` 中给出取代它的报告。

**示例**

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

## 把报告导出为 PDF、PowerPoint 或 Markdown
`POST /v1/reports/{report_id}/exports`

**权限范围** `reports:write` · **角色** 任何成员

为已完成的报告生成下载文件。请求立即返回下载文件对象（`status` 为 queued）；文件在后台生成，生成方式与报告页面上的下载相同。可以轮询 `GET /exports/{id}`，或订阅 `export.completed`；已完成的下载文件带有 `download_url`，这个链接一小时内有效（`expires_at`），使用时不需要 API 密钥。导出不收费。

**请求体**

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `format` | `string` | 是 | pdf、pptx 或 markdown。可选值：`pdf`、`pptx`、`markdown`。 |

**示例**

```bash
curl https://api.imario.ai/v1/reports/REPORT_ID/exports \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "format": "pdf"
  }'
```

## 获取下载文件
`GET /v1/exports/{export_id}`

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

`status`（queued | running | completed | failed）；完成后还有 `download_url`（从这次响应起一小时内有效，见 `expires_at`）、`filename`、`content_type` 和 `size`。需要新链接时再请求一次即可。

**示例**

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

## 创建报告的公开链接
`POST /v1/reports/{report_id}/share-links`

**权限范围** `reports:write` · **角色** 任何成员

任何拿到链接的人都能打开，直到 `expires_at` 或链接被撤销（`DELETE /share-links/{id}`）为止。工作区的分享规则同样适用：组织可以关闭公开链接（`public_links_off`），也可以限制链接的有效期（`link_too_long`）。`include_responses` 决定链接里是否显示每位受访者的回答。每次调用都会生成一个新链接。

**请求体**

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `expires_in_days` | `integer` | 否 | 链接的有效天数（工作区可能只允许更少的天数）。取值范围 1 到 365。默认值为 `7`。 |
| `include_responses` | `boolean` | 否 | 链接是否显示**回答**（Responses）和**编码表**（Code Book）标签页（即每位受访者的回答）。默认值为 `true`。 |

**示例**

```bash
curl https://api.imario.ai/v1/reports/REPORT_ID/share-links \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "expires_in_days": 7,
    "include_responses": false
  }'
```

## 列出报告的分享链接
`GET /v1/reports/{report_id}/share-links`

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

这份报告生成过的所有链接，按时间倒序排列，并带有各自的 `status`（active、expired 或 revoked）。

**参数**

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

**示例**

```bash
curl https://api.imario.ai/v1/reports/REPORT_ID/share-links \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

**响应**

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

## 撤销分享链接
`DELETE /v1/share-links/{share_link_id}`

**权限范围** `reports:write` · **角色** 任何成员

撤销后，链接立即失效，无法再打开报告。撤销一个已撤销的链接，会原样返回该链接。

**示例**

```bash
curl -X DELETE https://api.imario.ai/v1/share-links/SHARE_LINK_ID \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```
