> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aibase.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js 调用示例

> 使用 fetch 调用 GEOBase 统一入口的完整示例。

<Note>
  本页示例为基于 cURL 官方示例的等价 Node.js 转写，未经服务端联调，请以实际返回结果为准。
</Note>

## 最小调用示例

```javascript theme={null}
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());
```

## 带错误处理的封装

```javascript theme={null}
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);
```

## 本地调试

```javascript theme={null}
const ENDPOINT = "http://localhost:6006/v1/openapi/execute";
```

<Warning>
  浏览器前端直接调用会暴露 API Key。请在服务端或边缘函数中发起调用，前端只与自己的后端交互。
</Warning>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.