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. Add an edit_file tool to your agent
Add the Parameters:
edit_file tool to your agent. Use one of the formats below.- General Prompt
- JSON Tool (Claude)
- Output Parsing (No Tool)
Tool Description
target_file(string, required): The target file to modifyinstructions(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.
Models
Choose the model that best fits your use case:Request Format
Parameters
initial_code(required): The complete original code that needs modificationedit_snippet(required): Code snippet showing the changes with// ... existing code ...markers for unchanged sectionsinstructions(optional): Brief description of what you’re changing to help disambiguate the editmodel(optional): Model to use (morph-v3-fast,morph-v3-large, orauto- defaults toauto)stream(optional): Whether to stream the response (defaults tofalse)
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