> ## Documentation Index
> Fetch the complete documentation index at: https://docs.morphllm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Messages API

> Anthropic-compatible /v1/messages endpoint — point an Anthropic SDK at Morph by changing the base URL

## Overview

`POST /v1/messages` mirrors Anthropic's Messages API. Clients built on an Anthropic SDK switch to Morph by changing the base URL and API key — no request or response rewriting.

```python theme={null}
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.morphllm.com",
    api_key="YOUR_MORPH_API_KEY",
)

message = client.messages.create(
    model="morph-glm52-744b",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Refactor this function to be async."}],
)
print(message.content[0].text)
```

Model ids, prices, and context windows are served live at [morphllm.com/api/models/json](https://www.morphllm.com/api/models/json) — fetch that rather than hardcoding.

For OpenAI-style clients, use [`POST /v1/chat/completions`](/api-reference/endpoint/apply) instead; both routes serve the same models.


## OpenAPI

````yaml POST /v1/messages
openapi: 3.1.0
info:
  title: Morph API
  version: 1.1.0
  description: >-
    The Morph public API at api.morphllm.com: OpenAI- and Anthropic-compatible
    inference (chat completions, messages), Fast Apply code editing, Compact
    context compression, Reflex classification, and fine-tuning. Model ids,
    prices, and context windows are served live at
    https://www.morphllm.com/api/models/json.
  contact:
    name: Morph
    url: https://morphllm.com
    email: info@morphllm.com
  license:
    name: Proprietary
    url: https://morphllm.com/privacy/tos
servers:
  - url: https://api.morphllm.com
    description: Production
security: []
tags:
  - name: chat
    description: >-
      OpenAI- and Anthropic-compatible chat inference, including Fast Apply and
      WarpGrep models.
  - name: compact
    description: Context compression for long agent conversations.
  - name: reflex
    description: 'Per-turn classifiers: realtime prediction and batches.'
  - name: fine-tuning
    description: Reflex fine-tuning job lifecycle.
  - name: models
    description: Model listing and management.
  - name: telemetry
    description: Usage reporting hooks.
paths:
  /v1/messages:
    post:
      tags:
        - chat
      summary: Create a message
      description: >-
        Mirrors Anthropic's Messages API so Anthropic SDKs and agents run
        against Morph models by changing the base URL and API key only — request
        and response shapes are unchanged. Set `stream: true` to receive the
        message as a `text/event-stream` of Anthropic-style events; the 200
        response below documents the non-streaming shape.
      operationId: createMessage
      requestBody:
        description: Anthropic-compatible message request
        required: true
        content:
          application/json:
            example:
              model: morph-glm52-744b
              max_tokens: 1024
              messages:
                - role: user
                  content: Refactor this Express route to use async/await.
              system: You are a senior TypeScript engineer. Reply with code only.
              stream: false
              temperature: 0
            schema:
              $ref: '#/components/schemas/AnthropicMessageRequest'
      responses:
        '200':
          description: Message response
          content:
            application/json:
              example:
                id: msg_01XFDUDYJgAACzvnptvVoYEL
                type: message
                role: assistant
                content:
                  - type: text
                    text: |-
                      router.get("/users", async (req, res) => {
                        const users = await db.users.findMany();
                        res.json(users);
                      });
                model: morph-glm52-744b
                stop_reason: end_turn
                usage:
                  input_tokens: 42
                  output_tokens: 37
              schema:
                $ref: '#/components/schemas/AnthropicMessageResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/InternalError'
      security:
        - bearerAuth: []
components:
  schemas:
    AnthropicMessageRequest:
      type: object
      properties:
        model:
          type: string
          description: ID of the Morph model to use
          example: morph-glm52-744b
        max_tokens:
          type: integer
          description: Maximum number of tokens to generate before stopping
          example: 1024
        messages:
          type: array
          items:
            type: object
            properties:
              role:
                type: string
                enum:
                  - user
                  - assistant
                description: Which participant produced this Messages API turn
                example: user
              content:
                type: string
                description: The text content of the message
                example: Refactor this Express route to use async/await.
            required:
              - role
              - content
            description: A single turn in the conversation.
            example:
              role: user
              content: Refactor this Express route to use async/await.
          description: >-
            Conversation turns, alternating between user and assistant, oldest
            first
          example:
            - role: user
              content: Refactor this Express route to use async/await.
        system:
          type: string
          description: System prompt applied ahead of the conversation
          example: You are a senior TypeScript engineer. Reply with code only.
        stream:
          type: boolean
          default: false
          description: >-
            Enable streaming response. When true the response is a
            `text/event-stream` of Anthropic-style `message_start`,
            `content_block_delta`, and `message_stop` events.
          example: false
        temperature:
          type: number
          default: 0
          description: >-
            Sampling temperature for the Messages request (0.0 for deterministic
            output)
          example: 0
      required:
        - model
        - max_tokens
        - messages
      description: Anthropic-shaped Messages request served by a Morph model.
      example:
        model: morph-glm52-744b
        max_tokens: 1024
        messages:
          - role: user
            content: Refactor this Express route to use async/await.
        system: You are a senior TypeScript engineer. Reply with code only.
        stream: false
        temperature: 0
    AnthropicMessageResponse:
      type: object
      properties:
        id:
          type: string
          description: Unique identifier for the message
          example: msg_01XFDUDYJgAACzvnptvVoYEL
        type:
          type: string
          enum:
            - message
          description: Object type, always `message`
          example: message
        role:
          type: string
          enum:
            - assistant
          description: The role of the message author, always `assistant` on a response
          example: assistant
        content:
          type: array
          items:
            type: object
            properties:
              type:
                type: string
                enum:
                  - text
                description: Content block type, always `text`
                example: text
              text:
                type: string
                description: The generated text
                example: |-
                  router.get("/users", async (req, res) => {
                    const users = await db.users.findMany();
                    res.json(users);
                  });
            required:
              - type
              - text
            description: A text content block.
            example:
              type: text
              text: |-
                router.get("/users", async (req, res) => {
                  const users = await db.users.findMany();
                  res.json(users);
                });
          description: Content blocks generated by the model
          example:
            - type: text
              text: |-
                router.get("/users", async (req, res) => {
                  const users = await db.users.findMany();
                  res.json(users);
                });
        model:
          type: string
          description: ID of the model that produced the message
          example: morph-glm52-744b
        stop_reason:
          type: string
          enum:
            - end_turn
            - max_tokens
            - stop_sequence
          description: Why the model stopped generating tokens
          example: end_turn
        usage:
          type: object
          properties:
            input_tokens:
              type: integer
              description: Number of input tokens counted for this message
              example: 42
            output_tokens:
              type: integer
              description: Number of output tokens generated for this message
              example: 37
          required:
            - input_tokens
            - output_tokens
          description: Token usage for the message
          example:
            input_tokens: 42
            output_tokens: 37
      required:
        - id
        - type
        - role
        - content
        - model
        - stop_reason
        - usage
      description: Anthropic-shaped Messages response produced by a Morph model.
      example:
        id: msg_01XFDUDYJgAACzvnptvVoYEL
        type: message
        role: assistant
        content:
          - type: text
            text: |-
              router.get("/users", async (req, res) => {
                const users = await db.users.findMany();
                res.json(users);
              });
        model: morph-glm52-744b
        stop_reason: end_turn
        usage:
          input_tokens: 42
          output_tokens: 37
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: string
              example: invalid_request_error
              description: Machine-readable error code.
            message:
              type: string
              example: The request body is missing the `model` field.
              description: Human-readable explanation of the failure.
          required:
            - code
            - message
      required:
        - error
      description: Standard error envelope returned by every non-2xx response.
      example:
        error:
          code: invalid_request_error
          message: The request body is missing the `model` field.
  responses:
    BadRequest:
      description: Malformed request — missing or invalid fields.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: invalid_request_error
              message: The request body is missing the `model` field.
    Unauthorized:
      description: Missing or invalid API key.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: unauthorized
              message: Invalid API key provided.
    RateLimited:
      description: Rate limited — retry after the interval in the Retry-After header.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: rate_limited
              message: Too many requests. Retry in 12 seconds.
    InternalError:
      description: Internal error — safe to retry with backoff.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: internal_error
              message: Something went wrong on our side.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: opaque
      description: >-
        Morph API key, passed as `Authorization: Bearer sk-...`. Create keys at
        https://www.morphllm.com/dashboard/api-keys.

````