平台指南

图像生成

使用文生图或图生图模型生成图片,并正确处理 URL 或 Base64 返回值。

选择图像模型

先在模型与价格页面筛选“图像”类型,确认模型名称、支持的端点和计费单位。不同模型支持的尺寸、质量和参考图数量可能不同。

以目录为准 如果模型目录没有列出图像端点,请不要直接套用本页示例。

发起生成请求

图像生成使用 OpenAI 兼容的 Images API。请求至少需要 model 和 prompt;size、quality、style 与 response_format 按模型能力选择。

bash
curl https://api.moloop.io/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "YOUR_IMAGE_MODEL_ID",
    "prompt": "A clean product photo of a ceramic cup on a wood table",
    "size": "1024x1024",
    "n": 1,
    "response_format": "url"
  }'

参数与返回结果

参数说明
prompt描述画面、主体、风格和限制条件。
size使用模型支持的宽高,例如 1024x1024。
response_formaturl 便于下载;b64_json 适合由服务端自行保存。
n一次请求生成的图片数量;数量越多,消耗通常越高。

成功响应通常在 data 数组中返回 url 或 b64_json。URL 可能有有效期,生产环境应在收到响应后立即下载并保存到自己的对象存储。

javascript
const response = await fetch("https://api.moloop.io/v1/images/generations", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer YOUR_API_KEY",
  },
  body: JSON.stringify({
    model: "YOUR_IMAGE_MODEL_ID",
    prompt: "A paper-cut illustration of a city at sunrise",
    size: "1024x1024",
    response_format: "url",
  }),
});

const result = await response.json();
const imageUrl = result.data?.[0]?.url;

失败排查

  1. 401:检查 API 密钥是否完整、启用且未过期。

  2. 400:检查模型是否支持 Images API,以及 size、quality 等参数是否在模型允许范围内。

  3. 429:降低并发并使用指数退避;不要无上限重复生成。

  4. 结果无法打开:优先下载 URL,或改用 b64_json 并由服务端保存。