API Stock 与 PiAPI 对比
何时选择 API Stock
对于已支持的 PiAPI 图像模型,如果希望使用 Bearer 验证和一个公开模型 id,并将参数放在 input 内,可选择 API Stock。
横向滚动以查看表格的所有列。
| 集成细节 | PiAPI | API Stock |
|---|---|---|
| 请求 | POST /api/v1/task | POST /api/v1/generation/create |
| 身份验证 | X-API-Key | Authorization: Bearer |
| 模型标识符 | model: gemini / task_type: gemini-2.5-flash-image | nano-banana |
这里只比较下方示例模型,并非服务商的全部端点。模型同名不代表参数、审核规则或输出完全一致。
价格对比:PiAPI 与 API Stock
选取双方共有的模型,按相同计费单位比较。我方价格来自当前目录;对方价格为附有核查日期和来源链接的记录。
| 模型 | API Stock | PiAPI | 我方价格差异 |
|---|---|---|---|
| Nano Banana每张图片 · 文本生成图片 | API Stock$0.012 | PiAPI$0.03 | 低 60% |
| Nano Banana Pro1K · 每张图片 · 文本生成图片 | API Stock$0.029 | PiAPI$0.105 | 低 72.4% |
| Nano Banana Pro4K · 每张图片 · 文本生成图片 | API Stock$0.043 | PiAPI$0.18 | 低 76.1% |
文本生成单张图片,不含参考图片。此处为公开基础价格;订阅、批量折扣、其他分辨率和编辑可能影响最终费用。
工作量费用示例
Nano Banana
按上述配置生成 1,000 张图片
- API Stock
- $12
- PiAPI
- $30
Nano Banana Pro · 1K
按上述配置生成 1,000 张图片
- API Stock
- $29
- PiAPI
- $105
按上表计算的示例总价,不含批量折扣、订阅或付费附加选项。不代表相同延迟或完全一致的输出。
从 PiAPI 迁移
将 X-API-Key 改为 Authorization: Bearer。将 model 和 task_type 映射到公开目录 id,参数保留在 input 内。读取 data.taskId 而非 task_id,并适配状态和 webhook 解析。
1
创建账号、充值预付余额并生成 API 密钥。
2
向 /api/v1/generation/create 发送 POST:model 使用公开目录 id,参数放在 input 内。每个任务生成一个结果。
3
确认 HTTP 成功且 code = 200 后,保存 data.taskId。轮询 GET /api/v1/task/status/TASK_ID,或在创建时设置 webhook。
PiAPI
curl --fail-with-body 'https://api.piapi.ai/api/v1/task' \-H "X-API-Key: $PIAPI_API_KEY" \-H 'Content-Type: application/json' \-d '{"model": "gemini","task_type": "gemini-2.5-flash-image","input": {"prompt": "A ceramic mug on a wooden table, soft morning light","aspect_ratio": "1:1"}}'
API Stock
curl --fail-with-body 'https://api.api-stock.com/api/v1/generation/create' \-H "Authorization: Bearer $API_KEY" \-H 'Content-Type: application/json' \-d '{"model": "nano-banana","input": {"prompt": "A ceramic mug on a wooden table, soft morning light"}}'
完整示例:创建任务并获取文件
在设置了 API_KEY 的服务器上使用 Node 22+ 或 Bun 运行。示例创建一个付费任务,在轮询前输出 ID;轮询上限为 10 分钟和 300 次。要继续查询,将 TASK_ID 设为保存的 ID 后重新运行。如果创建超时或响应无效,请先检查生成历史,再决定是否创建另一个任务。结果 URL 是临时的,请将文件保存到自己的存储中。
import { writeFile } from "node:fs/promises";const baseURL = "https://api.api-stock.com/api/v1";const apiKey = process.env.API_KEY;if (!apiKey) throw new Error("Set API_KEY on the server");async function request(path, options = {}) {const response = await fetch(baseURL + path, {...options,headers: {Authorization: `Bearer ${apiKey}`,"Content-Type": "application/json",},signal: AbortSignal.timeout(30_000),});if (!response.ok) throw new Error(`HTTP ${response.status}`);const body = await response.json();if (!body || body.code !== 200) {throw new Error(`API error: ${body?.code ?? "missing code"}`);}if (!body.data || typeof body.data !== "object" || Array.isArray(body.data)) {throw new Error("Malformed response data");}return body.data;}// Set TASK_ID to resume an existing task without another paid POST.let taskId = process.env.TASK_ID?.trim();if (!taskId) {try {const data = await request("/generation/create", {method: "POST",body: JSON.stringify({"model": "nano-banana","input": {"prompt": "A ceramic mug on a wooden table, soft morning light"}}),});if (typeof data.taskId !== "string" || !data.taskId.trim()) {throw new Error("Missing taskId");}taskId = data.taskId;} catch (error) {throw new Error(`${error.message}. Creation outcome unconfirmed: check generation history before creating another task.`);}}console.log("Save taskId:", taskId, "Resume with TASK_ID set to this ID.");const deadline = Date.now() + 10 * 60_000;let fileUrl;for (let attempt = 0; attempt < 300 && Date.now() < deadline; attempt++) {let data;try {data = await request(`/task/status/${encodeURIComponent(taskId)}`);} catch (error) {throw new Error(`${error.message}. Set TASK_ID=${taskId} to resume polling; do not create again.`);}if (data.status === "finished") {fileUrl = data.files?.[0]?.fileUrl;if (typeof fileUrl !== "string" || !fileUrl) throw new Error("Task finished without a file");break;}if (data.status === "failed" || data.status === "expired") {throw new Error(data.errorMessage || data.status);}if (data.status !== "not_started" && data.status !== "processing") {throw new Error("Malformed task status; use the saved taskId to check history");}await new Promise((resolve) => setTimeout(resolve, 2000));}if (!fileUrl) throw new Error(`Still processing: set TASK_ID=${taskId} to resume polling`);const file = await fetch(fileUrl, { signal: AbortSignal.timeout(60_000) });if (!file.ok) throw new Error(`Download failed: ${file.status}`);const mime = file.headers.get("content-type")?.split(";")[0];const extension = { "image/png": "png", "image/jpeg": "jpg", "image/webp": "webp" }[mime] || "bin";await writeFile(`generated-image.${extension}`, new Uint8Array(await file.arrayBuffer()));
将 API 密钥保存在服务器端。迁移生产流量前,先测试一个请求的参数、状态处理和输出。
常见问题
如何从 PiAPI 迁移?
将 X-API-Key 改为 Authorization: Bearer。将 model 和 task_type 映射到公开目录 id,参数保留在 input 内。读取 data.taskId 而非 task_id,并适配状态和 webhook 解析。
API Stock 总比 PiAPI 便宜吗?
并非如此。请比较表中具体模型、配置和计费单位。价格可能上涨或下降;迁移生产流量前请核查双方目录。
能迁移所有 PiAPI 工作流吗?
PiAPI 的部分任务类型和自有账号托管流程在此没有直接对应方案。请逐项检查;模型名称相同不代表支持全部任务类型。
失败或未完成的请求如何处理?
媒体任务按已配置路由重试,有可用备用服务商时才会切换。生成最终失败会退款。正在处理的任务应继续轮询,不要重复创建。文件链接过期不代表生成失败。
如何充值余额?
可用的付款方式、币种和最低充值金额会在付款前显示在您的账户中。无需订阅,模型使用费用从预付余额中扣除。
在 API Stock 尝试第一个请求
选择模型,确认参数和价格,再创建 API 密钥。迁移流量前先测试一个请求。