Skip to main content
Edgee provides an OpenAI-compatible API, which means you can use the official OpenAI SDKs for TypeScript and Python with Edgee. This allows you to leverage Edgee’s routing, observability, and cost tracking features without changing your existing code.

Why Use OpenAI SDK with Edgee?

  • No Code Changes: Use your existing OpenAI SDK code as-is
  • Multi-Provider Access: Route to OpenAI, Anthropic, Google, and more through one API
  • Automatic Failover: Built-in reliability with fallback providers
  • Cost Tracking: Real-time visibility into token usage and costs
  • Observability: Request tracing and logging across all providers

Installation

Install the OpenAI SDK for your preferred language:
npm install openai

Configuration

Configure the OpenAI SDK to use Edgee’s API endpoint:
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://api.edgee.ai/v1",
  apiKey: process.env.EDGEE_API_KEY, // Your Edgee API key
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
      { role: "user", content: "What is the capital of France?" }
    ],
  });

  console.log(completion.choices[0].message.content);
}

main();

Advanced Usage

Function Calling (Tools)

Edgee fully supports OpenAI’s function calling interface:
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://api.edgee.ai/v1",
  apiKey: process.env.EDGEE_API_KEY,
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
      { role: "user", content: "What's the weather in Paris?" }
    ],
    tools: [
      {
        type: "function",
        function: {
          name: "get_weather",
          description: "Get the current weather for a location",
          parameters: {
            type: "object",
            properties: {
              location: {
                type: "string",
                description: "City name",
              },
            },
            required: ["location"],
          },
        },
      },
    ],
    tool_choice: "auto",
  });

  if (completion.choices[0].message.tool_calls) {
    console.log("Tool calls:", completion.choices[0].message.tool_calls);
  } else {
    console.log("Response:", completion.choices[0].message.content);
  }
}

main();

Streaming Responses

Edgee supports streaming responses for real-time token delivery:
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://api.edgee.ai/v1",
  apiKey: process.env.EDGEE_API_KEY,
});

async function main() {
  const stream = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
      { role: "user", content: "Tell me a short story" }
    ],
    stream: true,
  });

  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content || "";
    process.stdout.write(content);
  }
}

main();

Migration from OpenAI

If you’re already using the OpenAI SDK, migrating to Edgee is straightforward:
  1. Change the base URL: Update baseURL from https://api.openai.com/v1 to https://api.edgee.ai/v1
  2. Update API key: Use your Edgee API key instead of your OpenAI key
  3. That’s it! Your existing code will work without any other changes
All OpenAI SDK features are supported, including streaming, function calling, and response formatting. Edgee maintains full compatibility with the OpenAI API specification.

What’s Next?