> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modular.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Video generation

> Generate videos from text or images with the responses API

To use a video generation model, you need to use the [responses
API](/api/inference/create-response). The API accepts text and image inputs,
and returns generated videos as base64-encoded data.

<Warning>
  **Notice:** We currently don't offer any shared endpoints for video generation,
  so the examples below work with dedicated endpoints only.
</Warning>

## Requirements

* An [API key](/administration/api-keys). The code below assumes you set it
  in an environment variable:

  <CodeGroup>
    ```bash macOS/Linux theme={null}
    export MODULAR_API_KEY="your_api_key"
    ```

    ```bash Windows theme={null}
    $env:MODULAR_API_KEY="your_api_key"
    ```
  </CodeGroup>

* The `openai` Python package. You can install it with this command:

  <CodeGroup>
    ```bash Python (pip) theme={null}
    pip install openai
    ```

    ```bash Python (uv) theme={null}
    uv add openai
    ```

    ```bash Python (pixi) theme={null}
    pixi add openai
    ```
  </CodeGroup>

## Generate a video

To generate a video, set `input` to a text description of the video and set
generation parameters in `provider_options.image`.

The following example generates a video from a text prompt and saves it as
`output-text-to-video.mp4`.

<CodeGroup>
  ```python Python theme={null}
  import base64
  import os
  from pathlib import Path

  from openai import OpenAI

  client = OpenAI(
      base_url="https://api.modular.com/v1",
      api_key=os.environ.get("MODULAR_API_KEY"),
  )

  response = client.responses.create(
      model="Wan-AI/Wan2.2-T2V-A14B-Diffusers",
      input=(
          "A campfire crackles in a forest clearing at night, with sparks "
          "spiraling into a star-filled sky."
      ),
      extra_body={
          "provider_options": {
              "image": {
                  "height": 512,
                  "width": 512,
                  "steps": 28,
              }
          }
      },
  )

  video_data = response.output[0].content[0].image_data
  Path("output-text-to-video.mp4").write_bytes(base64.b64decode(video_data))
  ```

  ```bash cURL theme={null}
  curl -X POST https://api.modular.com/v1/responses \
    -H "Authorization: Bearer $MODULAR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "Wan-AI/Wan2.2-T2V-A14B-Diffusers",
      "input": "A campfire crackles in a forest clearing at night, with sparks spiraling into a star-filled sky.",
      "provider_options": {
        "image": {
          "height": 512,
          "width": 512,
          "steps": 28
        }
      }
    }' \
    | jq -r '.output[0].content[0].image_data' \
    | base64 -d > output-text-to-video.mp4
  ```
</CodeGroup>

The generated video should look similar to the following:

<video src="https://mintcdn.com/modular/21dnEKVIFHhCZ_2p/inference/images/output-text-to-video.mp4?fit=max&auto=format&n=21dnEKVIFHhCZ_2p&q=85&s=c29428d5a856561521c291c53406b73a" width="512" controls muted loop data-path="inference/images/output-text-to-video.mp4" />

## Generate a video from an image

To generate a video from an image, set `input` to a user message containing:

* An `input_image` block with an image URL or base64-encoded data URI.
* An `input_text` block describing how to animate the image.

The following example reads a local image named `input.png`, encodes it as a
data URI, and saves the generated video as `output-image-to-video.mp4`.

<CodeGroup>
  ```python Python theme={null}
  import base64
  import os
  from pathlib import Path

  from openai import OpenAI

  client = OpenAI(
      base_url="https://api.modular.com/v1",
      api_key=os.environ.get("MODULAR_API_KEY"),
  )

  input_data = base64.b64encode(Path("input.png").read_bytes()).decode("utf-8")

  response = client.responses.create(
      model="Wan-AI/Wan2.2-I2V-A14B-Diffusers",
      input=[
          {
              "role": "user",
              "content": [
                  {
                      "type": "input_image",
                      "image_url": f"data:image/png;base64,{input_data}",
                  },
                  {
                      "type": "input_text",
                      "text": "Animate this scene with wind moving through the grass.",
                  },
              ],
          }
      ],
      extra_body={
          "provider_options": {
              "image": {
                  "height": 480,
                  "width": 480,
                  "steps": 28,
              }
          }
      },
  )

  video_data = response.output[0].content[0].image_data
  Path("output-image-to-video.mp4").write_bytes(base64.b64decode(video_data))
  ```

  ```bash cURL theme={null}
  IMAGE_DATA=$(base64 < input.png | tr -d '\n')

  curl -X POST https://api.modular.com/v1/responses \
    -H "Authorization: Bearer $MODULAR_API_KEY" \
    -H "Content-Type: application/json" \
    --data-binary @- <<EOF | jq -r '.output[0].content[0].image_data' | base64 -d > output-image-to-video.mp4
  {
    "model": "Wan-AI/Wan2.2-I2V-A14B-Diffusers",
    "input": [
      {
        "role": "user",
        "content": [
          {
            "type": "input_image",
            "image_url": "data:image/png;base64,$IMAGE_DATA"
          },
          {
            "type": "input_text",
            "text": "Animate this scene with wind moving through the grass."
          }
        ]
      }
    ],
    "provider_options": {
      "image": {
        "height": 480,
        "width": 480,
        "steps": 28
      }
    }
  }
  EOF
  ```
</CodeGroup>
