> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ai-stats.phaseo.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Migrating from OpenAI SDK

> Step-by-step guide to migrate your OpenAI SDK integration to AI Stats Gateway.

# Migrating from OpenAI SDK

This guide will help you migrate your existing OpenAI SDK (Python or Node.js) integration to use the AI Stats Gateway instead.

## Overview

AI Stats Gateway provides OpenAI-compatible endpoints, making migration straightforward. You'll primarily need to:

* Change the API base URL
* Update your API key
* Update authentication method (from `api_key` to `Authorization: Bearer`)

## Prerequisites

* An AI Stats account and API key (get one at [Gateway Keys](https://ai-stats.phaseo.app/gateway/keys))
* Existing OpenAI SDK code

## Migration Steps

### 1. Update API Key and Base URL

Instead of using the OpenAI API key and default base URL, use your AI Stats credentials.

<CodeGroup>
  ```bash theme={null}
  # Before (OpenAI)
  curl https://api.openai.com/v1/chat/completions \
    -H "Authorization: Bearer sk-your-openai-key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4",
      "messages": [{"role": "user", "content": "Hello!"}]
    }'

  # After (AI Stats)

  curl https://api.phaseo.app/v1/chat/completions \
   -H "Authorization: Bearer your-ai-stats-key" \
   -H "Content-Type: application/json" \
   -d '{
  "model": "gpt-4",
  "messages": [{"role": "user", "content": "Hello!"}]
  }'

  ```

  ```python theme={null}
  import openai

  # Before
  client = openai.OpenAI(api_key="sk-your-openai-key")

  # After
  client = openai.OpenAI(
      api_key="your-ai-stats-key",
      base_url="https://api.phaseo.app/v1"
  )

  response = client.chat.completions.create(
      model="gpt-4",
      messages=[{"role": "user", "content": "Hello!"}]
  )
  ```

  ```javascript theme={null}
  import OpenAI from "openai";

  // Before
  const client = new OpenAI({
  	apiKey: "sk-your-openai-key",
  });

  // After
  const client = new OpenAI({
  	apiKey: "your-ai-stats-key",
  	baseURL: "https://api.phaseo.app/v1",
  });

  const response = await client.chat.completions.create({
  	model: "gpt-4",
  	messages: [{ role: "user", content: "Hello!" }],
  });
  ```
</CodeGroup>

### 3. Test Your Integration

Run your existing code with the updated configuration to ensure it works.

### 4. Handle Model Names

AI Stats supports a wide range of models. Check the [models endpoint](../api-reference/endpoint/models.mdx) for available models and their names.

Some model names may differ from OpenAI's naming convention.

## Common Issues

* **Model not found**: Ensure the model name is available in AI Stats. Use the `/gateway/models` endpoint to list available models.
* **Rate limits**: AI Stats may have different rate limits than OpenAI.
* **Features**: Some OpenAI-specific features may not be available or work differently.

## Next Steps

* Explore AI Stats' additional features like analytics and multi-provider routing.
* Check out the [API Reference](../api-reference/introduction.mdx) for full documentation.
