# OpenAI

## 1. Overview

The industry's leading large language model. Generates images from text prompts.

**Model List:**

* `gpt-image-2`

## 2. Request Description

* **Request Method**: `POST`
* **Request URL**:

  > `https://gateway.theturbo.ai/v1/images/generations`

***

## 3. Request Parameters

### 3.1 Header Parameters

| Parameter Name  | Type   | Required | Description                                                | Example                |
| --------------- | ------ | -------- | ---------------------------------------------------------- | ---------------------- |
| `Content-Type`  | string | Yes      | Set the request header type, must be `application/json`    | `application/json`     |
| `Accept`        | string | Yes      | Set the response type, recommended as `application/json`   | `application/json`     |
| `Authorization` | string | Yes      | API\_KEY for authentication, format `Bearer $YOUR_API_KEY` | `Bearer $YOUR_API_KEY` |

***

### 3.2 Body Parameters (application/json)

<table><thead><tr><th width="96.3828125">Parameter Name</th><th width="98.30078125">Type</th><th width="107.2578125">Required</th><th width="278.05078125">Description</th><th>Example (Default)</th></tr></thead><tbody><tr><td><strong>model</strong></td><td>string</td><td>Yes</td><td>The model ID to use. See the available versions listed in <a href="#id-1.-overview">Overview</a>, such as <code>gpt-image-2</code>.</td><td><code>gpt-image-2</code></td></tr><tr><td><strong>prompt</strong></td><td>string</td><td>Yes</td><td>A text description of the desired image. <code>gpt-image-2</code> has a maximum description length of 32,000 characters.</td><td><code>A cute baby sea otter</code></td></tr><tr><td>n</td><td>number</td><td>No</td><td>The number of images to generate, must be between 1 and 10. <code>gpt-image-2</code> only supports n=1.</td><td><code>1</code></td></tr><tr><td>size</td><td>string</td><td>No</td><td>The size of the generated image. <code>gpt-image-2</code> supports arbitrary resolutions, specified as a <code>WIDTHxHEIGHT</code> string, where both <code>WIDTH</code> and <code>HEIGHT</code> must be divisible by 16, and the requested aspect ratio must be between <code>1:3</code> and <code>3:1</code>, with a maximum of <code>3840x2160</code>.</td><td><code>1024x1024</code></td></tr><tr><td>quality</td><td>string</td><td>No</td><td>The quality option for the generated image. <code>gpt-image-2</code> supports <code>high</code>, <code>medium</code>, <code>low</code>.</td><td><code>high</code></td></tr></tbody></table>

***

## 4. Request Examples

{% tabs %}
{% tab title="HTTP" %}

```http
POST /v1/images/generations
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "gpt-image-2",
	"prompt": "A cute baby sea otter",
	"n": 1,
	"size": "1024x1024"
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
curl https://gateway.theturbo.ai/v1/images/generations \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
	\"model\": \"gpt-image-2\",
	\"prompt\": \"A cute baby sea otter\",
	\"n\": 1,
	\"size\": \"1024x1024\"
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"os"

	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {

	apiKey := "sk-123456789012345678901234567890123456789012345678"
	exampleFilePath := "example.png"

	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		option.WithBaseURL("https://gateway.theturbo.ai/v1"),
	)

	resp, err := client.Images.Generate(
		context.Background(),
		openai.ImageGenerateParams{
			Model:          "gpt-image-2",
			Prompt:         "A cute baby sea otter",
			Size:           "1024x1024",
		},
	)

	if err != nil {
		fmt.Println("error:", err)
		return
	}

	if len(resp.Data) == 0 || resp.Data[0].B64JSON == "" {
		fmt.Println("error: empty b64_json")
		return
	}

	imageData, err := base64.StdEncoding.DecodeString(resp.Data[0].B64JSON)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	if err := os.WriteFile(exampleFilePath, imageData, 0644); err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println("success to write to file", exampleFilePath)
}

```

{% endtab %}

{% tab title="Python" %}

```python
#!/usr/bin/env python3

import base64
from openai import OpenAI

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"
    example_file_path = "example.png"

    client = OpenAI(
        api_key=api_key,
        base_url="https://gateway.theturbo.ai/v1"
    )

    response = client.images.generate(
        model="gpt-image-2",
        prompt="A cute baby sea otter",
        size="1024x1024",
    )

    if not response.data or not response.data[0].b64_json:
        print("error: empty b64_json")
        return

    image_data = base64.b64decode(response.data[0].b64_json)

    with open(example_file_path, "wb") as f:
        f.write(image_data)

    print("success to write to file", example_file_path)

if __name__ == "__main__":
    main()
```

{% endtab %}
{% endtabs %}

## 5. Response Example

{% tabs %}
{% tab title="b64\_json" %}

```json
{
	"created": 1589478378,
	"data": [{
			"b64_json": "..."
		},
		{
			"b64_json": "..."
		}
	],
	"usage": {
		"input_tokens": 0,
		"input_tokens_details": {
			"image_tokens": 0,
			"text_tokens": 0
		},
		"output_tokens": 0,
		"total_tokens": 0,
		"output_tokens_details": {
			"image_tokens": 0,
			"text_tokens": 0
		}
	}
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.console.zenlayer.com/api/compute/aig/image-generation/openai-image-generation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
