本页示例为基于 cURL 官方示例的等价 Node.js 转写,未经服务端联调,请以实际返回结果为准。
最小调用示例
const res = await fetch("https://geobaseapi.aibase.com/v1/openapi/execute", {
method: "POST",
headers: {
"Content-Type": "application/json",
"AIBase-API-Key": process.env.AIBASE_API_KEY,
"AIBase-Request-Id": "req-20260924-001",
},
body: JSON.stringify({ apiCode: "geo.brand", data: {} }),
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
console.log(await res.json());
带错误处理的封装
const ENDPOINT = "https://geobaseapi.aibase.com/v1/openapi/execute";
async function callGeoBase(apiCode, data = {}) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 60_000);
try {
const res = await fetch(ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
"AIBase-API-Key": process.env.AIBASE_API_KEY,
"AIBase-Request-Id": "req-20260924-001",
},
body: JSON.stringify({ apiCode, data }),
signal: controller.signal,
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
const body = await res.json();
if (body.code === 200) return body.data;
if (body.code === 2001) return null; // 暂无数据
throw new Error(`GEOBase 业务错误 ${body.code}: ${body.msg}`);
} finally {
clearTimeout(timer);
}
}
// 分页结构接口:读取 records
const page = await callGeoBase("geo.brand", { brandId: "brand-001" });
for (const item of page?.records ?? []) {
console.log(item.id, item.brandName);
}
// 分组结构接口:直接读取 data
const trend = await callGeoBase("llm.trend", {
brandId: "brand-001",
startDate: "2026-09-01",
endDate: "2026-09-23",
});
console.log(trend);
本地调试
const ENDPOINT = "http://localhost:6006/v1/openapi/execute";
浏览器前端直接调用会暴露 API Key。请在服务端或边缘函数中发起调用,前端只与自己的后端交互。