Tất cả bài viết
Các hướng dẫn10 phút đọc

Làm thế nào để sử dụng Kling 2.6 API trên PoYo: Hướng dẫn phát triển đầy đủ

Tìm hiểu cách tích hợp Kling 2.6 API cho việc tạo video AI với âm thanh bản địa. Hướng dẫn từng bước với các ví dụ mã cho việc tạo văn bản sang video và hình ảnh sang video.

Làm thế nào để sử dụng Kling 2.6 API trên PoYo: Hướng dẫn phát triển đầy đủ
Các hướng dẫn

Làm thế nào để sử dụng Kling 2.6 API trên PoYo

2026 cập nhật: Kling 2.6 API có sẵn trên PoYo với giá hiện tại từ tín dụng 65 cho video 5 giây không có âm thanh và 120 tín dụng cho video 5 giây với âm thanh. Hướng dẫn này hướng dẫn bạn thông qua việc tích hợp khả năng âm thanh bản địa đồng bộ của Kling 2.6 vào các ứng dụng của bạn.

Bắt đầu với Kling 2.6 API

Bước 1: Thiết lập tài khoản PoYo của bạn

Đầu tiên, tạo tài khoản PoYo của bạn và lấy giấy chứng nhận API của bạn:

  1. Đăng ký tại PoYo.ai
  2. Tạo khóa API của bạn trong bảng điều khiển
  3. Thêm tín dụng đến tài khoản của bạn (Kling 2.6 giá thay đổi theo thời gian và âm thanh)

Bước 2: Lắp đặt các phụ thuộc cần thiết

# For Node.js/JavaScript
npm install axios

# For Python
pip install requests

# For cURL (no installation needed)

Tạo ra văn bản-video cơ bản

JavaScript/Node.js Thực hiện

const axios = require('axios');

const generateVideo = async (prompt, options = {}) => {
  const {
    duration = 5,       // 5 or 10 seconds
    aspectRatio = '16:9', // '1:1', '16:9', or '9:16'
    sound = true        // Enable native audio
  } = options;

  try {
    const response = await axios.post(
      'https://api.poyo.ai/api/generate/submit',
      {
        model: 'kling-2.6',
        callback_url: 'https://your-domain.com/callback', // Optional webhook
        input: {
          prompt: prompt,
          sound: sound,
          aspect_ratio: aspectRatio,
          duration: duration
        }
      },
      {
        headers: {
          'Authorization': `Bearer YOUR_API_KEY`,
          'Content-Type': 'application/json'
        }
      }
    );

    console.log('Task ID:', response.data.data.task_id);
    console.log('Status:', response.data.data.status);
    console.log('Created:', response.data.data.created_time);
    return response.data.data;
  } catch (error) {
    console.error('Error generating video:', error.response?.data || error.message);
  }
};

// Example: Generate a video with speech
generateVideo(
  'A man in a coffee shop looking at his laptop, smiling. He says "This AI is incredible!" in an excited voice.',
  { duration: 5, aspectRatio: '16:9', sound: true }
);

Thực hiện Python

import requests
import json

def generate_video(prompt, api_key, duration=5, aspect_ratio='16:9', sound=True):
    url = "https://api.poyo.ai/api/generate/submit"

    payload = {
        "model": "kling-2.6",
        "callback_url": "https://your-domain.com/callback",  # Optional
        "input": {
            "prompt": prompt,
            "sound": sound,
            "aspect_ratio": aspect_ratio,
            "duration": duration
        }
    }

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()

        result = response.json()
        print(f"Task ID: {result['data']['task_id']}")
        print(f"Status: {result['data']['status']}")
        print(f"Created: {result['data']['created_time']}")
        return result['data']

    except requests.exceptions.RequestException as e:
        print(f"Error generating video: {e}")
        return None

# Example usage
api_key = "YOUR_API_KEY"
result = generate_video(
    "A chef in a kitchen demonstrating how to chop vegetables. He narrates: 'First, we cut the onion in half.'",
    api_key,
    duration=10,
    aspect_ratio='16:9',
    sound=True
)

Thực hiện cURL

curl -X POST "https://api.poyo.ai/api/generate/submit" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "kling-2.6",
    "callback_url": "https://your-domain.com/callback",
    "input": {
      "prompt": "A woman sitting at a piano, playing a gentle melody. She sings softly: Welcome to my world of music.",
      "sound": true,
      "aspect_ratio": "16:9",
      "duration": 10
    }
  }'

Các tham số API

Cơ quan yêu cầu

Parameter Loại Cần Mô tả
model chuỗi Vâng. Chắc là vậy. "kling-2.6"
callback_url chuỗi Không. Webhook URL cho các thông báo bất đồng bộ
input.prompt chuỗi Vâng. Mô tả văn bản (tối đa là 1000 ký tự)
input.sound boolean Vâng. Khả năng phát âm bản địa
input.aspect_ratio chuỗi Vâng. "1:1", "16:9", hoặc "9:16"
input.duration số nguyên Vâng. 5 hoặc 10 giây
input.image_urls mảng Không. Hình ảnh tham chiếu cho hình ảnh-video

