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

# Apply API

> Apply code edits at 10,500 tok/s with 98% accuracy via OpenAI-compatible API

## Overview

The Apply API enables lightning-fast code editing at **10,500+ tokens/second** with **98% accuracy**. This OpenAI-compatible endpoint intelligently merges code changes while preserving structure and formatting.

## Models

Choose the model that best fits your use case:

<Table>
  <TableHead>
    <TableRow>
      <TableHeader>Model</TableHeader>
      <TableHeader>Speed</TableHeader>
      <TableHeader>Accuracy</TableHeader>
      <TableHeader>Best For</TableHeader>
    </TableRow>
  </TableHead>

  <TableBody>
    <TableRow>
      <TableCell>
        <code>morph-v3-fast</code>
      </TableCell>

      <TableCell>10,500+ tok/sec</TableCell>
      <TableCell>96%</TableCell>
      <TableCell>Real-time applications, quick edits</TableCell>
    </TableRow>

    <TableRow>
      <TableCell>
        <code>morph-v3-large</code>
      </TableCell>

      <TableCell>5000+ tok/sec</TableCell>
      <TableCell>98%</TableCell>
      <TableCell>Complex changes, highest accuracy</TableCell>
    </TableRow>

    <TableRow>
      <TableCell>
        <code>auto</code>
      </TableCell>

      <TableCell>5000-10,500tok/sec</TableCell>
      <TableCell>\~98%</TableCell>

      <TableCell>
        <strong>Recommended</strong> - automatically selects optimal model
      </TableCell>
    </TableRow>
  </TableBody>
</Table>

## Message Format

The Apply API uses a structured XML format within the message content:

```
<instruction>Brief description of what you're changing</instruction>
<code>Original code content</code>
<update>Code snippet showing only the changes with // ... existing code ... markers</update>
```

### Format Guidelines

* **`<instruction>`**: Optional but recommended. Use first-person, clear descriptions
* **`<code>`**: The complete original code that needs modification
* **`<update>`**: Show only what changes, using `// ... existing code ...` for unchanged sections

## Usage Examples

<CodeGroup>
  ```typescript TypeScript highlight={13} theme={null}
  import OpenAI from "openai";

  const openai = new OpenAI({
    apiKey: "YOUR_API_KEY",
    baseURL: "https://api.morphllm.com/v1",
  });

  const instruction = "I will add error handling to prevent division by zero";
  const originalCode = "function divide(a, b) {\n  return a / b;\n}";
  const codeEdit = "function divide(a, b) {\n  if (b === 0) {\n    throw new Error('Cannot divide by zero');\n  }\n  return a / b;\n}";

  const response = await openai.chat.completions.create({
    model: "morph-v3-fast",
    messages: [
      {
        role: "user",
        content: `<instruction>${instruction}</instruction>\n<code>${originalCode}</code>\n<update>${codeEdit}</update>`,
      },
    ],
  });

  const mergedCode = response.choices[0].message.content;
  ```

  ```python Python highlight={14} theme={null}
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key="YOUR_API_KEY",
      base_url="https://api.morphllm.com/v1"
  )

  instruction = "I will add error handling to prevent division by zero"
  original_code = "function divide(a, b) {\n  return a / b;\n}"
  code_edit = "function divide(a, b) {\n  if (b === 0) {\n    throw new Error('Cannot divide by zero');\n  }\n  return a / b;\n}"

  response = client.chat.completions.create(
      model="morph-v3-fast",
      messages=[
          {
              "role": "user",
              "content": f"<instruction>{instruction}</instruction>\n<code>{original_code}</code>\n<update>{code_edit}</update>"
          }
      ]
  )

  merged_code = response.choices[0].message.content
  ```

  ```bash cURL highlight={9} theme={null}
  curl -X POST "https://api.morphllm.com/v1/chat/completions" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "morph-v3-fast",
      "messages": [
        {
          "role": "user",
          "content": "<instruction>I will add error handling to prevent division by zero</instruction>\n<code>function divide(a, b) {\n  return a / b;\n}</code>\n<update>function divide(a, b) {\n  if (b === 0) {\n    throw new Error(\"Cannot divide by zero\");\n  }\n  return a / b;\n}</update>"
        }
      ]
    }'
  ```
</CodeGroup>

## Error Codes

<Table>
  <TableHead>
    <TableRow>
      <TableHeader>HTTP Status</TableHeader>
      <TableHeader>Description</TableHeader>
    </TableRow>
  </TableHead>

  <TableBody>
    <TableRow>
      <TableCell>
        <code>200</code>
      </TableCell>

      <TableCell>Success - chat completion response</TableCell>
    </TableRow>

    <TableRow>
      <TableCell>
        <code>400</code>
      </TableCell>

      <TableCell>Bad request - malformed request or parameters</TableCell>
    </TableRow>

    <TableRow>
      <TableCell>
        <code>401</code>
      </TableCell>

      <TableCell>Authentication error - invalid API key</TableCell>
    </TableRow>
  </TableBody>
</Table>

<CardGroup cols={2}>
  <Card title="edit_file Tool Guide" icon="wrench" href="/guides/edit_file_tool">
    Build AI agent tools with Morph Apply
  </Card>

  <Card title="More Examples" icon="code" href="/guides/tools">
    See more implementation patterns
  </Card>
</CardGroup>
