Integrate the video generation model

Updated at:

Call a Token Plan video generation model from an Agent tool, submit an asynchronous task, and download the video when generation completes.

Supported models

ModelInputDescription
happyhorse-1.1-t2vTextText-to-video
happyhorse-1.1-i2vFirst-frame image, textImage-to-video
happyhorse-1.1-r2vReference images, textReference-to-video
wan3.0-videoText, images, or reference materialsAll-in-one video generation

For more models, see models supported by Token Plan.

Prerequisites

  • A Token Plan subscription and an API key that starts with sk-sp-. For details, see Integration overview.
  • Python 3.9 or later.
  • Your AI coding tool supports Slash Commands. For configuration paths of other tools, see Other tools.

Interface

Submit a task: POST https://token-plan.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis

Query a task: GET https://token-plan.cn-beijing.maas.aliyuncs.com/api/v1/tasks/{task_id}

Use Authorization: Bearer $ANTHROPIC_AUTH_TOKEN in the request header. Add X-DashScope-Async: enable when submitting a task.

Step 1: Create a Slash Command

Configure the Token Plan API key as the environment variable $ANTHROPIC_AUTH_TOKEN.

Create .claude/commands/text-to-video.md in the project root and add the following content:

Call the Token Plan video generation API, submit a text-to-video task, wait for completion, and download the video.

Video description: $ARGUMENTS

## Steps

1. Extract the prompt (video description), model (default: happyhorse-1.1-t2v), resolution (default: 720P), ratio (default: 16:9), and duration (default: 5) from the description. If the description specifies a model, use that model.
2. Install dependencies: pip install requests
3. Use the Bash tool to run the following Python script. The script reads TOKEN_PLAN_API_KEY first, then ANTHROPIC_AUTH_TOKEN. Do not display the key or write it to files or logs.

```python
import json
import os
import sys
import time
from pathlib import Path

import requests

prompt = " ".join(sys.argv[1:])
key = os.environ.get("TOKEN_PLAN_API_KEY") or os.environ.get("ANTHROPIC_AUTH_TOKEN")
if not key:
    raise SystemExit("Token Plan API Key not found.")

base_url = "https://token-plan.cn-beijing.maas.aliyuncs.com"
headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}

response = requests.post(
    f"{base_url}/api/v1/services/aigc/video-generation/video-synthesis",
    headers={**headers, "X-DashScope-Async": "enable"},
    json={
        "model": "happyhorse-1.1-t2v",
        "input": {"prompt": prompt},
        "parameters": {"resolution": "720P", "ratio": "16:9", "duration": 5},
    },
    timeout=60,
)
if not response.ok:
    raise SystemExit(f"{response.status_code}: {response.text}")
task_id = response.json()["output"]["task_id"]
print(f"Task submitted: {task_id}")

while True:
    time.sleep(10)
    result = requests.get(f"{base_url}/api/v1/tasks/{task_id}", headers=headers, timeout=60)
    if not result.ok:
        raise SystemExit(f"{result.status_code}: {result.text}")
    data = result.json()
    status = data["output"]["task_status"]
    if status == "SUCCEEDED":
        video_url = data["output"]["video_url"]
        output = f"generated_{time.strftime('%Y%m%d_%H%M%S')}.mp4"
        video = requests.get(video_url, timeout=120)
        video.raise_for_status()
        Path(output).write_bytes(video.content)
        print(output)
        break
    if status in {"FAILED", "CANCELED", "UNKNOWN"}:
        raise SystemExit(json.dumps(data, ensure_ascii=False, indent=2))
    print(f"Task status: {status}")
```

4. Output the path of the downloaded video file.

Step 2: Generate a video

In Claude Code, enter /text-to-video a white cat sitting on a balcony in the sun. To adjust the duration, resolution, or aspect ratio, specify it in the command, for example /text-to-video generate a 10-second, 1080P, 9:16 city night scene.

Request parameters

ParameterRequiredDescription
modelYesVideo generation model ID.
input.promptYesVideo description.
parameters.resolutionNoResolution. Supports 480P, 720P, and 1080P. Default: 1080P.
parameters.ratioNoAspect ratio. Supports 16:9, 9:16, 1:1, and others. Default: 16:9.
parameters.durationNoDuration in seconds, from 3 to 15. Default: 5.

For more parameters, see HappyHorse text-to-video API.

Response

  • Submitting a task returns output.task_id and output.task_status.
  • Poll GET /api/v1/tasks/{task_id}. When the status is SUCCEEDED, the response contains output.video_url.
  • Task statuses include PENDING, RUNNING, SUCCEEDED, FAILED, CANCELED, and UNKNOWN.
  • Video URLs expire. Download the video promptly.

Limits

  • Video generation is asynchronous. Submit the task and poll for the result. A task_id is valid for 24 hours.
  • Supported input types, resolutions, durations, and aspect ratios differ by model. See each model's documentation.
  • Video generation consumes more than image generation and speech synthesis. Actual Credits consumption is shown in Usage in the console.

Other tools

For extension mechanisms and configuration paths of other tools, see Integration overview. Save the Slash Command above to the corresponding path.