Phương thức phản ứng

{
  "code": 200,
  "data": {
    "task_id": "task-unified-1757165031-uyujaw3d",
    "status": "not_started",
    "created_time": "2026-06-12T10:30:00"
  }
}

Tạo hình ảnh sang video

Chuyển đổi hình ảnh tĩnh thành video động với Kling 2.6:

const generateVideoFromImage = async (imageUrl, prompt, options = {}) => {
  const {
    duration = 5,
    aspectRatio = '16:9',
    sound = true
  } = options;

  try {
    const response = await axios.post(
      'https://api.poyo.ai/api/generate/submit',
      {
        model: 'kling-2.6',
        callback_url: 'https://your-domain.com/callback',
        input: {
          prompt: prompt,
          sound: sound,
          aspect_ratio: aspectRatio,
          duration: duration,
          image_urls: [imageUrl] // Reference image
        }
      },
      {
        headers: {
          'Authorization': `Bearer YOUR_API_KEY`,
          'Content-Type': 'application/json'
        }
      }
    );

    return response.data.data;
  } catch (error) {
    console.error('Error generating video:', error.response?.data || error.message);
  }
};

// Example: Animate a portrait photo
const result = await generateVideoFromImage(
  'https://example.com/portrait.jpg',
  'The person in the photo turns to smile at the camera and waves hello.',
  { duration: 5, sound: true }
);

Python Hình ảnh sang video

def generate_video_from_image(image_url, prompt, api_key, duration=5, aspect_ratio='16:9', sound=True):
    url = "https://api.poyo.ai/api/generate/submit"

    payload = {
        "model": "kling-2.6",
        "callback_url": "https://your-domain.com/callback",
        "input": {
            "prompt": prompt,
            "sound": sound,
            "aspect_ratio": aspect_ratio,
            "duration": duration,
            "image_urls": [image_url]
        }
    }

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()
        return response.json()['data']
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        return None

# Example: Animate a product image
result = generate_video_from_image(
    "https://example.com/product.jpg",
    "The product rotates slowly, showcasing all angles. A voice says: Introducing our latest innovation.",
    api_key,
    duration=10,
    sound=True
)

Tính năng âm thanh bản địa

Các sound tham số cho phép tạo âm thanh bản địa của Kling 2.6:

Viết prompt bằng âm thanh

Khi nào sound: true, bao gồm các tín hiệu âm thanh trong yêu cầu của bạn:

// Prompt with speech
const speechPrompt = `
  A news anchor at a desk looks into the camera.
  He says: "Breaking news tonight, scientists have made an incredible discovery."
  The anchor gestures with his hands while speaking.
`;

// Prompt with sound effects
const sfxPrompt = `
  A cat walks across a wooden floor, meowing softly.
  Birds chirp outside the window.
  The cat jumps onto a couch with a soft thud.
`;

// Prompt with singing
const musicPrompt = `
  A woman with a guitar sits on a beach at sunset.
  She strums gently and sings a soft melody about the ocean.
  Waves crash softly in the background.
`;

// Prompt with multilingual speech
const multilingualPrompt = `
  Scene: Open office with printer sounds in background.
  [Male worker] asks: "报告快好了吗?Manager needs it this afternoon."
  [Female worker] replies: "Almost done. I'll send it in ten minutes."
`;

Audio vs Video câm

// Generate with audio (120 credits for 5s)
const withAudio = await generateVideo(
  'A barista making coffee, explaining each step of the process.',
  { duration: 5, sound: true }
);

// Generate without audio (65 credits for 5s)
const silent = await generateVideo(
  'A timelapse of clouds moving across a mountain landscape.',
  { duration: 5, sound: false }
);

Kết quả xử lý

Máy xử lý Webhook

const express = require('express');
const app = express();

app.use(express.json());

app.post('/webhook/callback', async (req, res) => {
  const { task_id, status, files, error_message } = req.body;

  console.log(`Task ${task_id}: ${status}`);

  if (status === 'finished' && files && files.length > 0) {
    const videoFile = files.find(f => f.file_type === 'video');
    if (videoFile) {
      console.log('Video URL:', videoFile.file_url);
      await processCompletedVideo(task_id, videoFile.file_url);
    }
  } else if (status === 'failed') {
    console.error('Generation failed:', error_message);
    await handleVideoError(task_id, error_message);
  }

  res.status(200).send('OK');
});

app.listen(3000);

Đánh giá tình trạng thủ công

const checkTaskStatus = async (taskId) => {
  try {
    const response = await axios.get(
      `https://api.poyo.ai/api/generate/status/${taskId}`,
      {
        headers: {
          'Authorization': `Bearer YOUR_API_KEY`
        }
      }
    );
    return response.data.data;
  } catch (error) {
    console.error('Error checking status:', error.message);
    return null;
  }
};

