Image generation

Generate images from a text prompt.

Compatible with the OpenAI image generation endpoint. Image models are listed on the Model Square, for example gpt-image-1. To change an existing image, see Image edits.

POST https://tokenfunc.com/v1/images/generations

cURL

curl https://tokenfunc.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \
  -d '{
    "model": "gpt-image-1",
    "prompt": "An orange cat wearing a scarf sitting on a windowsill, watercolor style",
    "size": "1024x1024",
    "n": 1
  }'

Python

import base64
from openai import OpenAI

client = OpenAI(base_url="https://tokenfunc.com/v1", api_key="sk-xxxxxxxxxxxxxxxx")

resp = client.images.generate(
    model="gpt-image-1",
    prompt="An orange cat wearing a scarf sitting on a windowsill, watercolor style",
    size="1024x1024",
)
image = resp.data[0]
if image.b64_json:
    with open("cat.png", "wb") as f:
        f.write(base64.b64decode(image.b64_json))
else:
    print(image.url)

Node.js

import { writeFileSync } from 'node:fs'

import OpenAI from 'openai'

const client = new OpenAI({ baseURL: 'https://tokenfunc.com/v1', apiKey: 'sk-xxxxxxxxxxxxxxxx' })

const resp = await client.images.generate({
  model: 'gpt-image-1',
  prompt:
    'An orange cat wearing a scarf sitting on a windowsill, watercolor style',
  size: '1024x1024',
})
const image = resp.data[0]
if (image.b64_json)
  writeFileSync('cat.png', Buffer.from(image.b64_json, 'base64'))
else console.log(image.url)

Request parameters

200

{
  "created": 1760000000,
  "data": [{ "b64_json": "iVBORw0KGgo..." }],
  "background": "opaque",
  "output_format": "png",
  "size": "1024x1024",
  "quality": "medium",
  "usage": {
    "total_tokens": 1102,
    "input_tokens": 46,
    "output_tokens": 1056,
    "input_tokens_details": { "text_tokens": 46, "image_tokens": 0 }
  }
}

Response fields

Streaming

With stream: true the result comes back over SSE. If you set partial_images, you first get a few image_generation.partial_image events (in-progress previews); the final image_generation.completed event carries the finished image and usage:

event: image_generation.partial_image
data: {"type":"image_generation.partial_image","b64_json":"...","partial_image_index":0}

event: image_generation.completed
data: {"type":"image_generation.completed","b64_json":"...","usage":{"total_tokens":1102,"input_tokens":46,"output_tokens":1056,"input_tokens_details":{"text_tokens":46,"image_tokens":0}}}

Billing

Image models usually bill per image, with prices varying by size and quality; some bill per token. See the Model Square for prices.