Chat Completions
POST /v1/chat/completions: a text request in the OpenAI Chat format.
/v1/chat/completionsSends a conversation to a text model and returns the answer. The format is compatible with OpenAI Chat Completions: client.chat.completions.create(...) from the OpenAI SDK works. The request body goes to the provider as is, so OpenAI fields that the model supports work unchanged.
Headers
| Header | Value |
|---|---|
Authorization | Bearer <key>. Required. x-api-key: <key> is accepted instead |
Content-Type | application/json |
X-PII-Masking | on or off: whether to mask personal data in this request. PII masking |
X-Morphogen-Space | spc_…: the space the spending is assigned to. Optional |
Request body
modelstringrequiredA model name from GET /v1/models. Text models and image models (with modalities) work. The name of an embeddings, speech or video model gives 400 model_category_mismatch.
messagesarrayrequiredConversation messages: objects with role (system, user, assistant, tool) and content. Images in the input are passed as image_url blocks.
max_tokensintegerThe maximum response length. Always pass it: without it, an amount based on the model's maximum response length is reserved.
streambooleantrue turns on a streaming response in the server-sent events format.
temperaturenumberThe randomness of the response. Some models ignore the parameter without an error.
toolsarrayFunction descriptions for tool calls. Works on models that support tool use.
modalitiesarray["image"] or ["image", "text"] for image generation models. The image arrives in choices[0].message.images. More: Images and video.
The other OpenAI Chat fields (top_p, stop, response_format and others) are passed to the model as is.
Request example
curl https://api.morphogen.ru/v1/chat/completions \
-H "Authorization: Bearer $MORPHOGEN_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-5",
"max_tokens": 200,
"messages": [{"role": "user", "content": "Назови столицу Франции одним словом"}]
}'Response example
{
"id": "gen-1760104212-Kd3vQ8mZ",
"object": "chat.completion",
"model": "anthropic/claude-sonnet-5",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "Париж" },
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 18, "completion_tokens": 3, "total_tokens": 21 }
}The response comes in the OpenAI format. With stream: true the server sends data: {...} events and ends the stream with the data: [DONE] line. The response has the X-Request-Id and X-PII-Masking headers (applied or off).
Errors
invalid_jsonThe request body could not be parsed.model_category_mismatchAn embeddings, speech or video model. They have their own endpoints.invalid_pii_masking_headerThe X-PII-Masking value is not on, off, true, false, 1 or 0.pii_masking_unsupported_endpointX-PII-Masking: on with an image model.invalid_api_keyThe key is missing, invalid, revoked or expired.insufficient_fundsThe balance does not cover the request reserve.key_limit_exceededThe daily or monthly key limit is used up.model_not_allowedThe model is not in the key's list.pii_masking_not_enabledX-PII-Masking: on, but the masking option is not enabled.model_not_foundThere is no such model in the catalog.request_too_largeThe request body is larger than 32 MiB.rate_limit_exceededThe request or token limit per minute is exceeded. Retry after the time in Retry-After.Full list of codes: Errors.