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

# Text-to-Video API Reference

> Sora 2 text-to-video API reference and live playground — JSON request body, async three-step flow, flexible 4 / 8 / 12 second durations.

<Info>
  The interactive Playground on the right supports live debugging. Set your API Key in the **Authorization** field (format: `Bearer sk-xxx`), enter a prompt, choose model / size / seconds, and send.
</Info>

<Tip>
  **Scope**: This page covers "generate video from text only" — no `input_reference`, request body is `application/json`. To animate from a reference image (image-to-video), use the [Image-to-Video endpoint](/en/api-capabilities/sora-2/image-to-video) (same path + multipart upload).
</Tip>

<Warning>
  **⚠️ Three-step async flow — this page only covers step 1 (submission)**

  * **Step 1 (this page)**: `POST /v1/videos` → returns `video_id` + `status: "queued"`
  * **Step 2**: Poll `GET /v1/videos/{video_id}` until `status: "completed"`
  * **Step 3**: Download from `GET /v1/videos/{video_id}/content` (returns the MP4 file)

  The POST itself takes a few seconds and **does not block** until generation finishes. Full flow shown in the Python sample below.
</Warning>

## Code Samples

### Python (OpenAI SDK Drop-In)

```python theme={null}
from openai import OpenAI
import time

client = OpenAI(
    api_key="sk-your-api-key",
    base_url="https://api.apiyi.com/v1"
)

# Step 1: Submit the task
video = client.videos.create(
    model="sora-2",
    prompt="A golden retriever running on the beach at sunset, cinematic, golden hour, slow motion",
    seconds="8",
    size="1280x720"
)
print(f"Video ID: {video.id}, status: {video.status}")

# Step 2: Poll status
while True:
    video = client.videos.retrieve(video.id)
    print(f"Status: {video.status}, progress: {getattr(video, 'progress', 0)}%")
    if video.status == "completed":
        break
    if video.status == "failed":
        raise RuntimeError(f"Generation failed: {video}")
    time.sleep(15)

# Step 3: Download the video
content = client.videos.download_content(video.id)
content.write_to_file("output.mp4")
print("Saved: output.mp4")
```

### Python (Raw requests)

```python theme={null}
import requests
import time

API_KEY = "sk-your-api-key"
BASE_URL = "https://api.apiyi.com/v1"
HEADERS = {"Authorization": f"Bearer {API_KEY}"}

# Step 1: Submit (JSON body)
resp = requests.post(
    f"{BASE_URL}/videos",
    headers={**HEADERS, "Content-Type": "application/json"},
    json={
        "model": "sora-2",
        "prompt": "A serene Japanese garden with cherry blossoms, koi pond, traditional bridge, golden hour, ultra detailed",
        "seconds": "8",
        "size": "1280x720"
    },
    timeout=30  # The POST is just enqueueing; 30 seconds is enough
).json()
video_id = resp["id"]
print(f"Video ID: {video_id}, status: {resp['status']}")

# Step 2: Poll (max wait 15 minutes)
deadline = time.time() + 900
while time.time() < deadline:
    status_resp = requests.get(f"{BASE_URL}/videos/{video_id}", headers=HEADERS).json()
    print(f"Status: {status_resp['status']}, progress: {status_resp.get('progress', 0)}%")
    if status_resp["status"] == "completed":
        break
    if status_resp["status"] == "failed":
        raise RuntimeError(f"Generation failed: {status_resp}")
    time.sleep(15)

# Step 3: Download
with requests.get(f"{BASE_URL}/videos/{video_id}/content", headers=HEADERS, stream=True) as r:
    r.raise_for_status()
    with open("output.mp4", "wb") as f:
        for chunk in r.iter_content(chunk_size=8192):
            f.write(chunk)
print("Saved: output.mp4")
```

### cURL

