APIFree
APIFree
Text Generation

POST /v1/messages

Anthropic Messages-style text generation endpoint.

Generate text with an Anthropic Messages-compatible request and response format.

Description

This endpoint accepts Anthropic-style message payloads and forwards them through APIFree’s unified model platform. It is suitable for assistants, chat applications, agent loops, and other conversational workflows that already use the Messages protocol.

Headers

NameTypeRequiredDescription
AuthorizationstringYesBearer token in the format Bearer YOUR_API_KEY.
anthropic-versionstringNoAnthropic protocol version header to forward upstream.
anthropic-betastringNoOptional beta feature header to forward upstream.

Request body parameters

NameTypeRequiredDescription
modelstringYesModel ID to use for generation.
messagesarrayYesOrdered list of conversation messages.
systemstring / arrayNoSystem instruction content.
max_tokensintegerNoMaximum number of tokens to generate.
temperaturenumberNoSampling temperature.
top_pnumberNoNucleus sampling parameter.
streambooleanNoWhether to return SSE streaming events.
toolsarrayNoTool definitions available to the model.
tool_choiceobject / stringNoTool calling strategy.
metadataobjectNoOptional metadata for your own tracking.

messages item

Each item in messages is an object like:

{
  "role": "user",
  "content": "Write a concise product summary for an AI gateway."
}

Supported models

Model
claude-sonnet-4
claude-opus-4.6
claude-sonnet-4.6
claude-opus-4.5
claude-sonnet-4.5
claude-haiku-4.5

Example request

curl https://api.apifree.ai/v1/messages \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4",
    "max_tokens": 512,
    "messages": [
      {
        "role": "user",
        "content": "Summarize the benefits of a unified model API in one paragraph."
      }
    ]
  }'

Response schema

A successful non-stream response typically includes:

FieldTypeDescription
idstringUnique response identifier.
typestringResponse object type.
rolestringUsually assistant.
contentarrayGenerated content blocks.
modelstringModel used to generate the response.
stop_reasonstringWhy generation stopped.
usageobjectToken usage information.

Example response:

{
  "id": "msg_123",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-4",
  "content": [
    {
      "type": "text",
      "text": "A unified model API reduces integration cost by giving teams one endpoint for authentication, routing, billing, and observability across multiple providers."
    }
  ],
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 24,
    "output_tokens": 36
  }
}

Streaming

If stream is true, the endpoint returns Anthropic-style SSE events. Your client should consume the stream incrementally until the terminal event is received.

Errors

Errors are returned as JSON error objects. Typical cases include:

  • Missing or invalid model
  • Invalid JSON request body
  • Authentication failure
  • Upstream request failure