使用 Node.js 22+ 和原生 fetch 验证 Key,无需安装 SDK。此处只验证服务端身份;快速开始继续讲解 Customer 会话和 Web 读取。
在应用根目录保存被 Git 忽略的 .env.local,把下面的站点根地址、App ID 和 Key 换成 SDK 接入页里的值。INDIANA_SECRET_KEY 只在后端读取,不加 NEXT_PUBLIC_ 前缀。
在同一目录新建 indiana-server-check.mjs,完整复制下面代码。脚本会创建并保留一个 Sandbox 测试客户;不会签发客户 Token;符合资格的免费默认 Product 可以初始化,付费 Product/trial 必须走独立 billing。
首次请求完成
成功输出包含 requestId、Sandbox scope 和 customerId。重复运行返回同一 customerId,created 变为 false,表示找到了已有 Customer。
实际业务中,把固定测试 externalUserId 换成后端已经验证或业务任务已经授权的稳定用户 ID。不要把浏览器提交的 customerId 或用户 ID 直接当作授权。
1# 从 SDK 接入页复制 Sandbox 配置;此文件须被 Git 忽略。2INDIANA_BASE_URL=https://your-indiana-host.example3INDIANA_PROJECT_ID=your-app-id4INDIANA_SECRET_KEY=your-sandbox-server-key
indiana-server-check.mjsJavaScript 1class ConfigurationError extends Error {}2class RequestError extends Error {3 constructor(code, status, requestId) { super(code); this.code = code; this.status = status; this.requestId = requestId; }4}5function required(name) {6 const value = process.env[name];7 if (!value) throw new ConfigurationError("Missing environment variable: " + name);8 return value;9}10try {11 const baseUrl = required("INDIANA_BASE_URL");12 const key = required("INDIANA_SECRET_KEY");13 const projectId = required("INDIANA_PROJECT_ID");14 const response = await fetch(new URL("/api/v1/customers", baseUrl).href, {15 method: "POST", redirect: "error", signal: AbortSignal.timeout(10000),16 headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json",17 "X-Indiana-Project": projectId, "X-Indiana-Environment": "sandbox" },18 body: JSON.stringify({ externalUserId: "docs-server-auth-check" }),19 });20 const body = await response.json();21 if (!response.ok) throw new RequestError(body.error?.code ?? "HTTP_ERROR", response.status, body.requestId);22 const { requestId, scope, customer, created } = body;23 console.log(JSON.stringify({ requestId, scope, customerId: customer.id, created }, null, 2));24} catch (error) {25 if (error instanceof RequestError) console.error(JSON.stringify({ code: error.code, status: error.status, requestId: error.requestId }));26 else if (error instanceof ConfigurationError) console.error(error.message);27 else console.error("请求未完成。检查 Node 版本、环境文件和网络配置。");28 process.exitCode = 1;29}
1node --env-file=.env.local indiana-server-check.mjs
1{2 "requestId": "<request-id>",3 "scope": {4 "projectId": "<your-app-id>",5 "environment": "sandbox"6 },7 "customerId": "<customer-id>",8 "created": true9}