Skip to main content
Looking for a more stable solution?This page covers the Sync API (suitable for quick testing). For a more stable production environment solution, we recommend using the Async API.

Before You Begin

1

Get API Key

Log in to LaoZhang API Console to create an API Key
Important: Veo-3.1 models require pay-per-use tokens, not pay-as-you-go tokens. Please select “pay-per-use” type when creating tokens.
2

Ensure Account Balance

Make sure your account has sufficient balance. Veo-3.1 models charge per request (0.150.15-0.25/request)

Your First Request

Text-to-Video Example

Use cURL to quickly test text-to-video functionality:
curl --location --request POST 'https://api.laozhang.ai/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-YOUR_API_KEY' \
--data-raw '{
    "messages": [{
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Generate a video of two cats and a dog fighting"
            }
        ]
    }],
    "model": "veo-3.1",
    "stream": true,
    "n": 2
}'
Parameter explanation:
  • model: Select veo-3.1 series model
  • stream: Set to true to enable streaming response
  • n: Number of results to generate, setting to 2 will generate 2 different videos

Image-to-Video Example

Use images as reference to generate videos:
# Note: Sync API requires Base64 encoded images
# This is an example format, replace BASE64_STRING with actual Base64 string
curl --location --request POST 'https://api.laozhang.ai/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-YOUR_API_KEY' \
--data-raw '{
    "messages": [{
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Generate a smooth transition video based on two images"
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": "data:image/jpeg;base64,BASE64_STRING_1"
                }
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": "data:image/jpeg;base64,BASE64_STRING_2"
                }
            }
        ]
    }],
    "model": "veo-3.1-fl",
    "stream": true,
    "n": 2
}'

Python Quick Example

Install OpenAI SDK

pip install openai

Text-to-Video Code

from openai import OpenAI

client = OpenAI(
    api_key="sk-YOUR_API_KEY",
    base_url="https://api.laozhang.ai/v1"
)

response = client.chat.completions.create(
    model="veo-3.1",
    messages=[{
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Generate a video of a cute kitten playing on the grass"
            }
        ]
    }],
    stream=True,
    n=1
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end='')

Image-to-Video Code

import base64
from openai import OpenAI

# Helper function: Encode image to Base64
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

client = OpenAI(
    api_key="sk-YOUR_API_KEY",
    base_url="https://api.laozhang.ai/v1"
)

# Read local images
base64_image1 = encode_image("image1.jpg")
base64_image2 = encode_image("image2.jpg")

response = client.chat.completions.create(
    model="veo-3.1-fl",
    messages=[{
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Generate smooth transition animation based on images"
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image1}"
                }
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image2}"
                }
            }
        ]
    }],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end='')

Node.js Quick Example

Install OpenAI SDK

npm install openai

Basic Usage

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'sk-YOUR_API_KEY',
  baseURL: 'https://api.laozhang.ai/v1'
});

async function generateVideo() {
  const stream = await client.chat.completions.create({
    model: 'veo-3.1',
    messages: [{
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'Generate a video of sunset by the sea'
        }
      ]
    }],
    stream: true,
    n: 1
  });

  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content;
    if (content) {
      process.stdout.write(content);
    }
  }
}

generateVideo().catch(console.error);

Streaming Response Handling

Veo-3.1 supports streaming responses for real-time generation progress and results:
response = client.chat.completions.create(
    model="veo-3.1",
    messages=[...],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        content = chunk.choices[0].delta.content
        print(f"Received data: {content}")

Image Format Support

Veo-3.1 supports multiple image input formats:
Note: Sync API must use Base64 encoded images, http/https URLs are not supported. If you need to use URL links, please use the Async API.
Image Requirements:
  • Supported formats: JPEG, PNG, WebP
  • Maximum size: 10MB
  • Recommended resolution: 1024x1024 or higher
  • Maximum images: 2 (start frame + end frame)

Common Model Selection

Choose the appropriate model based on your needs:

Quick Testing

Recommended: veo-3.1-fastSuitable for quickly validating ideas, cheap price ($0.15/request)

Standard Quality

Recommended: veo-3.1Balances quality and cost, suitable for most scenarios ($0.25/request)

Image-to-Video

Recommended: veo-3.1-flGenerate videos or transition animations based on images ($0.25/request)

Landscape Video

Recommended: veo-3.1-landscapeProfessional landscape format, suitable for film production ($0.25/request)

Error Handling

from openai import OpenAI, OpenAIError

client = OpenAI(
    api_key="sk-YOUR_API_KEY",
    base_url="https://api.laozhang.ai/v1"
)

try:
    response = client.chat.completions.create(
        model="veo-3.1",
        messages=[{
            "role": "user",
            "content": [{"type": "text", "text": "Generate video"}]
        }],
        stream=True
    )

    for chunk in response:
        if chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content)

except OpenAIError as e:
    print(f"API error: {e}")
except Exception as e:
    print(f"Other error: {e}")

Complete Examples

View Complete Code Examples

Includes complete example code in Python, Node.js, Go, Java and more

Next Steps

Async API (Recommended)

More stable task queue approach, no charge on failure

Code Examples

View examples in more programming languages

Best Practices

Learn how to write better prompts

Troubleshooting

Having issues? Check solutions

Get Help

Technical Support

Having issues? Contact technical support

Telegram Community

Join the community for discussions