const waitForVideo = async (taskId, maxAttempts = 120) => {
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
    const taskData = await checkTaskStatus(taskId);

    if (!taskData) break;

    console.log(`Status: ${taskData.status}, Progress: ${taskData.progress || 0}%`);

    if (taskData.status === 'finished') {
      console.log('Video generated successfully!');
      const videoFile = taskData.files?.find(f => f.file_type === 'video');
      if (videoFile) {
        console.log('Video URL:', videoFile.file_url);
      }
      return taskData;
    } else if (taskData.status === 'failed') {
      console.error('Generation failed:', taskData.error_message);
      return null;
    }

    // Wait 5 seconds between checks (video generation takes time)
    await new Promise(resolve => setTimeout(resolve, 5000));
  }

  console.error('Timeout waiting for video');
  return null;
};

// Complete workflow
const createVideo = async (prompt, options = {}) => {
  const generation = await generateVideo(prompt, options);
  if (!generation) return null;

  const result = await waitForVideo(generation.task_id);
  return result;
};

Xác định tình trạng Python

import time

def check_task_status(task_id, api_key):
    url = f"https://api.poyo.ai/api/generate/status/{task_id}"
    headers = {"Authorization": f"Bearer {api_key}"}

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        return response.json()['data']
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        return None

def wait_for_video(task_id, api_key, max_attempts=120):
    for attempt in range(max_attempts):
        task_data = check_task_status(task_id, api_key)

        if not task_data:
            break

        print(f"Status: {task_data['status']}, Progress: {task_data.get('progress', 0)}%")

        if task_data['status'] == 'finished':
            print("Video generated successfully!")
            video_file = next(
                (f for f in task_data.get('files', []) if f['file_type'] == 'video'),
                None
            )
            if video_file:
                print(f"Video URL: {video_file['file_url']}")
            return task_data
        elif task_data['status'] == 'failed':
            print(f"Failed: {task_data.get('error_message')}")
            return None

        time.sleep(5)  # Wait 5 seconds

    print("Timeout")
    return None

# Complete workflow
def create_video(prompt, api_key, **options):
    generation = generate_video(prompt, api_key, **options)
    if not generation:
        return None
    return wait_for_video(generation['task_id'], api_key)

Việc xử lý lỗi

const robustVideoGeneration = async (prompt, options = {}, retries = 3) => {
  for (let attempt = 0; attempt < retries; attempt++) {
    try {
      const response = await axios.post(
        'https://api.poyo.ai/api/generate/submit',
        {
          model: 'kling-2.6',
          input: {
            prompt,
            sound: options.sound ?? true,
            aspect_ratio: options.aspectRatio ?? '16:9',
            duration: options.duration ?? 5
          }
        },
        {
          headers: {
            'Authorization': `Bearer YOUR_API_KEY`,
            'Content-Type': 'application/json'
          },
          timeout: 30000
        }
      );

      return response.data.data;

    } catch (error) {
      if (error.response) {
        const status = error.response.status;

        if (status === 401 || status === 403) {
          throw new Error('Authentication failed');
        }
        if (status === 400) {
          throw new Error(`Invalid request: ${error.response.data?.error}`);
        }
        if (status === 402) {
          throw new Error('Insufficient credits');
        }
      }

      if (attempt === retries - 1) throw error;

      const delay = Math.pow(2, attempt) * 1000;
      await new Promise(resolve => setTimeout(resolve, delay));
    }
  }
};

Những lời khuyên kỹ thuật prompt

Các video hiệu quả

// Structure: Scene + Characters + Action + Audio cues
const effectivePrompts = [
  // Interview style
  `A business woman in a modern office, facing the camera.
   She speaks confidently: "Our company has grown 200% this year."
   She gestures with her hands while explaining.`,

  // Product demo
  `Close-up of hands opening a smartphone box.
   A voice narrates: "Unboxing the latest flagship device."
   The phone is lifted out, camera pans to show the screen.`,

  // Storytelling
  `A child runs through a sunlit meadow, laughing.
   Birds sing in the background.
   The child stops to pick a flower and smiles at the camera.`,

  // Educational
  `An animated diagram of the solar system.
   A narrator explains: "The Earth takes 365 days to orbit the sun."
   Planets rotate in their orbits.`
];

Kết luận

Kling 2.6 API trên PoYo cung cấp việc tạo ra video AI đột phá với khả năng âm thanh bản địa. Tạo video hoàn chỉnh với giọng nói đồng bộ, âm nhạc và hiệu ứng âm thanh trong một cuộc gọi API.

Để biết thêm chi tiết, hãy truy cập Văn bản chính thức Kling 2.6 API.


- Anh cần giúp đỡ? cộng đồng nhà phát triển hoặc khám phá thêm Các hướng dẫn.

Blog · PoYo.aiTất cả bài viết
KẾT NỐI

Bạn đang có dự án?

Hãy chia sẻ nhu cầu của bạn. Chúng tôi sẽ tư vấn API AI phù hợp.

Chúng tôi chỉ dùng thông tin để phản hồi yêu cầu này.

PoYo AI

Sẵn sàng khám phá mô hình?

Xem các mô hình hình ảnh, video, âm thanh và ngôn ngữ trên PoYo.

Xem mô hình AI