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

# Compat introduction

# OpenAI Compat API

The **OpenAI Compat API** is a **drop-in replacement** for OpenAI's official Images API. Fully compatible with the official **`openai`** SDK (Python & Node.js) and the raw OpenAI REST interface — just change the `base_url`.

<Tip>
  **No code changes needed** — if you already use OpenAI's `images.generate` / `images.edit`, simply override `base_url` to `https://api.mountsea.ai/openai/v1` with your Mountsea API key.
</Tip>

## Why Use Compat API?

<CardGroup cols={2}>
  <Card title="Official SDK Support" icon="code">
    Works with OpenAI's official `openai` Python and Node.js SDKs
  </Card>

  <Card title="Same API Shape" icon="equals">
    Same request/response format as `https://api.openai.com/v1/images/*`
  </Card>

  <Card title="Synchronous" icon="bolt">
    Returns the generated image directly — no polling needed
  </Card>

  <Card title="Unified Billing" icon="wallet">
    Single API key, unified usage tracking through Mountsea
  </Card>
</CardGroup>

## Configuration

### Base URL

```
https://api.mountsea.ai/openai/v1
```

### Authentication

Use your **Mountsea API key**:

* HTTP header: `Authorization: Bearer your-api-key`
* Or set via the SDK's `api_key` / `apiKey` parameter

## Supported Endpoints

| Endpoint                        | Method | Description                                               |
| ------------------------------- | ------ | --------------------------------------------------------- |
| `/openai/v1/images/generations` | POST   | Text-to-image (JSON) — identical to OpenAI                |
| `/openai/v1/images/edits`       | POST   | Image editing (multipart/form-data) — identical to OpenAI |

## Supported Model

| Model         | Description                         |
| ------------- | ----------------------------------- |
| `gpt-image-2` | Latest OpenAI image model (default) |

***

## Using the Official `openai` SDK

### Installation

<CodeGroup>
  ```bash pip theme={null}
  pip install openai
  ```

  ```bash npm theme={null}
  npm install openai
  ```

  ```bash pnpm theme={null}
  pnpm add openai
  ```
</CodeGroup>

### Python

<CodeGroup>
  ```python Text-to-Image theme={null}
  from openai import OpenAI
  import base64

  client = OpenAI(
      api_key="your-mountsea-api-key",
      base_url="https://api.mountsea.ai/openai/v1",
  )

  response = client.images.generate(
      model="gpt-image-2",
      prompt="A photorealistic cat wearing a space helmet, floating in orbit",
      size="1024x1024",
      quality="high",
      n=1,
  )

  # gpt-image-2 always returns b64_json
  image_b64 = response.data[0].b64_json
  with open("output.png", "wb") as f:
      f.write(base64.b64decode(image_b64))
  ```

  ```python Image Edit theme={null}
  from openai import OpenAI
  import base64

  client = OpenAI(
      api_key="your-mountsea-api-key",
      base_url="https://api.mountsea.ai/openai/v1",
  )

  with open("source.png", "rb") as img:
      response = client.images.edit(
          model="gpt-image-2",
          image=img,
          prompt="Add a dramatic sunset sky with orange and purple clouds",
          size="1024x1024",
          input_fidelity="high",
      )

  image_b64 = response.data[0].b64_json
  with open("edited.png", "wb") as f:
      f.write(base64.b64decode(image_b64))
  ```

  ```python Inpainting (with mask) theme={null}
  from openai import OpenAI
  import base64

  client = OpenAI(
      api_key="your-mountsea-api-key",
      base_url="https://api.mountsea.ai/openai/v1",
  )

  with open("source.png", "rb") as img, open("mask.png", "rb") as mask:
      response = client.images.edit(
          model="gpt-image-2",
          image=img,
          mask=mask,
          prompt="Replace the masked area with a sunset beach",
          size="1024x1024",
      )

  image_b64 = response.data[0].b64_json
  with open("inpainted.png", "wb") as f:
      f.write(base64.b64decode(image_b64))
  ```
</CodeGroup>

### Node.js / TypeScript

