Skip to content
AnswerWell geo Documentation

Research · 9 min read · Answerwell

Image and Video Model Run API: Invocation, Parameters, and Async Delivery

How to call Cloudflare Workers AI image and video models via env.AI.run or REST, pick model IDs and inputs, read varied responses, and use webhooks and AI Gateway.

This page summarizes Cloudflare documentation for running catalog image, video, and audio models from a Worker with the AI binding or from the account REST endpoint POST .../ai/run. The same logical inputs appear in both paths; what changes is transport, how long you keep the connection open, and— for some providers—whether you call the catalog or AI Gateway instead.

env.AI.run and REST ai/run: how to call a model from a Worker

Section titled “env.AI.run and REST ai/run: how to call a model from a Worker”

From a Worker with an AI binding, you pass the model ID string and an object of input fields documented on that model’s page:

const response = await env.AI.run('<model-id>', { /* input fields */ });

Some examples add a third argument for gateway routing—for instance Kling v3 image uses { gateway: { id: 'default' } }.

The REST equivalent for most catalog models is:

curl "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run" \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"model": "<model-id>",
"input": { }
}'

Model pages (Seedream 4.5, Veo 3.1, Bria FIBO edit) show parallel Worker and cURL examples with the same fields inside input.

Model ID strings: catalog vendor/model vs @cf/ paths

Section titled “Model ID strings: catalog vendor/model vs @cf/ paths”

Catalog partner models use IDs like bytedance/seedream-5-pro, google/veo-3.1-fast, or bria/fibo-generate-1.5. Workers-hosted models use the @cf/ prefix, e.g. @cf/black-forest-labs/flux-1-schnell (documented on both the @cf path and workers-ai flux-1-schnell pages). The ID in env.AI.run('…') must match the string at the top of that model’s documentation.

Prompt fields and modality: what to pass in input

Section titled “Prompt fields and modality: what to pass in input”

Most text-to-image and text-to-video models expect prompt. Google Gemini Omni video models (gemini-omni-flash, gemini-omni-1.1-flash) use text instead—using prompt there is a common integration mistake.

Reference and edit flows use different names even within one vendor family:

Pattern Example models Parameter
Single reference URL Seedream 5 Pro image
One or more reference URLs Seedream 4.5, Seedream 5 Lite image_input
Image-to-video Wan 2.7 I2V, Alibaba HH1 I2V image + prompt
Multi-image video HH1.1 R2V, MiniMax H3 images[] or content[] with image_url roles
Masked edit Bria FIBO Edit 1.5 instruction, images[], optional mask
Video upscale / enhance Topaz fast upscale video_uri, frame metadata—not a text prompt

Aspect ratio, size, resolution, and duration

Section titled “Aspect ratio, size, resolution, and duration”

Vendors name sizing differently. Copy values exactly from the model page:

Concept Typical parameter names Examples in cluster
Aspect aspect_ratio, ratio Seedream '21:9', Kling v4 21:9; Veo '16:9' / '9:16'
Named or pixel size size Seedream '4K', '2K', '1536x864'; Wan 2.6 image '1024x768'
Video resolution resolution Veo '1080p'; Vidu '540p'–'1080p'; Wan I2V '1080P' (capital P in docs)
Duration duration Veo string '8s'; Vidu/Wan number seconds; Pixverse v6 up to 1–15 seconds
Audio generate_audio, audio Veo 3.1, FLUX 3 video; Vidu audio: false for silent output

Seedream 4.0 supports enhance_prompt. Seedream 4.5 and 5 Lite add sequential output via sequential_image_generation: 'auto' and max_images. Bria FIBO Generate 1.5 documents that resolution: '4MP' adds about 30 seconds of latency compared with 1MP in its examples.

Response shape: URL, base64, arrays, and jobs

Section titled “Response shape: URL, base64, arrays, and jobs”

Most catalog REST examples return a envelope with state: "Completed", a result object, and often gatewayMetadata.keySource (Unified or BYOK for bring-your-own-key paths on models such as OpenAI GPT Image).

Parse output keys per model, not globally:

Output pattern When it appears
result.image (string URL) Seedream 4.0, Wan 2.6 image, many Bria/OpenAI image models
result.images[] Seedream 4.5+, Qwen Image 3.0 Pro with n variants
result.video Veo 3.1, Vidu Q3, Bria v-rmbg 3.0, Topaz fast upscale
result.audio ElevenLabs music v2
Base64 on Worker response @cf flux-1-schnell — response.image, not always the state/result wrapper
Task / job fields MiniMax H3, Krea 3 — check status and URL fields in the doc snippet

For masked edits, Bria FIBO Edit 1.5 documents that white areas of the mask are edited and black areas are kept. OpenAI GPT Image 2 with multiple images[] entries routes the call to OpenAI’s /v1/images/edits endpoint and supports up to 16 reference images for multi-edit workflows.

gatewayMetadata.keySource: Unified vs BYOK

Section titled “gatewayMetadata.keySource: Unified vs BYOK”

Unified indicates Cloudflare’s unified billing/key path in newer examples. BYOK appears when the underlying provider expects your API key to be configured (common on OpenAI GPT Image and some Alibaba HH1 examples). The cluster does not document billing setup detail—treat BYOK as a signal to confirm key configuration before production use.

Background ai/run requests and webhooks for long jobs

Section titled “Background ai/run requests and webhooks for long jobs”

By default, synchronous /ai/run calls keep the connection open until the model finishes. Cloudflare’s REST API — Background requests and webhooks states:

By default, /ai/run requests are synchronous — the connection stays open until the model finishes and the result comes back in the response. For long-running models — such as image, video, or audio generation — or when you do not want to hold a connection open, run the request in the background and have AI Gateway notify a webhook when it completes.