```bash theme={null}
{/* Step 1: Submit the task */}
curl -X POST "https://api.apiyi.com/v1/videos" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "sora-2",
    "prompt": "A futuristic cityscape at night with neon lights and flying vehicles, cyberpunk style, cinematic",
    "seconds": "8",
    "size": "1280x720"
  }'

{/* Step 2: Poll status (replace video_id) */}
curl -X GET "https://api.apiyi.com/v1/videos/video_abc123" \
  -H "Authorization: Bearer sk-your-api-key"

{/* Step 3: Download the video file */}
curl -X GET "https://api.apiyi.com/v1/videos/video_abc123/content" \
  -H "Authorization: Bearer sk-your-api-key" \
  -o output.mp4
```

### Node.js (fetch)

```javascript theme={null}
import fs from 'node:fs';

const API_KEY = 'sk-your-api-key';
const BASE_URL = 'https://api.apiyi.com/v1';

// Step 1: Submit
const submitResp = await fetch(`${BASE_URL}/videos`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${API_KEY}`
    },
    body: JSON.stringify({
        model: 'sora-2',
        prompt: 'Aerial drone shot over snowy mountain range at sunrise, cinematic, ultra wide',
        seconds: '8',
        size: '1280x720'
    })
});
const { id: videoId } = await submitResp.json();
console.log(`Video ID: ${videoId}`);

// Step 2: Poll
let status = 'queued';
while (status !== 'completed' && status !== 'failed') {
    await new Promise(r => setTimeout(r, 15000));
    const statusResp = await fetch(`${BASE_URL}/videos/${videoId}`, {
        headers: { 'Authorization': `Bearer ${API_KEY}` }
    });
    const data = await statusResp.json();
    status = data.status;
    console.log(`Status: ${status}, progress: ${data.progress ?? 0}%`);
}

if (status === 'failed') throw new Error('Generation failed');

// Step 3: Download
const contentResp = await fetch(`${BASE_URL}/videos/${videoId}/content`, {
    headers: { 'Authorization': `Bearer ${API_KEY}` }
});
const buffer = Buffer.from(await contentResp.arrayBuffer());
fs.writeFileSync('output.mp4', buffer);
console.log('Saved: output.mp4');
```

### Browser JavaScript

```javascript theme={null}
{/* Demo only; route through your backend in production to avoid leaking the API key. Browsers also aren't ideal for downloading large video files. */}
const submitResp = await fetch('https://api.apiyi.com/v1/videos', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer sk-your-api-key'
    },
    body: JSON.stringify({
        model: 'sora-2',
        prompt: 'Watercolor northern lights over snowy mountains, gentle motion',
        seconds: '4',
        size: '720x1280'
    })
});
const { id } = await submitResp.json();
console.log('Video ID:', id);

