APIs and capabilities
API Scripting Guide
Copy Python and Node.js examples for Responses, Chat Completions, Messages, image generation and editing, and video APIs.
These examples cover text, vision, image and video APIs. Replace YOUR_API_KEY with your API key before running them.
OpenAI Responses: text and vision
The OpenAI Responses API for text conversations, image understanding and streaming output.
POST /v1/responsesOpenAI Chat Completions: text and vision
Compatible with the traditional OpenAI Chat format for migrating existing clients.
POST /v1/chat/completionsAnthropic Messages: text and vision
Claude / Anthropic Messages format. Image mode encodes the image as Base64 before sending it.
POST /v1/messagesGPT / Gemini image generation and editing
Use /v1/images/generations for text-to-image and /v1/images/edits for reference images or editing. Edits accept local files as multipart, multiple reference images, or JSON images[].image_url containing an image URL or data URL. For the Gemini (Official Direct) group, replace model with a Gemini image model listed in the Image Generation Guide, keeping stream=true, n=1 and response_format=b64_json. Gemini returns usage and usageMetadata; mime_type identifies the image format. size maps to an aspect ratio and a 1K/2K/4K tier; actual dimensions depend on the model, and the 2.5 series only supports 1K. Gemini currently does not support mask, n greater than 1, forced output_format, or quality/background values other than auto.
POST /v1/images/generationsgpt-image-2 image editing (multipart)
Upload an image, edit it according to the prompt, and save the result from streaming events.
POST /v1/images/editsgrok-imagine-video video generation
Asynchronous video generation: submit a task to obtain a request_id, poll until completion, then download the video file.
POST /v1/videos/generationsCommon error: expected struct ImageUrl
If the response says "Failed to deserialize the JSON body into the target type: image: invalid type: string https://..., expected struct ImageUrl", the image field was supplied as a string instead of an object. Correct: {"image": {"url": "https://example.com/a.png"}}. The format {"image": {"image_url": "https://example.com/a.png"}} is also accepted. Incorrect: {"image": "https://example.com/a.png"}. For local images, replace url with a data URL, such as "data:image/png;base64,<BASE64>".
Parameters and billing
resolution supports 480p / 720p. duration defaults to 8 seconds and accepts 1–15 seconds. Billing is per second. Generation is asynchronous: /v1/videos/generations returns request_id; poll GET /v1/videos/{request_id} for status and video.url, then download the video bytes with GET /v1/videos/{request_id}/content. video.url is a temporary signed download URL. Download and save the file promptly.