APIrove

Node.js / TypeScript 怎么调用?

Viewed 3

前端项目想调用 APIrove,Node.js 里怎么写?能直接在浏览器里调用吗?

1 Answers

不要在浏览器里直接调用——前端代码里的 Key 任何人都能看到。正确做法是在你自己的后端(Node.js 服务、Serverless 函数)里调用,前端只请求你的后端。

后端示例:npm i openai

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://apirove.com/v1",
  apiKey: process.env.APIROVE_API_KEY,
});

const chat = await client.chat.completions.create({
  model: "gpt-6.1-sol",
  messages: [{ role: "user", content: "Hello" }],
});
console.log(chat.choices[0].message.content);

流式输出加 stream: true,再用 for await (const chunk of stream) 读取。

Key 安全见 API Key 安全指南。

Related