All posts
How do I generate AI images with an API? A TokenRex guide
Tutorials 11 min read· Oct 22, 2026· By Guides

How do I generate AI images with an API? A TokenRex guide

Generate and save your first AI image, choose GPT or Gemini settings, check live prices and troubleshoot transparent output or incorrect resolution.

You can generate AI images through TokenRex using an enabled image model, one API key and the Images endpoint. Keep the key on your server, compare current prices and save the returned file before embedding it in your application.

What do I need before the first request?#

  • Create a TokenRex account and an API key; keep it in a private environment variable.
  • Confirm wallet balance and the chosen model's enabled status in Models & Pricing.
  • Install the OpenAI Python SDK with pip install openai.
  • Decide the subject, intended dimensions and whether the output must be transparent.

Generate and save a PNG#

python
import os, base64
from openai import OpenAI

client = OpenAI(api_key=os.environ["TOKENREX_API_KEY"], base_url="https://tokenrex.ai/api/public/v1")
result = client.images.generate(
    model="gpt-image-2.5-flare",
    prompt="A clear product photograph of a blue ceramic mug on a white table",
    quality="medium",
    output_format="png",
)
with open("mug.png", "wb") as output:
    output.write(base64.b64decode(result.data[0].b64_json))

Can I generate a transparent background?#

For GPT Image 2.5 Flare or Sunburst, add background="transparent" and request PNG or WebP. Do not use JPEG. A checkerboard drawn into the image is not genuine alpha transparency. See the full transparent-image steps, including how to inspect the result.

How do I select Gemini 2K or 4K output?#

Use an enabled gemini-3.1-flash-image-2k or gemini-3.1-flash-image-4k ID, or the corresponding Gemini 3 Pro Image ID. Resolution is selected by ID on TokenRex, not an OpenAI size parameter. Read the resolution guide for a complete request.

What does image generation cost?#

GPT image models use provider-reported token usage; Gemini image tiers use per-image rates. Larger output or higher quality can raise cost. Check live model prices, compare image options and estimate the intended workload in the cost calculator. Prices in an old article should never be treated as a current quote.

Troubleshoot common failures#

  • Authentication error: check the private TokenRex key and base URL.
  • Unknown or disabled model: copy the exact short ID from Models & Pricing.
  • Unsupported control: remove settings not exposed for the selected model; Gemini and GPT controls differ.
  • Transparent JPEG: change the format to PNG or WebP.
  • Wrong resolution: select the correct Gemini suffix before submitting.
  • No usable image: inspect the returned error or refusal instead of automatically repeating a paid request.

Next steps and sources#

Compare Gemini vs GPT Image, choose PNG, JPEG or WebP, or open Docs. Provider background: OpenAI image generation and Google Gemini image generation. Local controls can be narrower than provider-native capabilities.

Models in this article

Review current prices, specifications and source-linked benchmarks.

Build this with TokenRex

TokenRex is an LLM marketplace by Asirex Labs. Choose an enabled model, create a private API key and use the supported endpoint shown in Docs. Check live prices before your first request.