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

# Video generation

> Generate a video with the DGrid AI Gateway, track the task, and optionally provide reference images or videos.

This guide uses ByteDance Seedance 2.5 to create a video from a text prompt. You can also provide reference images or videos when using the BytePlus Seedance channel.

## 1. Set up your Model API Key

Use a production DGrid **Model API Key** with access to the model and enough available balance to cover the estimated cost. Store the key on your server. The examples use Bash, cURL 7.76 or later, and `uuidgen`. Bash hides the key as you type it. The Model API authenticates with a Model API Key. Use a Management API Key for management operations.

```bash theme={null}
read -r -s -p 'DGrid API Key: ' DGRID_API_KEY
printf '\n'
export DGRID_API_KEY
export DGRID_BASE_URL='https://api.dgrid.ai/v1'
```

The base URL includes `/v1`. Append paths such as `/videos` directly to it.

## 2. Prepare the request

Save the following JSON as `request.json`. This example requests a 5-second video at 720p with a 16:9 aspect ratio. Seedance 2.5 supports up to 30 seconds per generation, subject to supported input combinations. For other models, use the request format and parameters for that model.

```json theme={null}
{
  "model": "bytedance/seedance-2.5",
  "content": [
    {
      "type": "text",
      "text": "The camera slowly moves forward along the coast at dawn as waves gently lap against the rocks. Keep the motion smooth and natural."
    }
  ],
  "duration": 5,
  "resolution": "720p",
  "ratio": "16:9",
  "generate_audio": false,
  "watermark": false
}
```

## 3. Create the video task

Generate an idempotency key once for this task. Save it with the original request body.

```bash theme={null}
export DGRID_IDEMPOTENCY_KEY="$(uuidgen)"
```

The following request submits a generation task and may incur charges.

```bash theme={null}
curl -sS --fail-with-body --max-time 30 \
  "$DGRID_BASE_URL/videos" \
  --header "Authorization: Bearer $DGRID_API_KEY" \
  --header "Idempotency-Key: $DGRID_IDEMPOTENCY_KEY" \
  --header 'Content-Type: application/json' \
  --data-binary @request.json
```

HTTP `202` means DGrid accepted the task. Save the response `id` for polling. The `estimated_amount` field shows the estimated cost in USD.

If creation times out or returns a temporary `503` before you receive an ID, retry with the same API key, idempotency key, and request body. Once you have the ID, query the existing task. See [Create video](https://docs.dgrid.ai/api-reference/videos/create-video.md) for the full request and retry rules.

## 4. Retrieve the video

Replace the placeholder with the returned task ID. Use the API key that created the task.

```bash theme={null}
TASK_ID='replace-with-returned-video-task-id'
curl -sS --fail-with-body --max-time 30 \
  "$DGRID_BASE_URL/videos/$TASK_ID" \
  --header "Authorization: Bearer $DGRID_API_KEY"
```

Start by polling every 5 seconds and set a local timeout.

* While the task is processing or settlement is pending, keep querying the same task.
* When `status` is `succeeded` and `settlement_status` is `settled`, use `video_url` to play or download the video and `billing.total` to read the final USD charge.
* If `settlement_status` is `awaiting_balance`, top up the balance and query the same task again.
* For `failed`, `cancelled`, or `expired`, stop polling and show the error.
* For `submission_unknown`, `configuration_blocked`, or an unrecognized status, save the task ID and contact DGrid support before submitting another task.

Honor `Retry-After` when the API rate-limits a request. A local timeout ends polling. You can resume later with the same ID. See [Retrieve video](https://docs.dgrid.ai/api-reference/videos/retrieve-video.md) for all task states and response fields.

## 5. Add reference assets (optional)

You can skip this step for text-to-video. The following workflows apply to the BytePlus Seedance channel.

### Use an HTTPS URL

Upload a local image with the following command. To upload a video, change the file path to `./reference.mp4`.

```bash theme={null}
curl -sS --fail-with-body --max-time 120 \
  "$DGRID_BASE_URL/video-assets" \
  --header "Authorization: Bearer $DGRID_API_KEY" \
  --form 'file=@./reference.jpg'
```

HTTP `201` returns a signed `url` valid for 24 hours. Keep its full query string. You can also use a server-accessible HTTPS URL. An external video must be a direct MP4 link that supports HTTP Range requests and allows the server to read the duration.

Keep the text prompt in `content` and add the applicable media items. Replace each URL placeholder and remove any unused item.

```json theme={null}
{
  "model": "bytedance/seedance-2.5",
  "content": [
    {
      "type": "text",
      "text": "Use the scene in the reference image and the camera movement in the reference video to create a city street after the rain."
    },
    {
      "type": "image_url",
      "role": "reference_image",
      "image_url": {
        "url": "<IMAGE_UPLOAD_URL>"
      }
    },
    {
      "type": "video_url",
      "role": "reference_video",
      "video_url": {
        "url": "<VIDEO_UPLOAD_URL>"
      }
    }
  ],
  "duration": 5,
  "resolution": "720p",
  "ratio": "16:9"
}
```

### Use a reviewed asset ID

Upload the file with `submit_for_review=true`. Save the returned `asset_id`.

```bash theme={null}
curl -sS --fail-with-body --max-time 120 \
  "$DGRID_BASE_URL/video-assets?submit_for_review=true" \
  --header "Authorization: Bearer $DGRID_API_KEY" \
  --form 'file=@./reference.mp4'
```

Replace the placeholder with that asset ID and query the review status using credentials for the same DGrid user.

```bash theme={null}
ASSET_ID='replace-with-returned-asset-id'
curl -sS --fail-with-body --max-time 30 \
  "$DGRID_BASE_URL/video-assets/$ASSET_ID" \
  --header "Authorization: Bearer $DGRID_API_KEY"
```

Poll while the status is `Processing`. If it becomes `Failed`, stop polling and check the response for `error_code`. Once it becomes `Active`, put `asset://` followed by the asset ID in the corresponding media URL.

```json theme={null}
{
  "type": "image_url",
  "role": "reference_image",
  "image_url": {
    "url": "asset://<IMAGE_ASSET_ID>"
  }
}
```

```json theme={null}
{
  "type": "video_url",
  "role": "reference_video",
  "video_url": {
    "url": "asset://<VIDEO_ASSET_ID>"
  }
}
```

The asset must have been submitted for review through DGrid by the same user. You can reference an `Active` asset on the BytePlus Seedance channel after its signed upload URL expires. The asset remains subject to review status checks.

Save the updated request as `request.json`, generate a new idempotency key for the new task, and repeat steps 3 and 4. If you are retrying a previous task whose acceptance is uncertain, preserve its original request body, including the asset URLs. See [Upload reference asset](https://docs.dgrid.ai/api-reference/videos/upload-reference-asset.md) for file limits and review errors.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.