Prerequisite: You’ll need an account on Morph to obtain an API key.

Follow these steps to integrate Morph into your application:

Step 1: Get Your API Key

The first step is to obtain an API key from the Morph dashboard:

  1. Go to https://morphllm.com/dashboard
  2. Create an account or sign in to your existing account
  3. Create a new API key or use the existing one

Keep this API key secure as it will be used for authentication in your API requests.

Step 2: Use the Morph API

Morph provides an OpenAI-compatible API for applying model-suggested changes to your code files:

Morph API Documentation

View our OpenAI-compatible API

Base URL

https://api.morphllm.com/v1

Compatible with OpenAI SDK

Morph’s API is compatible with the OpenAI SDK. You only need to change the base URL and API key:

import OpenAI from 'openai';

// Initialize the OpenAI client with Morph's API endpoint
const openai = new OpenAI({
  apiKey: 'your-morph-api-key',
  baseURL: 'https://api.morphllm.com/v1'
});

async function applyChanges(originalCode: string, updatedCode: string): Promise<string> {
  const response = await openai.chat.completions.create({
    model: "morph-v2",
    messages: [
      {
        role: "user",
        content: `<code>${originalCode}</code>\n<update>${updatedCode}</update>`
      }
    ]
  });
  
  return response.choices[0].message.content;
}

// Example usage
const original = `function add(a: number, b: number): number {
  return a + b;
}`;

const edits = `function add(a: number, b: number): number {
  // Add validation
  if (isNaN(a) || isNaN(b)) {
    throw new Error('Both arguments must be valid numbers');
  }
  return a + b;
}`;

applyChanges(original, edits)
  .then(result => console.log(result))
  .catch(error => console.error('Error:', error));

Step 3: Build Your Edit File Tool

Create a powerful edit_file tool for your AI agent using Morph’s apply model:

Enhancing Your Agent Capabilities

Morph’s apply model offers significant advantages over traditional LLMs for code editing:

  • Speed: Process edits at 2000+ tokens per second
  • Accuracy: Precise application of changes with context awareness
  • Cost-effective: Lower token usage compared to generating entire files
  • Format preservation: Maintains code structure and formatting

For access to our latest models, self-hosting, or business inquiries, please contact us at info@morphllm.com.

Troubleshooting