MorphogenDocs
API

Chat Completions

POST /v1/chat/completions: a text request in the OpenAI Chat format.

POST/v1/chat/completions

Sends 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

HeaderValue
AuthorizationBearer <key>. Required. x-api-key: <key> is accepted instead
Content-Typeapplication/json
X-PII-Maskingon or off: whether to mask personal data in this request. PII masking
X-Morphogen-Spacespc_…: the space the spending is assigned to. Optional

Request body

modelstringrequired

A 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.

messagesarrayrequired

Conversation messages: objects with role (system, user, assistant, tool) and content. Images in the input are passed as image_url blocks.

max_tokensinteger

The maximum response length. Always pass it: without it, an amount based on the model's maximum response length is reserved.

streamboolean

true turns on a streaming response in the server-sent events format.

temperaturenumber

The randomness of the response. Some models ignore the parameter without an error.

toolsarray

Function 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

400invalid_jsonThe request body could not be parsed.
400model_category_mismatchAn embeddings, speech or video model. They have their own endpoints.
400invalid_pii_masking_headerThe X-PII-Masking value is not on, off, true, false, 1 or 0.
400pii_masking_unsupported_endpointX-PII-Masking: on with an image model.
401invalid_api_keyThe key is missing, invalid, revoked or expired.
402insufficient_fundsThe balance does not cover the request reserve.
402key_limit_exceededThe daily or monthly key limit is used up.
403model_not_allowedThe model is not in the key's list.
403pii_masking_not_enabledX-PII-Masking: on, but the masking option is not enabled.
404model_not_foundThere is no such model in the catalog.
413request_too_largeThe request body is larger than 32 MiB.
429rate_limit_exceededThe request or token limit per minute is exceeded. Retry after the time in Retry-After.
503service_unavailableThe service is temporarily unavailable. Retry the request.

Full list of codes: Errors.

On this page