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
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:
3

3. Get the merged code

Extract the final merged code from the response:
Response format:

Models

Choose the model that best fits your use case:

Request Format

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

Streaming Response

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

Example Request

Example Response

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