# 流程圖產生與編輯

以 SVG 繪製科研流程圖和工作流程圖，再透過後續指令修改。

來源：https://scifig.ai/zh-Hant/docs/api/flowcharts

## JSON 請求體

`POST /flowcharts/generations`。所需權限範圍：`figures:generate`。

| 欄位 | 類型 | 必填 | 預設值 | 規則 |
| --- | --- | --- | --- | --- |
| prompt | string | 是 | — | 1–8,000 個字元 |
| aspect_ratio | string | 否 | `auto` | `auto`, `16:9`, `1:1`, `2:3`, `3:2`, `3:4`, `4:3`, `4:5`, `5:4`, `9:16`, `21:9` |
| attachment_file_ids | array | 否 | — | 最多 8 個以 `flowchart_attachment` 用途上傳的檔案（例如實驗方案或草稿） |

消耗 100 點數。成功任務包含一個文字可編輯的 SVG 輸出，並攜帶 `editor_url`，可在網站的 FlowChart 對話中開啟。透過免費的[轉換](/zh-Hant/docs/api/image-conversions)匯出 PNG、PDF、TIFF 或 PPTX。

附件需先透過 `POST /files` 以 `flowchart_attachment` 用途上傳；計入網站上傳額度，有效期為 24 小時。

## 編輯

`POST /flowcharts/edits` 接受 JSON `{"job": "<flowchart job id>", "instruction": "..."}`，修改該任務產生的流程圖，並保留寬高比。`instruction` 為 1–8,000 個字元，消耗 100 點數。規則與[圖表編輯](/zh-Hant/docs/api/charts)相同：父任務仍執行時回傳可重試的 `409 REVISION_CONFLICT`（`PARENT_JOB_RUNNING`）；父任務失敗時回傳 `400 INVALID_REQUEST`（`JOB_NOT_SUCCEEDED`）；只有最新版本可編輯（`409 REVISION_CONFLICT`、`NOT_LATEST_REVISION`）；一次只能執行一個編輯（`EDIT_IN_PROGRESS`）；結果顯示在網站同一個 FlowChart 對話中。

## cURL

```bash
curl -X POST https://scifig.ai/api/v1/flowcharts/generations \
  -H "Authorization: Bearer $SCIFIG_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Prefer: wait=25" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "CONSORT flow of a randomized trial: 400 assessed, 80 excluded, 320 randomized into two arms of 160, follow-up and analysis",
    "aspect_ratio": "4:3"
  }'
```

## JavaScript

```javascript
const response = await fetch('https://scifig.ai/api/v1/flowcharts/generations', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.SCIFIG_API_KEY}`,
    'Idempotency-Key': crypto.randomUUID(),
    Prefer: 'wait=25',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "prompt": "CONSORT flow of a randomized trial: 400 assessed, 80 excluded, 320 randomized into two arms of 160, follow-up and analysis",
    "aspect_ratio": "4:3"
  }),
});
const result = await response.json();
console.log(response.status, result);
```

## Python

```python
import os
import uuid

import requests

headers = {
    "Authorization": f"Bearer {os.environ['SCIFIG_API_KEY']}",
    "Idempotency-Key": str(uuid.uuid4()),
    "Prefer": "wait=25",
}
response = requests.post(
    "https://scifig.ai/api/v1/flowcharts/generations",
    headers=headers,
    json={
        "prompt": "CONSORT flow of a randomized trial: 400 assessed, 80 excluded, 320 randomized into two arms of 160, follow-up and analysis",
        "aspect_ratio": "4:3"
    },
    timeout=60,
)
print(response.status_code, response.json())
```
