Skip to main content

Integrate a video generation model

Call Token Plan video generation models in Agent tools, submit asynchronous tasks, and download videos after 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 installed locally.
  • Your AI coding tool supports Slash Commands. For configuration paths of other tools, see Other tools.

Endpoint

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 to submit the task, poll its status, and download the video. 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 downloaded video file path.

Step 2: Generate a video

In Claude Code, enter /text-to-video A white cat basking in the sun on a balcony. To adjust the duration, resolution, or aspect ratio, specify them in the instruction, for example /text-to-video 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. The API default is 1080P. The example script explicitly passes 720P.
parameters.ratioNoAspect ratio. Supports 16:9, 9:16, 1:1, and more. Default: 16:9.
parameters.durationNoDuration. Range: 3 to 15 seconds. Default: 5 seconds.
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 them promptly.

Limits

  • Video generation is asynchronous. Poll for the result after submission. task_id is valid for 24 hours.
  • Supported input types, resolutions, durations, and aspect ratios vary by model. See each model's documentation.
  • Video generation consumes more than image generation and speech synthesis. Actual consumption is shown in the console usage statistics.

Other tools

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