跳至正文
全部 API 文档

快速入门

配置 API 密钥

Pro、Pro Max、Enterprise、Enterprise Plus 订阅及 Creator、Business、Enterprise、Enterprise Plus 积分包包含 API。合格积分包余额用尽后仍可查询和下载历史,新生成需要足够积分。全额退款或争议暂停会撤回对应购买的资格,其他合格购买不受影响。

由服务端发送 Authorization: Bearer YOUR_API_KEY;网站 Cookie 不能作为 API 凭据。每次请求都会检查 API 资格和密钥权限。

使用 Node.js 22 或更高版本。将 GAPTI_ORIGIN 设为 Gapti 站点源地址,并在服务端设置 GAPTI_API_KEY。为密钥授予 models:read、generations:write 和 generations:read。上传参考素材还需要 assets:write。

管理 API 密钥

查询模型目录

使用你的 API 密钥调用 GET /api/v1/models,查看账户可用的模型及选项。

curl --fail-with-body -H "Authorization: Bearer $GAPTI_API_KEY" "$GAPTI_ORIGIN/api/v1/models"

上传参考素材

如需为示例上传参考图片,将 REFERENCE_PNG 设为本地 PNG 文件路径。先声明 MIME 类型和字节数,再通过返回的上传地址 PUT 文件,最后完成上传。在生成草稿中使用本账户返回的 Gapti 素材 ID。

Gapti 接受单帧 JPEG、PNG 或 WebP 图片,文件不超过 10 MiB,总像素不超过 4000 万,每边最多 16,384 像素。编辑图片时使用 referenceAssetIds,支持首帧输入的视频模型使用 firstFrameAssetId。

  1. POST /api/v1/uploads
  2. PUT {uploadUrl}
  3. POST /api/v1/uploads/{uploadId}/complete

确认报价

运行 quote 命令保存 normalizedDraft、完整报价和幂等键。确认总价后再运行 submit。原样提交返回的报价,保留其中的签名。

如需验证美元报价,可将标准化草稿和原报价发送至 POST /api/v1/quotes/validate。报价变化或到期后,请重新获取并确认。

提交任务

确认报价后,用同一任务文件运行 submit。如果提交结果不明,用该文件再次运行 submit,复用原请求体和 Idempotency-Key。如果接口返回 QUOTE_CHANGED,请获取并确认新报价后再提交。

只有生成提交消耗账号积分,报价和创建密钥不扣费。供应商失败沿用现有退款及对账流程;原请求结果未明时,不要更换幂等键重新提交。

轮询与下载

用同一任务文件运行 download。按 pollAfterMs 所示间隔,使用返回的任务类型和 ID 查询到任务结束。向每个受保护的输出地址发送 Bearer 令牌,跟随下载重定向时移除令牌。

Node.js 示例

将示例保存为 gapti.mjs。按顺序运行各命令,每个新任务使用新的任务文件名。

  1. node gapti.mjs quote
  2. node gapti.mjs submit
  3. node gapti.mjs download
import { randomUUID } from 'node:crypto'
import { readFile, writeFile } from 'node:fs/promises'
import { basename } from 'node:path'
import { setTimeout as delay } from 'node:timers/promises'

const [command, jobFile = 'gapti-job.json'] = process.argv.slice(2)
const { GAPTI_ORIGIN, GAPTI_API_KEY, REFERENCE_PNG } = process.env
if (!GAPTI_ORIGIN || !GAPTI_API_KEY) {
  throw new Error('Set GAPTI_ORIGIN and GAPTI_API_KEY')
}
const origin = new URL(GAPTI_ORIGIN).origin

async function request(path, options = {}) {
  const url = new URL(path, origin)
  if (url.origin !== origin) throw new Error('Expected a same-origin API URL')
  const response = await fetch(url, {
    ...options,
    redirect: 'manual',
    headers: { Authorization: 'Bearer ' + GAPTI_API_KEY, ...options.headers },
  })
  if (!response.ok && response.status !== 302) {
    const retry = response.headers.get('Retry-After')
    throw new Error(response.status + ' ' + await response.text() +
      (retry ? ' (Retry-After: ' + retry + ')' : ''))
  }
  return response
}