{/* After polling completes, hand the video URL to a backend proxy for download and serve it back to the client. */}
```

## Parameters Quick Reference

| Parameter | Type   | Required | Default    | Description                                                                                                                        |
| --------- | ------ | -------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `model`   | string | Yes      | —          | `sora-2` (720p only) or `sora-2-pro` (720p / 1024p / 1080p tiers)                                                                  |
| `prompt`  | string | Yes      | —          | Video description; describe scene, camera motion, style, lighting in detail                                                        |
| `seconds` | string | No       | `"4"`      | Duration as **string enum**: `"4"` / `"8"` / `"12"` (**not a number**)                                                             |
| `size`    | string | No       | `720x1280` | Output resolution; must match the model's supported tiers (see [Tech Specs](/en/api-capabilities/sora-2/overview#technical-specs)) |

<Tip>
  Detailed parameter constraints, allowed values, and examples are visible in the right-hand Playground — all enum fields offer dropdowns. **For image-to-video parameters (`input_reference` upload), see the [Image-to-Video endpoint](/en/api-capabilities/sora-2/image-to-video)**.
</Tip>

## Response Format

### Step 1 — Immediate Submit Response

```json theme={null}
{
  "id": "video_abc123def456",
  "object": "video",
  "model": "sora-2",
  "status": "queued",
  "progress": 0,
  "created_at": 1712697600,
  "size": "1280x720",
  "seconds": "8",
  "quality": "standard"
}
```

### Step 2 — Polling While Running

```json theme={null}
{
  "id": "video_abc123def456",
  "object": "video",
  "model": "sora-2",
  "status": "in_progress",
  "progress": 45,
  "created_at": 1712697600,
  "size": "1280x720",
  "seconds": "8"
}
```

### Step 2 — Polling After Completion

```json theme={null}
{
  "id": "video_abc123def456",
  "object": "video",
  "model": "sora-2",
  "status": "completed",
  "progress": 100,
  "created_at": 1712697600,
  "completed_at": 1712697900,
  "size": "1280x720",
  "seconds": "8"
}
```

<Warning>
  **⚠️ Response field gotchas**

  * **No direct `video_url` field** — the video file must be downloaded from `GET /v1/videos/{id}/content` (returns a `video/mp4` binary stream). **Don't expect a CDN URL in the JSON response.**
  * `progress` is not strictly linear — it can jump (e.g. 0 → 45 → 80 → 100)
  * On `status: "failed"`, an `error` field is not always present — most failures are content-policy or capacity issues, **just retry or revise the prompt**
  * Video content is retained on OpenAI for **1 day only** — `/content` returns 404 after expiration
</Warning>

<Info>
  This endpoint is the async-task entry point. **Billing settles by `seconds` rate when the task completes** (see [pricing table](/en/api-capabilities/sora-2/overview#pricing)). The POST submission, status polling, and content download themselves are **not billed**, and failed tasks are **not billed**.
</Info>


## OpenAPI

````yaml api-reference/sora-2-text-to-video-openapi-en.yaml POST /v1/videos
openapi: 3.1.0
info:
  title: Sora 2 Text-to-Video API
  description: >
    OpenAI Sora 2 / Sora 2 Pro video generation — text-to-video endpoint (no
    `input_reference`, JSON request body).


    - Two active models: `sora-2` (720p, \$0.10/sec) and `sora-2-pro` (720p /
    1024p / 1080p tiers)

    - Duration: `"4"` / `"8"` / `"12"` seconds (**string enum**)

    - Outputs video with synchronized audio (ambient sound, dialogue, score)

    - **Async task endpoint**: this call only submits the task; combine with
    `GET /v1/videos/{id}` polling and `GET /v1/videos/{id}/content` download

    - Videos are retained on OpenAI servers for 1 day only — download
    immediately after completion

    - For image-conditioned generation, see the [Image-to-Video
    endpoint](/en/api-capabilities/sora-2/image-to-video) (same path + multipart
    `input_reference`)


    **Authentication**: Add `Authorization: Bearer YOUR_API_KEY` to the request
    header


    **API Key configuration**: In your APIYI console, set the group to **Sora2官转
    (Sora2 Official)** and billing mode to **usage-based**, otherwise you'll get
    403


    **Get API Key**: Visit the [APIYI Console](https://api.apiyi.com/token) to
    create a token
  version: 1.0.0
servers:
  - url: https://api.apiyi.com
    description: Primary endpoint
  - url: https://vip.apiyi.com
    description: Backup endpoint
security:
  - bearerAuth: []
paths:
  /v1/videos:
    post:
      tags:
        - Video Generation
      summary: 'Text-to-video: submit a video generation task from text'
      description: >
        Submits a Sora 2 video generation task (async). Returns `video_id` and
        `status: "queued"`.


        - Required: `model`, `prompt`

        - Optional: `seconds` (default `"4"`), `size` (default `"720x1280"`)

        - **Response does not include the video file** — poll `GET
        /v1/videos/{video_id}` until `status: "completed"`, then call `GET
        /v1/videos/{video_id}/content` to download the MP4

        - Typical generation time: sora-2 4 sec ≈ 3 minutes; sora-2-pro 12 sec
        at 1080p ≈ 8–10 minutes

        - For image-conditioned generation, use the [Image-to-Video
        endpoint](/en/api-capabilities/sora-2/image-to-video) instead
      operationId: generateSora2TextToVideoEn
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Sora2TextToVideoRequest'
            example:
              model: sora-2
              prompt: >-
                A golden retriever running on the beach at sunset, cinematic,
                golden hour, slow motion
              seconds: '8'
              size: 1280x720
      responses:
        '200':
          description: Task submitted, returns video_id with queued status
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Sora2VideoTask'
        '400':
          description: >-
            Invalid parameters (seconds not in 4/8/12, size not supported by the
            model, prompt missing, etc.)
        '401':
          description: Unauthorized — invalid API Key
        '403':
          description: Content policy / not on usage-based billing / group is not Sora2官转
        '429':
          description: Rate limit exceeded or insufficient balance
        '500':
          description: Upstream OpenAI gateway error — retry 1–2 times
      security:
        - bearerAuth: []
components:
  schemas:
    Sora2TextToVideoRequest:
      type: object
      required:
        - model
        - prompt
      properties:
        model:
          type: string
          description: >-
            Model ID. `sora-2` supports 720p only; `sora-2-pro` supports 720p /
            1024p / 1080p tiers
          enum:
            - sora-2
            - sora-2-pro
          default: sora-2
        prompt:
          type: string
          description: >-
            Video generation prompt; describe scene, camera motion, style,
            lighting, and character actions in detail
          example: >-
            A serene Japanese garden with cherry blossoms, koi pond, traditional
            bridge, golden hour, ultra detailed
        seconds:
          type: string
          description: >
            Video duration as a **string enum** (not a number):

            - `"4"` — 4 seconds (default), ideal for short demos, single shots,
            fast prompt iteration

            - `"8"` — 8 seconds, standard short-form video, most common

            - `"12"` — 12 seconds, long shots and continuous action


            Passing `"10"` / `"15"` or the integer `4` returns 400
          enum:
            - '4'
            - '8'
            - '12'
          default: '4'
        size:
          type: string
          description: >
            Output resolution. **`sora-2` and `sora-2-pro` support different
            tiers**:


            - `sora-2` (720p only): `720x1280` (portrait, default) / `1280x720`
            (landscape)

            - `sora-2-pro` additionally supports:
              - `1024x1792` / `1792x1024` (1024p, \$0.50/sec)
              - `1080x1920` / `1920x1080` (1080p, \$0.70/sec)

            Passing 1024p / 1080p sizes to `sora-2` returns 400
          enum:
            - 720x1280
            - 1280x720
            - 1024x1792
            - 1792x1024
            - 1080x1920
            - 1920x1080
          default: 720x1280
    Sora2VideoTask:
      type: object
      properties:
        id:
          type: string
          description: Task ID for subsequent polling and download
          example: video_abc123def456
        object:
          type: string
          description: Object type, fixed `video`
          example: video
        model:
          type: string
          description: Model ID used for this task
          example: sora-2
        status:
          type: string
          description: |
            Task status:
            - `queued` — submitted, waiting in queue
            - `in_progress` — generating
            - `completed` — done, ready to download (`/v1/videos/{id}/content`)
            - `failed` — failed (not billed), safe to retry
          enum:
            - queued
            - in_progress
            - completed
            - failed
          example: queued
        progress:
          type: integer
          description: Generation progress percentage (0–100), not strictly linear
          example: 0
        created_at:
          type: integer
          description: Task creation Unix timestamp (seconds)
          example: 1712697600
        completed_at:
          type: integer
          description: >-
            Task completion Unix timestamp (seconds), present only on completed
            status
          example: 1712697900
        size:
          type: string
          description: Actual output resolution (matches the requested `size`)
          example: 1280x720
        seconds:
          type: string
          description: Actual duration generated (matches the requested `seconds`)
          example: '8'
        quality:
          type: string
          description: Quality tier (`standard` for sora-2, `high` for sora-2-pro)
          example: standard
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        API Key from the APIYI console (must use Sora2官转 group + usage-based
        billing)

````