Skip to main content

Overview

The Code Apply API provides a direct interface for applying code edits using the Morph model. This endpoint intelligently merges code changes at 10,500+ tokens/second with 99.2% accuracy, designed specifically for AI agents and development tools. Unlike the chat-based API, this endpoint accepts structured parameters directly, making it easier to integrate into automated workflows and development environments.

Quickstart

1

1. Add an edit_file tool to your agent

Add the edit_file tool to your agent. Use one of the formats below.
Tool Description
Use this tool to make an edit to an existing file.

This will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.
When writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.

For example:

// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...
THIRD_EDIT
// ... existing code ...

You should still bias towards repeating as few lines of the original file as possible to convey the change.
But, each edit should contain minimally sufficient context of unchanged lines around the code you're editing to resolve ambiguity.
DO NOT omit spans of pre-existing code (or comments) without using the // ... existing code ... comment to indicate its absence. If you omit the existing code comment, the model may inadvertently delete these lines.
If you plan on deleting a section, you must provide context before and after to delete it. If the initial code is ```code \n Block 1 \n Block 2 \n Block 3 \n code```, and you want to remove Block 2, you would output ```// ... existing code ... \n Block 1 \n  Block 3 \n // ... existing code ...```.
Make sure it is clear what the edit should be, and where it should be applied.
Make edits to a file in a single edit_file call instead of multiple edit_file calls to the same file. The apply model can handle many distinct edits at once.
Parameters:
  • target_file (string, required): The target file to modify
  • instructions (string, required): A single sentence written in the first person describing what you’re changing. Used to help disambiguate uncertainty in the edit.
  • code_edit (string, required): Specify ONLY the precise lines of code that you wish to edit. Use // ... existing code ... for unchanged sections.
IMPORTANT: The instructions param should be generated by the model, not hardcoded. Example: “I am adding error handling to the user auth and removing the old auth functions”
2

2. Call the API

Send the original code and edit snippet to the Code Apply endpoint:
import requests

url = "https://api.morphllm.com/v1/code/apply"
api_key = "[YOUR_API_KEY]"

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

data = {
    "initial_code": initial_code,
    "edit_snippet": edit_snippet,
}

response = requests.post(url, headers=headers, json=data)
return response.json()
3

3. Get the merged code

Extract the final merged code from the response:
merged_code = response.json()["merged_code"]
Response format:
{
  "merged_code": "string",
  "usage": {
    "prompt_tokens": "number",
    "completion_tokens": "number",
    "total_tokens": "number"
  }
}

Models

Choose the model that best fits your use case:

Request Format

{
  "initial_code": "string",
  "edit_snippet": "string", 
  "instructions": "string (optional)",
  "model": "string (optional)",
  "stream": "boolean (optional)"
}

Parameters

  • initial_code (required): The complete original code that needs modification
  • edit_snippet (required): Code snippet showing the changes with // ... existing code ... markers for unchanged sections
  • instructions (optional): Brief description of what you’re changing to help disambiguate the edit
  • model (optional): Model to use (morph-v3-fast, morph-v3-large, or auto - defaults to auto)
  • stream (optional): Whether to stream the response (defaults to false)

Response Format

Non-Streaming Response

{
  "mergedCode": "string",
  "usage": {
    "prompt_tokens": "number",
    "completion_tokens": "number", 
    "total_tokens": "number"
  }
}

Streaming Response

For streaming requests (stream: true), the response follows the Server-Sent Events (SSE) format with incremental code updates.

Example Request

curl -X POST "https://api.morphllm.com/v1/code/apply" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "initial_code": "function divide(a, b) {\n  return a / b;\n}",
    "edit_snippet": "function divide(a, b) {\n  if (b === 0) {\n    throw new Error('\''Cannot divide by zero'\'');\n  }\n  return a / b;\n}",
    "instructions": "Add error handling to prevent division by zero"
  }'

Example Response

{
  "merged_code": "function divide(a, b) {\n  if (b === 0) {\n    throw new Error('Cannot divide by zero');\n  }\n  return a / b;\n}",
  "usage": {
    "prompt_tokens": 45,
    "completion_tokens": 28,
    "total_tokens": 73
  }
}

Error Codes

Key Features

  • High Performance: Up to 10,500+ tokens/second with morph-v3-fast
  • High Accuracy: 99.2% accuracy with intelligent code merging
  • Preserves Structure: Maintains code formatting, indentation, and comments
  • Streaming Support: Real-time streaming for large code changes
  • Multiple Models: Choose between speed and accuracy based on your needs
  • Direct Integration: Simple JSON API designed for automated workflows

Integration Guide

Learn how to integrate the Code Apply API into your workflow

Chat Completions API

Use the OpenAI-compatible chat interface instead