Skip to content

OpenAI Image Generation

1. Overview

OpenAI's image generation model family for creating images from text prompts.

2. Request

  • Method:POST
  • Endpoint:

    https://gateway.serevixai.ai/v1/images/generations
    

3. Parameters

3.1 Header Parameters

Parameter Type Required Description Example
Content-Type string Yes Sets the request content type. It must be application/json application/json
Accept string Yes Sets the response content type. The recommended value is application/json application/json
Authorization string Yes API key required for authentication, in the format Bearer $YOUR_API_KEY. Bearer $YOUR_API_KEY

3.2 Body Parameters (application/json)

ParameterTypeRequiredDescriptionExample (default)
modelstringYesThe model ID to use. See Model List for available versions, such as gpt-image-1.5.gpt-image-1.5
promptstringYesA text prompt describing the image to generate. gpt-image supports prompts up to 32,000 characters.A cute baby sea otter
nnumberNoNumber of images to generate. Must be between 1 and 10. gpt-image-1.5 supports only n=1.1
sizestringNoOutput image size. gpt-image-1.5 supports 1024x1024, 1792x1024, and 1024x1792; gpt-image-1 supports 1024x1024, 1536x1024, and 1024x1536.1024x1024
qualitystringNoImage quality option. gpt-image supports high, medium, and low.high

4. Request Examples

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

{
    "model": "gpt-image-1",
    "prompt": "A cute baby sea otter",
    "n": 1,
    "size": "1024x1024"
}
curl https://gateway.serevixai.ai/v1/images/generations \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $YOUR_API_KEY" \
    -d "{
    \"model\": \"gpt-image-1\",
    \"prompt\": \"A cute baby sea otter\",
    \"n\": 1,
    \"size\": \"1024x1024\"
}"
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.serevixai.ai/v1"),
    )

    resp, err := client.Images.Generate(
        context.Background(),
        openai.ImageGenerateParams{
            Model:          "gpt-image-1",
            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)
}
#!/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.serevixai.ai/v1"
    )

    response = client.images.generate(
        model="gpt-image-1",
        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()

5. Response Example

{
    "created": 1589478378,
    "data": [
        {
            "b64_json": "..."
        },
        {
            "b64_json": "..."
        }
    ]
}