PHP Laravel 与 Node.js Express 接入 API Store Hub
数分钟内为现有 Laravel 或 Express 后端接入 AI 文案生成、订单摘要与智能客服接口。
准备好通过 API Store Hub 的统一 API 密钥、透明计费与多上游容灾把 AI 投入生产了吗?
安装命令
| Language | PHP / TypeScript |
|---|---|
| Runtime | PHP 8.2+ (Laravel 11) / Node.js 20+ (Express 5) |
| Protocol | OpenAI Compatible (/v1/chat/completions) |
| 安装命令 | npm install openai express |
3 步快速接入流程
- 在 API Store Hub 创建 API 密钥: 登录 API Store Hub 控制台生成前缀为 sk-ash- 的密钥,并按需配置日/月消费上限。
- 安装 SDK 并配置 Base URL: 安装官方 SDK 包并将环境变量指向 https://apistorehub.com,以通过 /v1/chat/completions 发起请求。
- 调用模型并启用流式与重试: 使用显式超时与带抖动重试调用 gpt-6.1-terra;每次调用均记录 token、TTFT 与精确费用。
生产级示例代码
PHP / TypeScript
import express from "express";
import OpenAI from "openai";
const app = express();
app.use(express.json());
const openai = new OpenAI({ baseURL: "https://apistorehub.com/v1", apiKey: process.env.API_KEY });
app.post("/chat", async (req, res) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
const stream = await openai.chat.completions.create({
model: "gpt-6.1-terra",
stream: true,
messages: [{ role: "user", content: String(req.body.prompt || "Hello") }],
});
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta?.content || "";
if (text) res.write(`data: ${JSON.stringify({ text })}\n\n`);
}
res.end();
});cURL
curl https://apistorehub.com/v1/chat/completions \
-H "Authorization: Bearer sk-ash-your_key" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-6.1-terra","messages":[{"role":"user","content":"Generate product meta description"}]}'推荐模型与实时价格
| 模型 | Token 单价 | 上下文与端点 |
|---|---|---|
| GPT 6.1 Terra | 3,400 ST / 1M tokens | 1.1M |
| DeepSeek V4 Flash | 1,500 ST / 1M tokens | 1M |
| GPT Image 2.5 | 2,500 ST / req | - |
生产环境最佳实践清单
- 请将批量生成与文档分析投递至 Laravel Queue 或 BullMQ 队列,避免阻塞 PHP-FPM 或 HTTP 主线程。
- 对长输出(> 500 token)始终开启 stream=true,以降低首字延迟(TTFT)并防止连接超时。
- 切勿在前端代码中硬编码密钥;请通过服务端环境变量注入并在 API Store Hub 控制台定期轮换。
- 为 429/502/529 配置带抖动的指数退避,并在同一密钥下准备备用模型实现无缝回退。
常见问题解答
- 将 PHP Laravel & Node.js Express 接入 API Store Hub 是否需要重写现有调用逻辑?
- 不需要。API Store Hub 原生兼容 OpenAI(https://apistorehub.com/v1)与 Anthropic Messages(https://apistorehub.com/v1/messages)协议,只需替换 Base URL 与 sk-ash- 密钥。
- 一个 API 密钥可以同时支持流式输出、函数调用和向量嵌入吗?
- 可以。API Store Hub 的单一密钥即可调用全部 187 个模型,完整支持 SSE 流式、严格 JSON Schema、工具调用与向量嵌入。
- 费用与速率限制如何控制?
- 每次请求均按 token 或按次透明计费并可在 API Store Hub 控制台查询,绝不透支余额,且支持为单个密钥设置消费上限。