Request icon
LoopUntil icon
If icon
Fail icon
Log icon

Generate an AI Image with Magic Hour and Wait for Completion

Generate AI images with Magic Hour in Kestra, safely poll one project to completion, and retrieve the final download URL.

Categories
AIBusiness

Turn an image prompt into a completed Magic Hour asset from a reusable Kestra workflow. The flow starts one billable image project, stores its project ID, polls that same project until it reaches a terminal state, and reports the exact download URL and expiration time. Reusing the original ID is essential: starting a new project after an ambiguous timeout can create duplicate renders and extra API spend.

How it works

  1. create_image (io.kestra.plugin.core.http.Request) sends the prompt and aspect ratio to POST /v1/ai-image-generator. It requests one image with Magic Hour's default model and stores the returned project ID in {{ outputs.create_image.body | jq('.id') | first }}.
  2. wait_for_image (io.kestra.plugin.core.flow.LoopUntil) calls GET /v1/image-projects/{id} every 10 seconds. It stops only when the same project reaches complete or error, with a 10-minute maximum duration.
  3. fail_render_error converts Magic Hour's terminal error state into a failed Kestra execution that includes the API error details.
  4. report_completed_image logs the project ID, exact download URL, and expiration timestamp from the completed project.

Prerequisites

  • A running Kestra instance with outbound HTTPS access to api.magichour.ai.
  • A Magic Hour account with API credits and an API key from the developer dashboard.

Secrets

  • MAGIC_HOUR_API_KEY: authenticates the create and project-status requests. Each Kestra user should supply their own key; never place it directly in the flow YAML.

Inputs

  • prompt (STRING): required image description. The default demonstrates product photography.
  • aspect_ratio (SELECT): 1:1, 16:9, or 9:16; defaults to 1:1.

Expected outputs

  • Project ID: {{ outputs.create_image.body | jq('.id') | first }}.
  • Final URL: {{ outputs.poll_project.body | jq('.downloads[0].url') | first }}.
  • URL expiration: {{ outputs.poll_project.body | jq('.downloads[0].expires_at') | first }}.

Download URLs expire. Copy a completed asset to durable storage in a downstream task when the workflow needs to retain it.

Quick start

  1. Create a Magic Hour API key at https://magichour.ai/developer and save it as the Kestra secret MAGIC_HOUR_API_KEY.
  2. Import this flow and choose a prompt and aspect ratio.
  3. Execute the flow. The create request consumes Magic Hour API credits; the status polls do not create more projects.
  4. Open report_completed_image to copy the final URL before it expires.

How to extend

  • Add io.kestra.plugin.core.http.Download and an S3, GCS, or Azure Blob upload task to persist the completed image.
  • Trigger the flow from an approved campaign brief, product catalog row, or webhook.
  • Replace the create endpoint and body with Magic Hour image-to-video or lip-sync while keeping the same one-project polling pattern.
  • Add an editorial approval task before distributing the generated asset.

Links

Share this Blueprint
See How

New to Kestra?

Use blueprints to kickstart your first workflows.