Skip to main content
This guide shows how to use Python to generate images and videos, optionally from your own reference images and clips. For the full request and response fields, see the Images and Videos API reference. Every generation follows the same steps:
  1. Optionally upload reference images or videos. Each upload returns an ID.
  2. Start a job with POST /image or POST /video. The response contains a job_id and the credits the job will cost.
  3. Poll GET /image/{job_id} or GET /video/{job_id} until the job has succeeded, then download the result.

Prerequisites

  • Python 3.6+
  • requests library (pip install requests)
  • An API key, passed in the X-API-KEY header. See Authentication for how to create one.

Choosing a model

List the available models, with the resolutions, aspect ratios and credits each one supports. The list changes as models are added, so read model IDs from here instead of hard-coding them.
Resolutions are one of 480p, 720p, 1080p, 2k or 4k, and each model lists the ones it supports. Video models also return a label for each resolution with the size actually rendered. For example, a model whose lowest tier renders at 360p lists it as 480p with the label 360p. When you leave out resolution, aspect_ratio or duration, the model’s defaults are used.

Generating an image

Editing and combining images

Upload images first, then pass their IDs in reference_image_ids. The first image is the one being edited; the rest are extra references. Refer to them in the prompt as @Image 1, @Image 2 and so on, in the same order. Only models with supports_reference_images accept references.
Uploads accept PNG, JPEG or WebP images up to 20 MB. Images generated by an earlier job can be used as references too: use the image_id from the job result.

Generating a video

The kind of video is set by which inputs you send. Each model lists the modes it supports in modes. Send inputs for one mode only; mixing them returns a 422.

Text to video

Animating an image

Pass an uploaded image, or the image_id of a generated image, as the start frame. Add end_frame_image_id to control where the clip ends.

Using reference images and videos

Upload reference clips with POST /video/upload, then refer to every input in the prompt by type and position: @Image 1, @Video 1.
Video uploads accept MP4 or MOV files up to 100 MB and 60 seconds. Each model limits how many references it takes (max_reference_images, max_reference_videos) and how long reference clips may be. Requests outside a limit return a 400 that names it.

Editing a video

Pass the clip to edit as source_video_id. For models with output_duration_follows_source, the result is as long as the source clip, so leave out duration. You’re charged for the clip’s length.

Credits

  • Images are charged per generated image, at the price for the chosen resolution (credits_by_resolution).
  • Videos are charged per second of generated video, at the price for the chosen resolution (credits_per_second).
  • Every create request returns estimated_credits. Credits are checked before the job starts and charged once the result is ready.
  • Uploads are free.

Error handling

Image and video jobs don’t return a 429 when you start many at once: they queue and start as capacity frees up. A job that fails returns status: "failed" with a readable error, and isn’t charged.