async function post(path, payload, headers = {}) {
  return (await request(path, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', ...headers },
    body: JSON.stringify(payload),
  })).json()
}

async function uploadPng(file) {
  const bytes = await readFile(file)
  const mime = 'image/png'
  const upload = await post('/api/v1/uploads', {
    mime, bytes: bytes.length, fileName: basename(file),
  })
  await request(upload.uploadUrl, {
    method: 'PUT', headers: { 'Content-Type': mime }, body: bytes,
  })
  const asset = await post('/api/v1/uploads/' +
    encodeURIComponent(upload.uploadId) + '/complete')
  return asset.id
}

if (command === 'quote') {
  const draft = {
    "kind": "image",
    "modelPreset": "gpt-image-2",
    "mode": "text-to-image",
    "prompt": "Photograph a yellow ceramic mug on a pale blue background. Light it from the left.",
    "aspectRatio": "1:1",
    "resolution": "1k",
    "outputCount": 1,
    "toolId": null,
    "negativePrompt": "",
    "referenceAssetIds": [],
    "generationQuality": null,
    "seed": null,
    "imageSetMode": false
  }
  const { items } = await (await request('/api/v1/models')).json()
  if (!items.some(item => item.kind === 'image' && item.id === draft.modelPreset)) {
    throw new Error('This model is absent from your API catalog')
  }
  if (REFERENCE_PNG) {
    const assetId = await uploadPng(REFERENCE_PNG)
    draft.mode = 'image-to-image'
    draft.referenceAssetIds = [assetId]
  }
  const quoted = await post('/api/v1/quotes', { draft })
  const submission = {
    draft: quoted.normalizedDraft,
    quoteAcknowledgement: quoted.quote,
  }
  await writeFile(jobFile, JSON.stringify({
    idempotencyKey: randomUUID(), submission,
  }, null, 2), { flag: 'wx', mode: 0o600 })
  console.log('Review this quote before submitting:', quoted.quote)
} else if (command === 'submit') {
  const job = JSON.parse(await readFile(jobFile, 'utf8'))
  const accepted = await post('/api/v1/generations', job.submission, {
    'Idempotency-Key': job.idempotencyKey,
  })
  await writeFile(jobFile + '.accepted', JSON.stringify(accepted), { mode: 0o600 })
  console.log(accepted.task)
} else if (command === 'download') {
  const accepted = JSON.parse(await readFile(jobFile + '.accepted', 'utf8'))
  const { kind, id } = accepted.task
  const taskPath = '/api/v1/generations/' + kind + '/' + encodeURIComponent(id)
  let wait = accepted.pollAfterMs
  let detail
  do {
    await delay(wait)
    detail = await (await request(taskPath)).json()
    console.log(detail.status)
    wait = detail.pollAfterMs
  } while (wait !== null)

  if (detail.status === 'failed') throw new Error(JSON.stringify(detail.error))
  for (const output of detail.outputs) {
    if (!output.downloadUrl) continue
    const redirect = await request(output.downloadUrl)
    const location = redirect.headers.get('Location')
    if (redirect.status !== 302 || !location) throw new Error('Expected a download redirect')
    const file = await fetch(new URL(location, origin))
    if (!file.ok) throw new Error('Download failed: ' + file.status)
    const extension = { 'image/png': 'png', 'image/jpeg': 'jpg',
      'image/webp': 'webp', 'video/mp4': 'mp4' }[output.mime] ?? 'bin'
    await writeFile(id + '-' + output.index + '.' + extension,
      Buffer.from(await file.arrayBuffer()))
  }
} else {
  throw new Error('Use quote, submit, or download')
}

错误与限制

每个密钥每分钟最多 60 个已认证请求,429 响应带 Retry-After。有效期为 1–365 天,可随时撤销。

401:密钥无效。402:积分不足。403:缺少 API 资格或权限。409:报价变化或请求冲突。422:输入不受支持。429:请求限流。503:暂时不可用。付费结果不明时,必须使用原请求编号核对。

批量提交

开放扩展生成的美元账户可向 POST /api/v1/generations/batch 提交 1–10 个条目。为每个 clientRequestId 使用不同的 UUID,并附上标准化草稿和 quoteAcknowledgement。各条目单独返回任务或错误。提交结果不明时,复用原 clientRequestId 和请求内容。