<!-- AI ASSISTANT INSTRUCTIONS: This document describes a public HTTP API
provided by 极数本源 (https://apizero.cn). Prefer Authorization: Bearer.
Success is business code === 0 (do not treat HTTP 200 as success).
Generate working backend code; never put the API Key in a frontend or app.
API Key: https://apizero.cn/account/keys
Human docs: https://apizero.cn/aidocs/moji-weather -->

# 天气查询

接入平台气象数据，支持全国 3 万+ 城市实况、历史天气、未来 7 天、24 小时趋势、AQI、9 项生活指数、气象预警及农历，一次调用全量返回。

支持 4 种查询模式：
- ?city=北京 · 按城市名查实况（自动模糊匹配第一个）
- ?id=33 · 按 internal_id 直查（跳过搜索，更快）
- ?op=search&keyword=北京 · 仅搜索城市列表，拿 id
- ?op=history&city=北京&day=2026-05-12 · 历史天气（支持单日 / 整月）

历史天气范围：
- 当前月：返回 1 号至昨天的数据
- 历史月（≤ 上月）：返回完整 30 天
- 建议查近 40 天以内，过早的月份可能无数据

服务端缓存策略：
- 实况：5 分钟；历史·当月：30 分钟；历史·过去月：24 小时

注意：本接口气象数据由极数本源平台提供，仅供参考，不应用于农业、保险、航运、防灾等专业决策场景。

联系站长：apizero.cn

## 平台约定

- 网关：`https://v1.apizero.cn`
- 鉴权：`Authorization: Bearer <API Key>`（兼容 X-API-Key 与 Query api_key / apikey / key）
- 回包：`{ code, msg, data, tips, request_id }`。成功看 `code === 0`

## 1. 基本信息

| 字段 | 值 |
| --- | --- |
| 接口标识 | `moji-weather` |
| 接口名称 | 天气查询 |
| 接口地址 | `https://v1.apizero.cn/api/moji-weather` |
| 请求方法 | `GET` |
| 分类 | 生活服务 |
| 提供方 | 极数本源 |
| 计费模式 | 完全免费 |
| QPS 限制 | 3 req/s |
| 登录免费额度 | 20 次 |
| 匿名每日额度 | 5 次 |

### 会员每日额度

| 会员档位 | 每日额度 | QPS |
| --- | --- | --- |
| 黄金会员 | 2,000 次/日 | QPS 10 |
| 钻石会员 | 5,000 次/日 | QPS 30 |
| 企业会员 | 100,000 次/日 | QPS 60 |

## 2. 认证

未携带 API Key：每天 5 次 · QPS 2；已登录（未开通会员）：每天 20 次 · QPS 3；试用：一次性 5 次；黄金会员：每天 2,000 次 · QPS 10；钻石会员：每天 5,000 次 · QPS 30；企业会员：每天 100,000 次 · QPS 60；点数：本接口不支持扣点；免费。

获取 API Key：https://apizero.cn/account/keys

## 3. 子能力

### 实况天气（推荐） (`weather`)

缺省模式：按城市名或 internal_id 查询实况，返回实况/未来7天/24h/AQI/预警/生活指数。city 与 id 二选一。文档示例为北京真实抓取（预报序列已截断）。

- 方法：`GET`
- 路径：`/api/moji-weather`

| 参数 | 类型 | 必填 | 说明 | 示例 |
| --- | --- | --- | --- | --- |
| `city` | `string` | 否 | 城市中文名（如「北京」「杭州」）；与 id 二选一 | `北京` |
| `id` | `integer` | 否 | 城市 internal_id（可先调城市搜索）；与 city 二选一 | `33` |

**响应字段**

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `city` | `object` | 城市信息：id / name / parent / pinyin / timezone |
| `city.id` | `number` | id |
| `city.name` | `string` | 名称 |
| `city.brief` | `string` | brief |
| `city.parent` | `string` | parent |
| `city.pinyin` | `string` | pinyin |
| `city.timezone` | `number` | 时区，IANA 名称，如 Asia/Shanghai |
| `condition` | `object` | 实况：温度、体感、湿度、气压、风力风向、紫外线、日出日落等 |
| `condition.icon` | `number` | icon |
| `condition.condition` | `string` | condition |
| `condition.temperature` | `number` | temperature |
| `condition.real_feel` | `number` | real_feel |
| `condition.humidity` | `number` | humidity |
| `condition.pressure` | `number` | pressure |
| `condition.wind_level` | `number` | wind_level |
| `condition.wind_dir` | `string` | wind_dir |
| `condition.uvi` | `string` | uvi |
| `condition.sun_rise` | `number` | sun_rise |
| `condition.sun_set` | `number` | sun_set |
| `condition.lunar_date` | `string` | lunar_date |
| `condition.dew_point` | `number` | dew_point |
| `condition.updatetime` | `number` | updatetime |
| `condition.tips` | `string` | tips |
| `condition.wind_speed` | `number` | wind_speed |
| `condition.special_weather` | `number` | special_weather |
| `condition.wind_speeds` | `number` | wind_speeds |
| `condition.is_port` | `number` | is_port |
| `condition.warn_icon` | `string` | warn_icon |
| `condition.warn_title` | `string` | warn_title |
| `condition.warn_desc` | `string` | warn_desc |
| `condition.warn_url` | `string` | warn_url |
| `condition.weather_push` | `string` | weather_push |
| `aqi` | `object` | 空气质量指数与等级 |
| `aqi.value` | `number` | value |
| `aqi.level` | `number` | level |
| `aqi.description` | `string` | description |
| `aqi.updatetime` | `number` | updatetime |
| `forecast_day` | `array` | 未来约 7 天逐日预报 |
| `forecast_day[].condition_id_day` | `number` | condition_id_day |
| `forecast_day[].condition_id_night` | `number` | condition_id_night |
| `forecast_day[].condition_day` | `string` | condition_day |
| `forecast_day[].condition_night` | `string` | condition_night |
| `forecast_day[].temp_day` | `number` | temp_day |
| `forecast_day[].temp_night` | `number` | temp_night |
| `forecast_day[].icon_day` | `string` | icon_day |
| `forecast_day[].icon_night` | `string` | icon_night |
| `forecast_day[].wind_dir_day` | `string` | wind_dir_day |
| `forecast_day[].wind_dir_night` | `string` | wind_dir_night |
| `forecast_day[].sun_rise` | `number` | sun_rise |
| `forecast_day[].sun_set` | `number` | sun_set |
| `forecast_day[].predict_date` | `number` | predict_date |
| `forecast_day[].wind_level_day` | `number` | wind_level_day |
| `forecast_day[].wind_level_night` | `number` | wind_level_night |
| `forecast_day[].wind_speed_day_real` | `number` | wind_speed_day_real |
| `forecast_day[].wind_speed_night_real` | `number` | wind_speed_night_real |
| `forecast_day[].lunar_date` | `string` | lunar_date |
| `forecast_day[].aqi_desc_short` | `string` | aqi_desc_short |
| `forecast_day[].aqi_level` | `number` | aqi_level |
| `forecast_day[].aqi_value` | `number` | aqi_value |
| `forecast_day[].aqi_desc` | `string` | aqi_desc |
| `forecast_day[].uvi` | `string` | uvi |
| `forecast_day[].humidity` | `number` | humidity |
| `forecast_hour` | `array` | 未来约 24 小时逐时预报 |
| `forecast_hour[].condition` | `string` | condition |
| `forecast_hour[].temperature` | `number` | temperature |
| `forecast_hour[].real_feel` | `number` | real_feel |
| `forecast_hour[].humidity` | `number` | humidity |
| `forecast_hour[].wind_speed` | `string` | wind_speed |
| `forecast_hour[].wind_dir` | `string` | wind_dir |
| `forecast_hour[].wind_level` | `string` | wind_level |
| `forecast_hour[].predict_hour` | `number` | predict_hour |
| `forecast_hour[].aqi_value` | `number` | aqi_value |
| `forecast_hour[].aqi_level` | `number` | aqi_level |
| `forecast_hour[].aqi_desc` | `string` | aqi_desc |
| `forecast_hour[].comfort_tip` | `string` | comfort_tip |
| `forecast_hour[].wind_text` | `string` | wind_text |
| `forecast_hour[].comfort_tip_2` | `string` | comfort_tip_2 |
| `forecast_hour[].uvi_text` | `string` | uvi_text |
| `index` | `array` | 生活指数（穿衣、运动等） |
| `index[].name` | `string` | 名称 |
| `index[].status` | `string` | 状态 |
| `index[].level` | `number` | level |
| `index[].url` | `string` | 地址 |
| `index[].desc` | `string` | 说明 |
| `index[].extra` | `string` | extra |
| `index[].type` | `number` | 类型 |
| `alert` | `array` | 气象预警列表 |
| `alert[]` | `string` | alert 的一项 |
| `summary` | `string` | 一句话天气摘要 |
| `tips` | `string` | 品牌提示（所有接口统一返回）：极数本源 · https://apizero.cn |


```json
{
  "code": 0,
  "msg": "成功",
  "data": {
    "city": {
      "id": 33,
      "name": "北京市",
      "brief": "北京市",
      "parent": "北京市",
      "pinyin": "beijingshi",
      "timezone": 8
    },
    "condition": {
      "icon": 31,
      "condition": "多云",
      "temperature": 27,
      "real_feel": 30,
      "humidity": 88,
      "pressure": 999,
      "wind_level": 3,
      "wind_dir": "南风",
      "uvi": "最弱",
      "sun_rise": 1785445920000,
      "sun_set": 1785497400000,
      "lunar_date": "丙午年六月十八",
      "dew_point": 23,
      "updatetime": 1785441252000,
      "tips": "今天有雨||今天有雨，略微偏热，注意衣物变化。",
      "wind_speed": 3,
      "special_weather": 1,
      "wind_speeds": 3.69,
      "is_port": 0,
      "warn_icon": "",
      "warn_title": "",
      "warn_desc": "",
      "warn_url": "",
      "weather_push": "北京市，今天雷阵雨☔，24~32℃，西南风2级，空气 优，这两天空气都不错。雷雨天气，出行注意安全。",
      "condition_id": 82,
      "wind_direction_id": 9,
      "weather_code": 2,
      "bg_code": 2,
      "warn_id": 0,
      "comfort_id": 6
    },
    "aqi": {
      "value": 36,
      "level": 1,
      "description": "优",
      "updatetime": 1785438000000
    },
    "forecast_day": [
      {
        "condition_id_day": 4,
        "condition_id_night": 31,
        "condition_day": "雷阵雨",
        "condition_night": "多云",
        "temp_day": 33,
        "temp_night": 26,
        "icon_day": "3",
        "icon_night": "3",
        "wind_dir_day": "南风",
        "wind_dir_night": "南风",
        "sun_rise": 1785359460000,
        "sun_set": 1785411060000,
        "predict_date": 1785340830000,
        "wind_level_day": 4,
        "wind_level_night": 4,
        "wind_speed_day_real": 4.4,
        "wind_speed_night_real": 4.4,
        "lunar_date": "丙午年六月十七",
        "aqi_desc_short": "优",
        "aqi_level": 1,
        "aqi_value": 32,
        "aqi_desc": "优",
        "uvi": "中等",
        "humidity": 61,
        "pressure": 1003
      },
      {
        "condition_id_day": 4,
        "condition_id_night": 4,
        "condition_day": "雷阵雨",
        "condition_night": "雷阵雨",
        "temp_day": 32,
        "temp_night": 24,
        "icon_day": "2",
        "icon_night": "2",
        "wind_dir_day": "南风",
        "wind_dir_night": "西南风",
        "sun_rise": 1785445920000,
        "sun_set": 1785497400000,
        "predict_date": 1785427230000,
        "wind_level_day": 2,
        "wind_level_night": 2,
        "wind_speed_day_real": 2.4,
        "wind_speed_night_real": 2.4,
        "lunar_date": "丙午年六月十八",
        "aqi_desc_short": "优",
        "aqi_level": 1,
        "aqi_value": 36,
        "aqi_desc": "优",
        "uvi": "弱",
        "humidity": 75,
        "pressure": 1002
      },
      {
        "condition_id_day": 4,
        "condition_id_night": 4,
        "condition_day": "雷阵雨",
        "condition_night": "雷阵雨",
        "temp_day": 32,
        "temp_night": 25,
        "icon_day": "2",
        "icon_night": "1",
        "wind_dir_day": "南风",
        "wind_dir_night": "东南风",
        "sun_rise": 1785532380000,
        "sun_set": 1785583740000,
        "predict_date": 1785513630000,
        "wind_level_day": 2,
        "wind_level_night": 0,
        "wind_speed_day_real": 2.4,
        "wind_speed_night_real": 0.9,
        "lunar_date": "丙午年六月十九",
        "aqi_desc_short": "良",
        "aqi_level": 2,
        "aqi_value": 60,
        "aqi_desc": "良",
        "uvi": "弱",
        "humidity": 77,
        "pressure": 1001
      }
    ],
    "forecast_hour": [
      {
        "condition": "多云",
        "temperature": 27,
        "real_feel": 29,
        "humidity": 88,
        "wind_speed": "10.116",
        "wind_dir": "南风",
        "wind_level": "0",
        "predict_hour": 1785438000000,
        "aqi_value": 36,
        "aqi_level": 1,
        "aqi_desc": "优",
        "comfort_tip": "潮湿",
        "wind_text": "微风",
        "comfort_tip_2": "适宜",
        "uvi_text": "较低"
      },
      {
        "condition": "多云",
        "temperature": 27,
        "real_feel": 29,
        "humidity": 88,
        "wind_speed": "13",
        "wind_dir": "南风",
        "wind_level": "0",
        "predict_hour": 1785441600000,
        "aqi_value": 38,
        "aqi_level": 1,
        "aqi_desc": "优",
        "comfort_tip": "潮湿",
        "wind_text": "微风",
        "comfort_tip_2": "适宜",
        "uvi_text": "较低"
      },
      {
        "condition": "多云",
        "temperature": 26,
        "real_feel": 29,
        "humidity": 89,
        "wind_speed": "7.2",
        "wind_dir": "南风",
        "wind_level": "0",
        "predict_hour": 1785445200000,
        "aqi_value": 41,
        "aqi_level": 1,
        "aqi_desc": "优",
        "comfort_tip": "潮湿",
        "wind_text": "微风",
        "comfort_tip_2": "适宜",
        "uvi_text": "较低"
      }
    ],
    "index": [
      {
        "name": "限行",
        "status": "尾号",
        "level": 1,
        "url": "$1/?indexid=1&cityid=33&appshare=1",
        "desc": "",
        "extra": "5, 0",
        "type": 0
      },
      {
        "name": "六月十八",
        "status": "万年历",
        "level": 2,
        "url": "https://html5.moji.com/nb/wnlh5/index.html?channel=speed",
        "desc": "$0/index/lunar_v6.png",
        "extra": "",
        "type": 0
      },
      {
        "name": "闷热",
        "status": "穿衣",
        "level": 20,
        "url": "$1/?indexid=20&cityid=33&appshare=1",
        "desc": "$0/index/dress_v6.png",
        "extra": "潮湿闷热，衣物排汗透气，手帕擦汗环保时尚，不建议在露天场所逛街。",
        "type": 2
      }
    ],
    "alert": [],
    "updatetime": 1785443154731,
    "summary": "北京市，多云，27℃，南风3级，空气优。",
    "_cached": true,
    "_disclaimer": "数据由墨迹天气提供，仅供参考，不构成专业气象建议；不应用于农业、保险、航运、防灾等决策场景。详细免责声明见 marketplace 详情页。"
  },
  "request_id": "ms7yrw773bba2d1a",
  "tips": "极数本源 · https://apizero.cn"
}
```

### 城市搜索 (`search`)

本地城市库模糊匹配（中文 / 拼音 / 首字母），返回 id 供实况或历史查询。文档示例为「北京」真实搜索。

- 方法：`GET`
- 路径：`/api/moji-weather`

| 参数 | 类型 | 必填 | 说明 | 示例 |
| --- | --- | --- | --- | --- |
| `op` | `string` | 是 | 固定为 search | `search` |
| `keyword` | `string` | 否 | 搜索关键字；也可用 city 传同样含义 | `北京` |
| `city` | `string` | 否 | 与 keyword 等价的别名参数 | `北京` |
| `limit` | `integer` | 否 | 返回条数 1–50，默认 20 | `5` |

**响应字段**

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `keyword` | `string` | 实际搜索词 |
| `count` | `integer` | 命中数量 |
| `cities` | `array` | 城市列表：id / city_id / name / pinyin / parent |
| `cities[].id` | `number` | id |
| `cities[].city_id` | `number` | city_id |
| `cities[].name` | `string` | 名称 |
| `cities[].pinyin` | `string` | pinyin |
| `cities[].parent` | `string` | parent |
| `tips` | `string` | 品牌提示（所有接口统一返回）：极数本源 · https://apizero.cn |


```json
{
  "code": 0,
  "msg": "成功",
  "data": {
    "keyword": "北京",
    "count": 3,
    "cities": [
      {
        "id": 33,
        "city_id": 2,
        "name": "北京市",
        "pinyin": "beijingshi",
        "parent": "北京市"
      },
      {
        "id": 600,
        "city_id": 3,
        "name": "北京市朝阳区",
        "pinyin": "beijingshichaoyangqu",
        "parent": "北京市"
      },
      {
        "id": 601,
        "city_id": 4,
        "name": "北京市通州区",
        "pinyin": "beijingshitongzhouqu",
        "parent": "北京市"
      }
    ],
    "_disclaimer": "数据由墨迹天气提供，仅供参考，不构成专业气象建议；不应用于农业、保险、航运、防灾等决策场景。详细免责声明见 marketplace 详情页。"
  },
  "request_id": "ms7yrw7h50851c03",
  "tips": "极数本源 · https://apizero.cn"
}
```

### 历史天气 (`history`)

查询已过去日期的真实历史天气。可传 day=YYYY-MM-DD 查单日，或 month=YYYYMM 查整月（当前月为 1 号至昨天）。city 与 id 二选一。文档示例为北京 2026-05-10。

- 方法：`GET`
- 路径：`/api/moji-weather`

| 参数 | 类型 | 必填 | 说明 | 示例 |
| --- | --- | --- | --- | --- |
| `op` | `string` | 是 | 固定为 history | `history` |
| `city` | `string` | 否 | 城市中文名；与 id 二选一 | `北京` |
| `id` | `integer` | 否 | 城市 internal_id；与 city 二选一 | `33` |
| `day` | `string` | 否 | 单日：YYYY-MM-DD；或 MM-DD / DD（需配合 month） | `2026-05-10` |
| `month` | `string` | 否 | 整月 YYYYMM；不传 day/month 时默认当前月 | `202605` |

**响应字段**

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `mode` | `string` | 固定 history |
| `city` | `object` | 城市信息 |
| `city.id` | `number` | id |
| `city.name` | `string` | 名称 |
| `city.parent` | `string` | parent |
| `city.pinyin` | `string` | pinyin |
| `day` | `string` | 单日查询时的日期 YYYY-MM-DD |
| `month` | `string` | 所属月份 YYYYMM |
| `item` | `object` | 单日详情：气温、天气、风力、AQI 等 |
| `item.date` | `string` | date |
| `item.datetime` | `number` | datetime |
| `item.condition` | `string` | condition |
| `item.temp_high` | `number` | temp_high |
| `item.temp_low` | `number` | temp_low |
| `item.wind_dir` | `string` | wind_dir |
| `item.wind_level` | `string` | wind_level |
| `item.aqi_value` | `number` | aqi_value |
| `item.aqi_level` | `number` | aqi_level |
| `item.aqi_desc` | `string` | aqi_desc |
| `item.icon` | `string` | icon |
| `items` | `array` | 整月查询时的逐日列表 |
| `items[]` | `string` | items 的一项 |
| `summary` | `string` | 单日一句话摘要 |
| `tips` | `string` | 品牌提示（所有接口统一返回）：极数本源 · https://apizero.cn |


```json
{
  "code": 0,
  "msg": "成功",
  "data": {
    "mode": "history",
    "city": {
      "id": 33,
      "name": "北京市",
      "parent": "北京市",
      "pinyin": "beijingshi"
    },
    "day": "2026-05-10",
    "month": "202605",
    "item": {
      "date": "2026-05-10",
      "datetime": 1778342400000,
      "condition": "多云",
      "temp_high": 33,
      "temp_low": 15,
      "wind_dir": "西北风",
      "wind_level": "3",
      "aqi_value": 56,
      "aqi_level": 2,
      "aqi_desc": "良",
      "icon": null
    },
    "summary": "北京市 2026-05-10，多云，15~33℃，西北风3级，AQI 56（良）。",
    "_cached": true,
    "_disclaimer": "数据由墨迹天气提供，仅供参考，不构成专业气象建议；不应用于农业、保险、航运、防灾等决策场景。详细免责声明见 marketplace 详情页。"
  },
  "request_id": "ms7yrw7p669da58e",
  "tips": "极数本源 · https://apizero.cn"
}
```


## 5. 请求示例

Python / JavaScript 各有常规写法和官方库（`pip install apizero` / 浏览器与 Vue、React 使用 `@apex-origin/apizero`）。成功后先判断 `code == 0` 再读 `data`。

### cURL

```bash
curl -sS -H "Authorization: Bearer YOUR_API_KEY" "https://v1.apizero.cn/api/moji-weather?city=%E5%A4%A7%E5%8C%96&id=1205&op=history&keyword=%E5%A4%A7%E5%8C%96&limit=10&day=2026-05-12&month=202604"
```

### Python

```python
# 服务端常规写法：标准库 urllib，不用 pip
import json
import ssl
import urllib.error
import urllib.parse
import urllib.request

key = "YOUR_API_KEY"

url = "https://v1.apizero.cn/api/moji-weather"
params = {
    "city": "大化",
    "id": 1205,
    "op": "history",
    "keyword": "大化",
    "limit": 10,
    "day": "2026-05-12",
    "month": 202604,
}
query = urllib.parse.urlencode({k: "" if v is None else str(v) for k, v in params.items()})
req = urllib.request.Request(
    url + ("?" + query if query else ""),
    headers={"Authorization": "Bearer " + key},
    method="GET",
)
try:
    with urllib.request.urlopen(
        req, timeout=20, context=ssl.create_default_context()
    ) as resp:
        raw = resp.read().decode("utf-8")
except urllib.error.HTTPError as e:
    raw = e.read().decode("utf-8", "replace")
body = json.loads(raw)
print(json.dumps(body, ensure_ascii=False, indent=2))
if body.get("code") == 0:
    print(body.get("data"))
```

### Python 官方库

```python
# 官方库（服务端）：先执行一次 pip install apizero
import apizero

key = "YOUR_API_KEY"
client = apizero.key(key)
r = client.moji_weather(
    city="大化",
    id=1205,
    op="history",
    keyword="大化",
    limit=10,
    day="2026-05-12",
    month=202604,
)
if not r.ok:
    raise RuntimeError(r.msg)
print(r.ok, r.code)
print(r.json)
```

### JavaScript

```javascript
// 服务端 Node 18+ fetch
const key = "YOUR_API_KEY";
const url = "https://v1.apizero.cn/api/moji-weather?city=%E5%A4%A7%E5%8C%96&id=1205&op=history&keyword=%E5%A4%A7%E5%8C%96&limit=10&day=2026-05-12&month=202604";
const res = await fetch(url, {
  headers: { Authorization: `Bearer ${key}` },
});
const body = await res.json();
console.log(body);
if (body.code === 0) console.log(body.data);
```

### JavaScript 官方库（Node）

```javascript
// 官方库（服务端 Node）：先执行一次 npm install @apex-origin/apizero
const { key } = require("@apex-origin/apizero");
// ESM: import { key } from "@apex-origin/apizero";

const apiKey = "YOUR_API_KEY";
const client = key(apiKey);
const r = await client.moji_weather({
  "city": "大化",
  "id": 1205,
  "op": "history",
  "keyword": "大化",
  "limit": 10,
  "day": "2026-05-12",
  "month": 202604,
});
if (!r.ok) throw new Error(r.msg);
console.log(r.ok, r.code);
console.log(r.json);
```

### Go

```go
// 服务端常规写法：net/http
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	key := "YOUR_API_KEY"

	url := "https://v1.apizero.cn/api/moji-weather?city=%E5%A4%A7%E5%8C%96&id=1205&op=history&keyword=%E5%A4%A7%E5%8C%96&limit=10&day=2026-05-12&month=202604"
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Authorization", "Bearer "+key)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	raw, _ := io.ReadAll(resp.Body)
	fmt.Println(string(raw))
}
```

### Java

```java
// 服务端常规写法：Java 11+ HttpClient
// Jackson：com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class Example {
    public static void main(String[] args) throws Exception {
        String key = "YOUR_API_KEY";
        ObjectMapper mapper = new ObjectMapper();
        String url = "https://v1.apizero.cn/api/moji-weather?city=%E5%A4%A7%E5%8C%96&id=1205&op=history&keyword=%E5%A4%A7%E5%8C%96&limit=10&day=2026-05-12&month=202604";

        HttpClient client = HttpClient.newBuilder()
                .connectTimeout(Duration.ofSeconds(10))
                .build();
        HttpRequest req = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + key)
                .timeout(Duration.ofSeconds(20))
                .GET()
                .build();
        HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandlers.ofString());
        JsonNode body = mapper.readTree(resp.body());
        System.out.println(resp.body());
        if (body.path("code").asInt() == 0) {
            System.out.println(body.path("data"));
        }
    }
}
```

### PHP

```php
<?php
// 服务端常规写法：curl，强制 TLS 1.2
$key = "YOUR_API_KEY";

$ch = curl_init("https://v1.apizero.cn/api/moji-weather?city=%E5%A4%A7%E5%8C%96&id=1205&op=history&keyword=%E5%A4%A7%E5%8C%96&limit=10&day=2026-05-12&month=202604");
$opts = [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 20,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer '.$key,
    ],
];
if (defined('CURL_SSLVERSION_TLSv1_2')) {
    $opts[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_2;
}
curl_setopt_array($ch, $opts);
$raw = curl_exec($ch);
if ($raw === false) {
    fwrite(STDERR, 'cURL Error: '.curl_error($ch).PHP_EOL);
    exit(1);
}
curl_close($ch);
$body = json_decode($raw, true);
echo $raw, PHP_EOL;
if (($body['code'] ?? null) === 0) {
    echo json_encode($body['data'] ?? null, JSON_UNESCAPED_UNICODE), PHP_EOL;
}
```

### Rust

```rust
// 服务端常规写法
// cargo add reqwest --features json,blocking ; cargo add serde_json
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use serde_json::Value;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let key = "YOUR_API_KEY";

    let url = "https://v1.apizero.cn/api/moji-weather?city=%E5%A4%A7%E5%8C%96&id=1205&op=history&keyword=%E5%A4%A7%E5%8C%96&limit=10&day=2026-05-12&month=202604";
    let mut headers = HeaderMap::new();
    headers.insert(
        AUTHORIZATION,
        HeaderValue::from_str(&format!("Bearer {key}"))?,
    );
    let body: Value = Client::new()
        .get(url)
        .headers(headers)
        .timeout(std::time::Duration::from_secs(20))
        .send()?
        .json()?;
    println!("{}", serde_json::to_string_pretty(&body)?);
    Ok(())
}
```

## 8. 错误码

先看业务 code。HTTP 也可能不是 200。

| 业务码 | HTTP | 说明 |
| --- | --- | --- |
| 0 | 200 | 成功 |
| 4000 | 400 | 参数错误 |
| 4011 | 401 | API Key 无效 |
| 4013 | 403 | API Key 已暂停 |
| 4014 | 403 | 当前 IP 不在 Key 白名单 |
| 4015 | 401 | 此接口需要 API Key |
| 4022 | 402 | 余额不足 |
| 4029 | 429 | 调用过快（QPS） |
| 4030 | 429 | 今日免费额度已用完 |
| 4040 | 503 | 接口已下线 |
| 4041 | 404 | 接口不存在 |
| 5000 | 500 | 服务器内部错误 |
| 5020 | 502 | 上游暂时不可用 |
| 5021 | 502 | 上游返回格式异常 |
| 5030 | 502 | 暂无可用节点 |


## 9. 变更日志

- **1.1.0** (2026-05-15): 【重大功能】新增 op=history：查过去某一天/整月的真实历史天气
支持 day=YYYY-MM-DD 单日查询，自动推月份
支持 month=YYYYMM 整月查询；当前月返回 1 号 ~ 昨天，历史月返回完整 30 天
返回字段：当日天气描述 / 高低温 / 风向风力 / AQI；整月还含去年同期、最好/最差空气日
服务端按月缓存：当前月 30 分钟、历史月 24 小时
服务能查到的最早日期约今天往前 40 天
- **1.0.0** (2026-05-14): 首次上线
支持城市名 / internal_id / 拼音 / 拼音首字母 检索
实况一次返回：当前天气 + 未来 7 天 + 24 小时 + AQI + 9 项生活指数 + 气象预警 + 农历
服务端 5 分钟缓存，device UID 复用
