# 图像生成

根据文字提示词生成科研插图，也可通过参考图像引导生成。

来源：https://scifig.ai/zh/docs/api/image-generations

## JSON 请求体

`POST /images/generations` 接受 JSON 请求体。所需权限范围：`figures:generate`。

| 字段 | 类型 | 必填 | 默认值 | 规则 |
| --- | --- | --- | --- | --- |
| prompt | string | 是 | — | 3–20,000 个字符 |
| aspect_ratio | string | 否 | `1:1` | `1:1`, `2:3`, `3:2`, `3:4`, `4:3`, `4:5`, `5:4`, `9:16`, `16:9`, `21:9` |
| quality | string | 否 | `standard` | `standard`、`advanced` 或 `premium`；决定模型和价格 |
| style | string | 否 | — | `flat`, `2-5d`, `line-art`, `hand-drawn`, `scientific`, `line`, `3d`, `editorial`, `sketch`, `watercolor`, `auto` |
| palette | 字符串或数组 | 否 | — | 配色预设 ID，或 1–4 个 `#RRGGBB` 颜色值 |
| reference_file_ids | array | 否 | — | 1–3 个图像 `file_id`（上传文件或此前的输出） |

未知字段会被拒绝，返回 `400 INVALID_REQUEST`；`details.issues` 列出各项问题。部分模型、参考图像和宽高比需要付费档位，否则返回 `403 PLAN_REQUIRED`。

## 积分

| quality | 积分 |
| --- | --- |
| standard | 50 |
| advanced | 75 |
| premium | 100 |

创建任务时预留积分，成功后仅扣除一次，失败时全额返还。响应返回任务；字段和状态请参阅[任务与有限等待](/zh/docs/api/jobs)，错误请参阅[错误](/zh/docs/api/errors)。

## cURL

```bash
curl -X POST https://scifig.ai/api/v1/images/generations \
  -H "Authorization: Bearer $SCIFIG_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Prefer: wait=25" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Schematic of CRISPR-Cas9 gene editing: guide RNA, Cas9, target DNA and the double-strand break, labelled",
    "aspect_ratio": "4:3",
    "quality": "standard"
  }'
```

## JavaScript

```javascript
const response = await fetch('https://scifig.ai/api/v1/images/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": "Schematic of CRISPR-Cas9 gene editing: guide RNA, Cas9, target DNA and the double-strand break, labelled",
    "aspect_ratio": "4:3",
    "quality": "standard"
  }),
});
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/images/generations",
    headers=headers,
    json={
        "prompt": "Schematic of CRISPR-Cas9 gene editing: guide RNA, Cas9, target DNA and the double-strand break, labelled",
        "aspect_ratio": "4:3",
        "quality": "standard"
    },
    timeout=60,
)
print(response.status_code, response.json())
```
