Apply API
Apply code edits at 10,500 tok/s with 98% accuracy via OpenAI-compatible API
curl --request POST \
--url https://api.morphllm.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data @- <<EOF
{
"model": "morph-v3-fast",
"messages": [
{
"role": "user",
"content": "<instruction>I will add error handling</instruction>\n<code>function divide(a, b) {\n return a / b;\n}</code>\n<update>function divide(a, b) {\n if (b === 0) throw new Error('Division by zero');\n return a / b;\n}</update>"
}
],
"stream": false,
"max_tokens": 150,
"temperature": 0
}
EOFimport requests
url = "https://api.morphllm.com/v1/chat/completions"
payload = {
"model": "morph-v3-fast",
"messages": [
{
"role": "user",
"content": "<instruction>I will add error handling</instruction>
<code>function divide(a, b) {
return a / b;
}</code>
<update>function divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}</update>"
}
],
"stream": False,
"max_tokens": 150,
"temperature": 0
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'morph-v3-fast',
messages: [
{
role: 'user',
content: '<instruction>I will add error handling</instruction>\n<code>function divide(a, b) {\n return a / b;\n}</code>\n<update>function divide(a, b) {\n if (b === 0) throw new Error(\'Division by zero\');\n return a / b;\n}</update>'
}
],
stream: false,
max_tokens: 150,
temperature: 0
})
};
fetch('https://api.morphllm.com/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.morphllm.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'morph-v3-fast',
'messages' => [
[
'role' => 'user',
'content' => '<instruction>I will add error handling</instruction>
<code>function divide(a, b) {
return a / b;
}</code>
<update>function divide(a, b) {
if (b === 0) throw new Error(\'Division by zero\');
return a / b;
}</update>'
]
],
'stream' => false,
'max_tokens' => 150,
'temperature' => 0
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.morphllm.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"morph-v3-fast\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"<instruction>I will add error handling</instruction>\\n<code>function divide(a, b) {\\n return a / b;\\n}</code>\\n<update>function divide(a, b) {\\n if (b === 0) throw new Error('Division by zero');\\n return a / b;\\n}</update>\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 150,\n \"temperature\": 0\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.morphllm.com/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"morph-v3-fast\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"<instruction>I will add error handling</instruction>\\n<code>function divide(a, b) {\\n return a / b;\\n}</code>\\n<update>function divide(a, b) {\\n if (b === 0) throw new Error('Division by zero');\\n return a / b;\\n}</update>\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 150,\n \"temperature\": 0\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.morphllm.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"morph-v3-fast\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"<instruction>I will add error handling</instruction>\\n<code>function divide(a, b) {\\n return a / b;\\n}</code>\\n<update>function divide(a, b) {\\n if (b === 0) throw new Error('Division by zero');\\n return a / b;\\n}</update>\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 150,\n \"temperature\": 0\n}"
response = http.request(request)
puts response.read_body{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\ndef calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax\n"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 32,
"total_tokens": 57
}
}{
"error": {
"code": "invalid_request_error",
"message": "The request body is missing the `model` field."
}
}{
"error": {
"code": "unauthorized",
"message": "Invalid API key provided."
}
}{
"error": {
"code": "rate_limited",
"message": "Too many requests. Retry in 12 seconds."
}
}{
"error": {
"code": "internal_error",
"message": "Something went wrong on our side."
}
}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: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
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;
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
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>"
}
]
}'
Error Codes
edit_file Tool Guide
More Examples
Authorizations
Morph API key, passed as Authorization: Bearer sk-.... Create keys at https://www.morphllm.com/dashboard/api-keys.
Body
Chat completion request for Apply or Warp Grep (OpenAI-compatible)
- Morph Apply
- Warp Grep
Either a Morph Apply request or a Warp Grep request, discriminated by model.
ID of the Apply model to use, or auto to let the router choose
morph-v3-fast, morph-v3-large, auto "morph-v3-fast"
Array containing a single user message with structured content using instruction-guided format
Show child attributes
Show child attributes
[ { "role": "user", "content": "<instruction>I will add error handling</instruction>\n<code>function divide(a, b) {\n return a / b;\n}</code>\n<update>function divide(a, b) {\n if (b === 0) throw new Error('Division by zero');\n return a / b;\n}</update>" } ]
Enable streaming response. When true the response is a text/event-stream of OpenAI-style chat.completion.chunk deltas terminated by data: [DONE].
false
Maximum number of tokens the Apply model may generate
150
Sampling temperature for the Apply request (0.0 for deterministic output)
0
Response
Chat completion response
Completion returned by a Morph chat model.
Unique identifier for the completion
"chatcmpl-123"
Always chat.completion
"chat.completion"
Unix timestamp of when the completion was created
1677652288
List of completion choices
Show child attributes
Show child attributes
[ { "index": 0, "message": { "role": "assistant", "content": "\ndef calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax\n" }, "finish_reason": "stop" } ]
Usage statistics for the completion request
Show child attributes
Show child attributes
{ "prompt_tokens": 25, "completion_tokens": 32, "total_tokens": 57 }
curl --request POST \
--url https://api.morphllm.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data @- <<EOF
{
"model": "morph-v3-fast",
"messages": [
{
"role": "user",
"content": "<instruction>I will add error handling</instruction>\n<code>function divide(a, b) {\n return a / b;\n}</code>\n<update>function divide(a, b) {\n if (b === 0) throw new Error('Division by zero');\n return a / b;\n}</update>"
}
],
"stream": false,
"max_tokens": 150,
"temperature": 0
}
EOFimport requests
url = "https://api.morphllm.com/v1/chat/completions"
payload = {
"model": "morph-v3-fast",
"messages": [
{
"role": "user",
"content": "<instruction>I will add error handling</instruction>
<code>function divide(a, b) {
return a / b;
}</code>
<update>function divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}</update>"
}
],
"stream": False,
"max_tokens": 150,
"temperature": 0
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'morph-v3-fast',
messages: [
{
role: 'user',
content: '<instruction>I will add error handling</instruction>\n<code>function divide(a, b) {\n return a / b;\n}</code>\n<update>function divide(a, b) {\n if (b === 0) throw new Error(\'Division by zero\');\n return a / b;\n}</update>'
}
],
stream: false,
max_tokens: 150,
temperature: 0
})
};
fetch('https://api.morphllm.com/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.morphllm.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'morph-v3-fast',
'messages' => [
[
'role' => 'user',
'content' => '<instruction>I will add error handling</instruction>
<code>function divide(a, b) {
return a / b;
}</code>
<update>function divide(a, b) {
if (b === 0) throw new Error(\'Division by zero\');
return a / b;
}</update>'
]
],
'stream' => false,
'max_tokens' => 150,
'temperature' => 0
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.morphllm.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"morph-v3-fast\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"<instruction>I will add error handling</instruction>\\n<code>function divide(a, b) {\\n return a / b;\\n}</code>\\n<update>function divide(a, b) {\\n if (b === 0) throw new Error('Division by zero');\\n return a / b;\\n}</update>\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 150,\n \"temperature\": 0\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.morphllm.com/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"morph-v3-fast\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"<instruction>I will add error handling</instruction>\\n<code>function divide(a, b) {\\n return a / b;\\n}</code>\\n<update>function divide(a, b) {\\n if (b === 0) throw new Error('Division by zero');\\n return a / b;\\n}</update>\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 150,\n \"temperature\": 0\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.morphllm.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"morph-v3-fast\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"<instruction>I will add error handling</instruction>\\n<code>function divide(a, b) {\\n return a / b;\\n}</code>\\n<update>function divide(a, b) {\\n if (b === 0) throw new Error('Division by zero');\\n return a / b;\\n}</update>\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 150,\n \"temperature\": 0\n}"
response = http.request(request)
puts response.read_body{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\ndef calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax\n"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 32,
"total_tokens": 57
}
}{
"error": {
"code": "invalid_request_error",
"message": "The request body is missing the `model` field."
}
}{
"error": {
"code": "unauthorized",
"message": "Invalid API key provided."
}
}{
"error": {
"code": "rate_limited",
"message": "Too many requests. Retry in 12 seconds."
}
}{
"error": {
"code": "internal_error",
"message": "Something went wrong on our side."
}
}