Set options.background: true and options.webhookUrl alongside model and input in the request body. The same page notes:

webhookUrl can only be provided when background is true. Providing a webhookUrl without background: true returns a 400 error.

Use background mode for video, music, and high-resolution image runs (for example google/nano-banana in the webhook example, or multi-megapixel Bria FIBO). Authentication for REST tests typically uses $CLOUDFLARE_API_TOKEN and account ID (see wrangler whoami / wrangler auth token in that doc’s examples).

AI Gateway vs catalog ai/run: Fal, Ideogram, and Replicate

Section titled “AI Gateway vs catalog ai/run: Fal, Ideogram, and Replicate”

Not every third-party surface goes through the catalog body-only ai/run pattern.

  • Fal AI on AI Gateway — base https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/fal, header x-fal-target-url pointing at the Fal queue URL, auth with a Fal token or Cloudflare API token when using BYOK.
  • Ideogram on AI Gateway — POST to .../ideogram/v1/ideogram-v3/generate with an Api-Key header.

Use these when you need the provider’s native HTTP API behind Cloudflare’s gateway, not the Workers AI model slug.

Pruna P-video on Replicate through AI Gateway

Section titled “Pruna P-video on Replicate through AI Gateway”

The Pruna P-video tutorial explains:

This tutorial shows how to call the Pruna’s P-video ↗︎ model on Replicate through AI Gateway.

Endpoint pattern: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions. Headers include Replicate Authorization, Cloudflare cf-aig-authorization, and optionally:

Prefer: wait — blocks until the prediction completes instead of returning immediately.

For runs expected under Replicate’s 60-second synchronous limit:

Because this is under Replicate’s 60-second synchronous limit, you can use the Prefer: wait header to send a request and get the result in a single call:

Longer renders should omit Prefer: wait, then poll GET .../replicate/predictions/{prediction_id} until status is succeeded or failed; the output field then holds the video URL. This path is not the same as env.AI.run('pruna/p-video', …) on the Workers AI catalog (Pruna P-video exists in-catalog, but the tutorial is gateway-specific).

Choosing a model family (image, video, enhance, utility)

Section titled “Choosing a model family (image, video, enhance, utility)”

Use the model page as the source of truth; the table below groups common tasks from this cluster.

  • Google: Veo 3.1 (1080p, generate_audio) vs Veo 3.1 Fast (examples emphasize 720p and shorter social clips); Gemini Omni uses text + resolution/aspect_ratio.
  • Alibaba: Wan 3.0 / Prime with ratio: 'adaptive'; Wan 2.7 I2V with seed for reproducibility.
  • Keyframes / references: Kling v4 (first_frame, middle_frames[], last_frame, prompt @image_1 refs); MiniMax H3 Max with content[] roles.
  • Duration / quality trade-offs: Vidu Q3 Turbo (up to 16s, 540p preview) vs Q3 Pro; Pixverse v6 supports 1–15 seconds per example headings.

Chaining video, frames, vision, and storage in Workers

Section titled “Chaining video, frames, vision, and storage in Workers”

Bind Media Transformations to Workers shows clipping, frame extraction, and R2 upload via env.MEDIA.input(video).transform(…).output({ mode: "frame", time: "2s", format: "jpg" }), then feeding a frame into a vision model—for example env.AI.run("@cf/unum/uform-gen2-qwen-500m", { image: Uint8Array, prompt, max_tokens }).

To transcode a generated image before R2 storage, Transcode an image from Workers AI before uploading to R2 documents:

Transcode an image from Workers AI before uploading to R2

Flow: env.AI.run → image stream → env.IMAGES.input(stream).output({ format: "image/avif" }) → env.R2.put. That example uses @cf/bytedance/stable-diffusion-xl-lightning, illustrating another @cf/ ID alongside catalog bytedance/seedream-* slugs.

Utility outputs can be non-URL JSON: Bria Ad Delayer returns layered ad structures with message text such as “Constructed a layered ad.” in examples.

Common mistakes when calling image and video models

Section titled “Common mistakes when calling image and video models”
  1. Wrong prompt field on Gemini Omni video (text, not prompt).
  2. Wrong response parser — especially result.image vs result.images[] across Seedream versions; flux-1-schnell base64 on response.image.
  3. webhookUrl without background: true → HTTP 400 per background requests doc.
  4. Mixing REST URL/body styles (catalog model+input vs @cf path POST).
  5. Assuming BYOK works without provider keys when gatewayMetadata.keySource is BYOK.
  6. Duration type mismatch — Veo '8s' strings vs Vidu integer seconds.
  7. Calling Pruna only via env.AI.run when your integration follows the Replicate gateway tutorial instead.
  1. Confirm account context with wrangler whoami and an API token with access to AI, as shown in REST API — Background requests and webhooks.

  2. Deploy or run a minimal Worker that calls env.AI.run with a model ID from that model’s page (for example Seedream 4.5 or Veo 3.1 Fast) and log the top-level keys you receive.

  3. Repeat the same input via POST .../ai/run with Authorization: Bearer $CLOUDFLARE_API_TOKEN and compare state, result, and gatewayMetadata to the Worker response.

  4. For a long-running model, send options: { background: true, webhookUrl: "https://…" } per REST API — Background requests and webhooks and confirm the immediate response plus webhook delivery; if using Replicate for P-video, test with and without Prefer: wait per Use Pruna P-video through AI Gateway.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match the linked Cloudflare documentation or your live API responses, email yimasynthetic@gmail.com with the page URL and the exact statement or model ID you are using so it can be corrected or clarified.