How to generate images with an API
Create images from text prompts with OpenAI and Google image models through one endpoint.
Call the images endpoint
Send a prompt to the images endpoint with an image model ID. Image models are billed per image, and higher resolutions cost more.
Example
from openai import OpenAI
client = OpenAI(api_key="YOUR_TOKENREX_KEY", base_url="https://tokenrex.ai/api/public")
img = client.images.generate(model="gpt-image-2", prompt="A lighthouse at dawn")
print(img.data[0].url or img.data[0].b64_json[:40])Questions
- Do I need a separate key for each provider?
- No. One TokenRex API key works for every enabled model; change only the model ID.
Related
- How to use the OpenAI SDK with a custom base URL
- How to call Claude with the OpenAI SDK
- How to call Gemini with the OpenAI SDK
- How to use the Anthropic Messages API on TokenRex
- How to stream LLM responses
- How to use function calling (tools) with an LLM API
- How to send images to a vision model
- How to convert text to speech with an API