示例代码

各种编程语言的 API 调用示例,快速上手 AICG

Py Python

Python

文生图 - 生成图片

import requests

api_key = "YOUR_API_KEY"
url = "https://api.aicg.com/v1/images/generations"

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

data = {
    "prompt": "一只可爱的猫咪在草地上玩耍",
    "model": "dall-e-3",
    "n": 1,
    "size": "1024x1024"
}

response = requests.post(url, json=data, headers=headers)
print(response.json())
使用 Python requests 库调用文生图 API,生成一张猫咪图片。
Python

文生视频 - 生成视频

import requests

api_key = "YOUR_API_KEY"
url = "https://api.aicg.com/v1/videos/generations"

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

data = {
    "prompt": "日出时分,海浪拍打金色沙滩",
    "model": "sora",
    "duration": 10
}

response = requests.post(url, json=data, headers=headers)
result = response.json()
print(f"视频生成中,ID: {result['id']}")
使用 Python 调用文生视频 API,生成一段海浪日出视频。

JS JavaScript / Node.js

JavaScript

Node.js - 文生图

const axios = require('axios');

async function generateImage() {
  const response = await axios.post(
    'https://api.aicg.com/v1/images/generations',
    {
      prompt: '一只可爱的猫咪在草地上玩耍',
      model: 'dall-e-3',
      n: 1,
      size: '1024x1024'
    },
    {
      headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
      }
    }
  );
  
  console.log(response.data);
}

generateImage();
使用 Node.js axios 库调用文生图 API,使用 async/await 语法。

cURL

cURL

命令行快速测试

curl -X POST https://api.aicg.com/v1/images/generations \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "一只可爱的猫咪在草地上玩耍",
    "model": "dall-e-3",
    "n": 1,
    "size": "1024x1024"
  }'
使用 cURL 命令行工具快速测试 API,无需安装任何编程语言环境。