<CodeGroup>
  ```typescript Text-to-Image theme={null}
  import OpenAI from 'openai';
  import fs from 'fs';

  const client = new OpenAI({
    apiKey: 'your-mountsea-api-key',
    baseURL: 'https://api.mountsea.ai/openai/v1',
  });

  const response = await client.images.generate({
    model: 'gpt-image-2',
    prompt: 'A photorealistic cat wearing a space helmet, floating in orbit',
    size: '1024x1024',
    quality: 'high',
    n: 1,
  });

  const imageB64 = response.data[0].b64_json!;
  fs.writeFileSync('output.png', Buffer.from(imageB64, 'base64'));
  ```

  ```typescript Image Edit theme={null}
  import OpenAI, { toFile } from 'openai';
  import fs from 'fs';

  const client = new OpenAI({
    apiKey: 'your-mountsea-api-key',
    baseURL: 'https://api.mountsea.ai/openai/v1',
  });

  const response = await client.images.edit({
    model: 'gpt-image-2',
    image: await toFile(fs.createReadStream('source.png'), 'source.png'),
    prompt: 'Add a dramatic sunset sky with orange and purple clouds',
    size: '1024x1024',
    input_fidelity: 'high',
  });

  const imageB64 = response.data[0].b64_json!;
  fs.writeFileSync('edited.png', Buffer.from(imageB64, 'base64'));
  ```

  ```typescript Inpainting (with mask) theme={null}
  import OpenAI, { toFile } from 'openai';
  import fs from 'fs';

  const client = new OpenAI({
    apiKey: 'your-mountsea-api-key',
    baseURL: 'https://api.mountsea.ai/openai/v1',
  });

  const response = await client.images.edit({
    model: 'gpt-image-2',
    image: await toFile(fs.createReadStream('source.png'), 'source.png'),
    mask: await toFile(fs.createReadStream('mask.png'), 'mask.png'),
    prompt: 'Replace the masked area with a sunset beach',
    size: '1024x1024',
  });

  const imageB64 = response.data[0].b64_json!;
  fs.writeFileSync('inpainted.png', Buffer.from(imageB64, 'base64'));
  ```
</CodeGroup>

***

## Using the REST API Directly

If you don't want to use the SDK, you can call the endpoints directly.

### Text-to-Image (JSON)

```bash theme={null}
curl -X POST "https://api.mountsea.ai/openai/v1/images/generations" \
  -H "Authorization: Bearer your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "A photorealistic cat wearing a space helmet, floating in orbit",
    "size": "1024x1024",
    "quality": "high",
    "n": 1
  }'
```

### Image Edit (multipart/form-data)

```bash theme={null}
curl -X POST "https://api.mountsea.ai/openai/v1/images/edits" \
  -H "Authorization: Bearer your-api-key" \
  -F "model=gpt-image-2" \
  -F "image=@source.png" \
  -F "prompt=Add a dramatic sunset sky" \
  -F "size=1024x1024" \
  -F "input_fidelity=high"
```

### Inpainting (multipart/form-data)

```bash theme={null}
curl -X POST "https://api.mountsea.ai/openai/v1/images/edits" \
  -H "Authorization: Bearer your-api-key" \
  -F "model=gpt-image-2" \
  -F "image=@source.png" \
  -F "mask=@mask.png" \
  -F "prompt=Replace the masked area with a sunset beach" \
  -F "size=1024x1024"
```

***

## Request Parameters

### `images/generations` (JSON)

| Field                | Type              | Description                                                 |
| -------------------- | ----------------- | ----------------------------------------------------------- |
| `prompt`             | string (required) | Prompt for image generation                                 |
| `model`              | string            | Defaults to `gpt-image-2`                                   |
| `size`               | enum              | `auto`, `1024x1024`, `1024x1536`, `1536x1024`               |
| `n`                  | number            | Currently only 1                                            |
| `quality`            | enum              | `auto`, `low`, `medium`, `high`, `standard`                 |
| `background`         | enum              | `transparent`, `opaque`, `auto`                             |
| `output_format`      | enum              | `png`, `jpeg`, `webp`                                       |
| `output_compression` | number            | 0-100 for JPEG/WebP                                         |
| `moderation`         | enum              | `auto`, `low`                                               |
| `response_format`    | enum              | `url` or `b64_json` (gpt-image-2 always returns `b64_json`) |
| `user`               | string            | End-user identifier for abuse monitoring                    |

### `images/edits` (multipart/form-data)

All fields above plus:

| Field            | Type            | Description                                        |
| ---------------- | --------------- | -------------------------------------------------- |
| `image`          | file (required) | Source image                                       |
| `mask`           | file            | Optional mask; transparent areas will be repainted |
| `input_fidelity` | `high` / `low`  | How closely the output follows the input image     |

***

## Response Format

Identical to OpenAI's official response:

```json theme={null}
{
  "created": 1712345678,
  "data": [
    {
      "b64_json": "<base64 encoded image>",
      "revised_prompt": "<model's improved prompt>"
    }
  ],
  "background": "opaque",
  "output_format": "png",
  "quality": "high",
  "size": "1024x1024",
  "usage": {
    "input_tokens": 45,
    "input_tokens_details": { "image_tokens": 0, "text_tokens": 45 },
    "output_tokens": 3200,
    "total_tokens": 3245
  }
}
```

<Info>
  `gpt-image-2` always returns the image as `b64_json`. The `url` response format is only kept for DALL·E compatibility and will be ignored for gpt-image models.
</Info>

***

## Notes & Limitations

<Warning>
  Currently `n` only supports `1`. Multi-image generation per request is not yet supported.
</Warning>

* All standard OpenAI parameters are supported; unknown parameters are ignored gracefully.
* `response_format=url` is accepted but gpt-image-2 will still return `b64_json`.
* Both `openai` SDK and raw REST calls work identically.

***

## Explore the API Documentation

* [images.generate (Compat)](compat-generate) — OpenAI-compatible text-to-image endpoint
* [images.edit (Compat)](compat-edit) — OpenAI-compatible image editing endpoint
