> ## 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.

# Python 调用示例

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

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

## 最小调用示例

```python theme={null}
import os
import requests

res = requests.post(
    "https://geobaseapi.aibase.com/v1/openapi/execute",
    headers={
        "AIBase-API-Key": os.environ["AIBASE_API_KEY"],
        "Content-Type": "application/json",
        "AIBase-Request-Id": "req-20260924-001",
    },
    json={"apiCode": "geo.brand", "data": {}},
    timeout=60,
)
res.raise_for_status()
print(res.json())
```

## 带错误处理的封装

```python theme={null}
import os
import requests
from typing import Any

ENDPOINT = "https://geobaseapi.aibase.com/v1/openapi/execute"


def call_geobase(api_code: str, data: dict | None = None) -> Any:
    """调用 GEOBase 统一入口，返回业务 data。"""
    payload = {"apiCode": api_code, "data": data or {}}

    res = requests.post(
        ENDPOINT,
        headers={
            "AIBase-API-Key": os.environ["AIBASE_API_KEY"],
            "Content-Type": "application/json",
            "AIBase-Request-Id": "req-20260924-001",
        },
        json=payload,
        timeout=(10, 60),  # (连接超时, 读取超时)
    )
    res.raise_for_status()

    body = res.json()
    code = body.get("code")

    if code == 200:
        return body.get("data")
    if code == 2001:
        return None  # 暂无数据，按空结果处理

    raise RuntimeError(f"GEOBase 业务错误 {code}: {body.get('msg')}")


# 分页结构接口：读取 records
brands = call_geobase("geo.brand", {"brandId": "brand-001"})
for item in (brands or {}).get("records", []):
    print(item["id"], item["brandName"])

# 分组结构接口：直接读取 data
trend = call_geobase(
    "llm.trend",
    {"brandId": "brand-001", "startDate": "2026-09-01", "endDate": "2026-09-23"},
)
print(trend)
```

## 本地调试

```python theme={null}
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.