Create a chat completion
Endpoint
POST
/v1/chat/completionsOpenAI Chat Completions compatible endpoint for GPT and Gemini models. Claude is not recommended here: prompt caching, thinking, and similar capabilities are lost. Use Create a message for Claude instead.
Request
| Header | Value | Notes |
|---|---|---|
Authorization |
Bearer sk-moyiapi-xxxxxx |
Authentication. x-api-key: sk-moyiapi-xxxxxx also works |
content-type |
application/json |
Minimal request body:
{
"model": "gpt-5.6-sol",
"messages": [
{"role": "user", "content": "Hello"}
]
}
Parameters
| Field | Type | Required | Notes |
|---|---|---|---|
model |
string | Yes | GPT or Gemini model ID; see GET /v1/models for the full list |
messages |
array | Yes | Conversation messages; role is system, user, or assistant; content is text |
max_tokens |
integer | No | Maximum output tokens |
stream |
boolean | No | Set to true for SSE streaming; defaults to false |
Other fields pass through in the OpenAI Chat Completions format; refer to the OpenAI documentation for their usage.
Response
A non-streaming call returns a chat.completion object:
{
"object": "chat.completion",
"model": "gpt-5.6-sol",
"choices": [
{"index": 0, "message": {"role": "assistant", "content": "Hello!"}, "finish_reason": "stop"}
],
"usage": {"prompt_tokens": 214, "completion_tokens": 3, "total_tokens": 217}
}
| Field | Notes |
|---|---|
id |
Response ID |
object |
Always chat.completion |
created |
Creation time (Unix seconds) |
model |
The model that produced the response |
choices |
Array of replies; the text is in choices[0].message.content, and finish_reason is the stop reason (for example stop) |
usage |
prompt_tokens, completion_tokens, total_tokens; on a cache hit, prompt_tokens_details.cached_tokens is also present |
service_tier |
The service tier actually used for this request |
With stream set to true, the response is a series of data: {...} chunks (object is chat.completion.chunk), and the last chunk before [DONE] carries usage. See Streaming.
Examples
Basic request
curl https://api.moyiapi.com/v1/chat/completions \
-H "Authorization: Bearer sk-moyiapi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]
}'
Streaming request
curl https://api.moyiapi.com/v1/chat/completions \
-H "Authorization: Bearer sk-moyiapi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"stream": true,
"messages": [{"role": "user", "content": "Write one sentence"}]
}'
Python SDK
from openai import OpenAI
client = OpenAI(
api_key="sk-moyiapi-xxxxxx",
base_url="https://api.moyiapi.com/v1",
)
resp = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)
Errors
| HTTP status | error.type |
When it happens |
|---|---|---|
| 401 | missing_auth_credential / invalid_api_key / invalid_bearer_token |
Authentication failed; see Authentication |
| 400 | model_not_available |
model does not exist or is temporarily unavailable; check the ID against GET /v1/models |
| 400 | model_not_supported |
The model cannot be called at the moment; switch to another model as the message suggests |