API文档

完整的API接口文档,轻松集成到您的项目

5+
API端点
99.9%
可用性
<1s
响应时间
10K+
开发者

🚀 快速开始

1

获取API密钥

注册账号并获取您的API密钥,用于身份验证

2

发送请求

使用您的API密钥发送请求到我们的端点

3

获取结果

接收生成的图像URL并集成到您的应用

认证

所有API请求都需要在请求头中包含API密钥:

Authorization: Bearer YOUR_API_KEY

⚠️ 请妥善保管您的API密钥,不要在客户端代码中暴露。

API端点

GET /text_to_image

根据文本提示生成图像

查看详情 →
GET /image_variations

生成图像的变体版本

查看详情 →
GET /image_edit

编辑和优化现有图像

查看详情 →
GET /batch_generate

批量生成多张图像

查看详情 →
GET /status

查询API状态和使用情况

查看详情 →

文本生图 API

https://core-normal.trae.ai/api/ide/v1/text_to_image

请求参数

参数名 类型 必填 默认值 说明
prompt string - 图像生成的提示词,最多1000字符
image_size string square 图像尺寸:square_hd, square, portrait_4_3, portrait_16_9, landscape_4_3, landscape_16_9
num_images integer 1 生成图像数量,1-4
quality string standard 图像质量:standard, hd, ultra_hd
style string - 艺术风格:realistic, artistic, anime, 3d_render, etc.

响应格式

{
  "success": true,
  "data": {
    "images": [
      {
        "url": "https://example.com/generated/image1.jpg",
        "id": "img_123456",
        "width": 1024,
        "height": 1024
      }
    ],
    "prompt": "A beautiful sunset over the ocean",
    "generation_time": 0.85
  },
  "message": "Image generated successfully"
}

代码示例

Python

import requests

url = "https://core-normal.trae.ai/api/ide/v1/text_to_image"
headers = {
    "Authorization": "Bearer YOUR_API_KEY"
}
params = {
    "prompt": "A beautiful sunset over the ocean",
    "image_size": "square",
    "num_images": 2,
    "quality": "hd"
}

response = requests.get(url, headers=headers, params=params)
result = response.json()

if result["success"]:
    for image in result["data"]["images"]:
        print(f"Generated image: {image['url']}")
else:
    print(f"Error: {result['message']}")

JavaScript (Fetch API)

const url = "https://core-normal.trae.ai/api/ide/v1/text_to_image";
const headers = {
    "Authorization": "Bearer YOUR_API_KEY"
};
const params = new URLSearchParams({
    prompt: "A beautiful sunset over the ocean",
    image_size: "square",
    num_images: 2,
    quality: "hd"
});

fetch(`${url}?${params}`, { headers })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            data.data.images.forEach(image => {
                console.log(`Generated image: ${image.url}`);
            });
        } else {
            console.error(`Error: ${data.message}`);
        }
    })
    .catch(error => console.error('Request failed:', error));

Node.js (Axios)

const axios = require('axios');

const url = "https://core-normal.trae.ai/api/ide/v1/text_to_image";
const headers = {
    "Authorization": "Bearer YOUR_API_KEY"
};
const params = {
    prompt: "A beautiful sunset over the ocean",
    image_size: "square",
    num_images: 2,
    quality: "hd"
};

axios.get(url, { headers, params })
    .then(response => {
        if (response.data.success) {
            response.data.data.images.forEach(image => {
                console.log(`Generated image: ${image.url}`);
            });
        } else {
            console.error(`Error: ${response.data.message}`);
        }
    })
    .catch(error => console.error('Request failed:', error));

cURL

curl -X GET "https://core-normal.trae.ai/api/ide/v1/text_to_image?prompt=A%20beautiful%20sunset%20over%20the%20ocean&image_size=square&num_images=2&quality=hd" \
  -H "Authorization: Bearer YOUR_API_KEY"

错误处理

状态码 错误类型 说明 解决方案
400 Bad Request 请求参数错误 检查请求参数是否正确
401 Unauthorized API密钥无效或过期 检查API密钥是否正确
429 Too Many Requests 超过速率限制 降低请求频率或升级套餐
500 Internal Server Error 服务器内部错误 稍后重试或联系技术支持

速率限制

为了确保服务的稳定性,我们对API请求实施速率限制:

  • 免费套餐: 100次请求/小时
  • 基础套餐: 1000次请求/小时
  • 专业套餐: 10000次请求/小时
  • 企业套餐: 无限制

当超过速率限制时,API将返回429状态码。请在响应头中查看 X-RateLimit-RemainingX-RateLimit-Reset 来了解剩余配额和重置时间。

💡 最佳实践

缓存结果

对于相同的提示词,缓存生成的图像以减少API调用次数和成本。

错误重试

实现指数退避重试机制,处理临时性错误和网络问题。

监控使用

定期检查API使用情况,避免超出配额限制。

安全存储

将API密钥存储在环境变量中,不要硬编码在代码中。

准备好开始集成了吗?

立即获取API密钥,开始构建您的AI图像应用

前往在线生图 →