# OpenAI Protocol (GPT)

OpenAI Chat Completions / Responses compatible. For GPT, plus DeepSeek, GLM, etc.

## Endpoints
| Usage | Method | Path |
| --- | --- | --- |
| Chat (REST + SSE) | POST | /v1/chat/completions |
| Codex (Responses) | POST | /v1/responses |
| Model list | GET | /v1/models |

## Supported models
gpt-* (e.g. gpt-4o), deepseek-*, glm-* (e.g. glm-5.2), etc. See GET /v1/models.

## Request body
| Param | Type | Required | Description |
| --- | --- | --- | --- |
| model | string | Yes | Model, e.g. gpt-4o |
| messages | array | Yes | Messages (role/content) |
| stream | boolean | — | true for SSE; add stream_options.include_usage=true to get usage |
| temperature / top_p | number | — | Sampling controls |
| max_tokens / max_completion_tokens | integer | — | Max output |
| tools / tool_choice | — | — | Tool calling |

## Streaming
Set "stream": true and "stream_options": {"include_usage": true}; receive SSE with curl -N. Events data: {chunk}, ends with data: [DONE]; usage in the final chunk.

## Non-streaming
```bash
curl -X POST "https://api.novapi.ai/v1/chat/completions" \
  -H "Authorization: Bearer <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role":"user","content":"你好，介绍一下你自己"}]
  }'
```

## Streaming (SSE with usage)
```bash
curl -N -X POST "https://api.novapi.ai/v1/chat/completions" \
  -H "Authorization: Bearer <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "stream": true,
    "stream_options": {"include_usage": true},
    "messages": [{"role":"user","content":"写一首关于夏天的短诗"}]
  }'
```

## Response example
```json
{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "model": "gpt-4o",
  "choices": [
    { "index": 0, "message": { "role": "assistant", "content": "你好！……" }, "finish_reason": "stop" }
  ],
  "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 }
}
```

## Image Generation (GPT-Image · sync)

Beyond chat, the same OpenAI protocol also offers synchronous image generation: a single request returns the image (url or b64_json) directly — no taskId, no polling. Developers already using an OpenAI SDK / images client can integrate as-is.

### Endpoints
| Usage | Method | Path |
| --- | --- | --- |
| Text-to-image | POST | /v1/images/generations |
| Image edit (multipart) | POST | /v1/images/edits |
| Variations (multipart) | POST | /v1/images/variations |

### Supported models
gpt-image-1 / gpt-image-1.5 / gpt-image-2 / gpt-image-2.5-flare / gpt-image-2.5-sunburst.

### Request body
| Param | Type | Required | Description |
| --- | --- | --- | --- |
| model | string | Yes | gpt-image-1 / gpt-image-1.5 / gpt-image-2 / gpt-image-2.5-flare / gpt-image-2.5-sunburst |
| prompt | string | Yes | Image description |
| size | string | — | Output size (official pixels, default 1024x1024) |
| response_format | string | — | url (a stable, long-lived link on our CDN — it does not expire shortly after generation) or b64_json (inline image data; gpt-image-1 often returns this) |

> ⚠ This synchronous channel and the async GPT-Image channel (POST /api/cqt/generator/gpt-image) are billed differently — the same model may cost a different amount on each, so check the pricing page. Image-to-image / variations use multipart /v1/images/edits and /v1/images/variations.

### Text-to-image (sync)
```bash
curl -X POST "https://api.novapi.ai/v1/images/generations" \
  -H "Authorization: Bearer <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "a red panda astronaut, studio lighting",
    "size": "1024x1024"
  }'
```

### Image edit (multipart)
```bash
# 图生图走 /v1/images/edits，multipart/form-data 上传参考图（字段 image[]，可多张）
# 可选 mask 蒙版做局部重绘；不要带 Content-Type: application/json
curl -X POST "https://api.novapi.ai/v1/images/edits" \
  -H "Authorization: Bearer <API_KEY>" \
  -F "model=gpt-image-2" \
  -F "image[]=@input.png" \
  -F "prompt=add a red hat on the cat" \
  -F "size=1024x1024"
```

### Image variations (multipart)
```bash
# 变体走 /v1/images/variations，无 prompt，基于参考图生成风格相近的变体
curl -X POST "https://api.novapi.ai/v1/images/variations" \
  -H "Authorization: Bearer <API_KEY>" \
  -F "model=gpt-image-2" \
  -F "image[]=@input.png" \
  -F "size=1024x1024"
```

### Response example
```json
{
  "created": 1710000000,
  "data": [
    { "url": "https://.../image.png" }
  ]
}
```
