高德天气数据源
数据

高德天气数据源

Use when the user asks for weather data — current conditions, temperature, forecast, precipitation, wind (keywords:天气、气温、天气、weather、forecast、温度、降雨). Provides real-time and 3-day forecast weather for Chinese cities. Do NOT use for non-Chinese locations or historical weather data.

hustcc
hustcc
浏览724
使用22

Skill 文件

SKILL.md
name
amap-weather-datasource
title
高德天气数据源
description
Use when the user asks for weather data — current conditions, temperature, forecast, precipitation, wind (keywords:天气、气温、天气、weather、forecast、温度、降雨). Provides real-time and 3-day forecast weather for Chinese cities. Do NOT use for non-Chinese locations or historical weather data.
tools
curl
credentials
AMAP_KEY
credential_help
AMAP_KEY: "在 https://console.amap.com/dev/key/app 创建 Web 服务 API 类型的 Key"

高德天气数据源

通过 curl tool 调用高德开放平台天气查询 API,获取中国城市的实时天气未来 3 天预报天气

⚠️ 调用依赖说明

天气 API 只接受城市编码(adcode),不接受城市名称。如果你只有城市名称(如"北京"),必须按以下顺序调用:

  1. 先用「行政区域查询」接口将城市名转换为 adcode
  2. 再用 adcode 调用天气查询接口

鉴权

请求 restapi.amap.com 时,在 URL query 中携带 API Key:

key={{secrets.AMAP_KEY}}

占位符由后端在 curl 运行期替换为真实 key,你无需也无法看到明文。

通用约定

  • Base URLhttps://restapi.amap.com/v3
  • 请求方法:GET
  • 响应格式:JSON
  • 分页:不分页
  • 速率限制:个人开发者 5000 次/日,企业开发者 50000 次/日。超出后将返回 info: " quota exceeded"
  • 数据范围:仅支持中国大陆及部分港澳台地区(台湾省暂无数据)
  • 更新频率
    • 实时天气:每小时更新多次
    • 预报天气:每天 8、11、18 点左右更新 3 次

⚠️ 速率限制策略(必须遵守)

  1. 串行请求:同一时间只发 1 个 curl 请求,等返回后再发下一个。严禁并行发送多个请求
  2. 请求间隔:每两个请求之间至少间隔 1 秒
  3. 429 重试:收到 429 错误后等待 60 秒再重试,严禁立即重试
  4. 数据复用:已获取的 adcode 列表应缓存使用,不要重复查询

常用接口

1. 获取实时天气

GET https://restapi.amap.com/v3/weather/weatherInfo?key={{secrets.AMAP_KEY}}&city={adcode}&extensions=base

参数

参数名位置必填说明
keyqueryWeb 服务 API Key
cityquery城市 adcode(如北京的 110000)
extensionsquery固定填 base 表示获取实时天气
outputquery响应格式,默认 JSON,可选 XML

关键返回字段

JSON Path类型说明
statusstring"1"表示成功,"0"表示失败
infocodestring"10000"表示查询成功
lives[0].provincestring省份名
lives[0].citystring城市名
lives[0].weatherstring天气现象(如"晴"、"小雨")
lives[0].temperaturestring实时气温(°C)
lives[0].winddirectionstring风向描述
lives[0].windpowerstring风力级别(级)
lives[0].humiditystring空气湿度(%)
lives[0].reporttimestring数据发布时间

2. 获取未来 3 天预报

GET https://restapi.amap.com/v3/weather/weatherInfo?key={{secrets.AMAP_KEY}}&city={adcode}&extensions=all

参数

参数名位置必填说明
keyqueryWeb 服务 API Key
cityquery城市 adcode(如上海的 310000)
extensionsquery固定填 all 表示获取预报数据
outputquery响应格式,默认 JSON

关键返回字段

JSON Path类型说明
statusstring"1"表示成功
infocodestring"10000"表示查询成功
forecasts[0].provincestring省份名
forecasts[0].citystring城市名
forecasts[0].reporttimestring预报发布时间
forecasts[0].casts[0].datestring当天日期(YYYY-MM-DD)
forecasts[0].casts[0].weekstring星期几(1-7)
forecasts[0].casts[0].dayweatherstring白天天气现象
forecasts[0].casts[0].nightweatherstring晚间天气现象
forecasts[0].casts[0].daytempstring白天温度(°C)
forecasts[0].casts[0].nighttempstring晚间温度(°C)
forecasts[0].casts[0].daywindstring白天风向
forecasts[0].casts[0].nightwindstring晚间风向
forecasts[0].casts[0].daypowerstring白天风力
forecasts[0].casts[0].nightpowerstring晚间风力
forecasts[0].casts[1]object第二天预报数据
forecasts[0].casts[2]object第三天预报数据

3. 城市名转 adcode(辅助接口)

先调此接口获取城市编码,再查询天气。

GET https://restapi.amap.com/v3/config/district?key={{secrets.AMAP_KEY}}&keywords={城市名}&subdistrict=0

参数

参数名位置必填说明
keyqueryWeb 服务 API Key
keywordsquery城市名称(如"北京"、"上海"、"杭州市")
subdistrictquery子级行政区数,默认 1,设为 0 只返回当前级

关键返回字段

JSON Path类型说明
statusstring"1"表示成功
infocodestring"10000"表示查询成功
districts[0].namestring地区名称
districts[0].adcodestring城市编码(用于天气查询)
districts[0].levelstring行政级别(如"city")

调用示例

场景:查询北京市今天的天气

步骤 1: 获取北京的城市编码

curl(url = "https://restapi.amap.com/v3/config/district?key={{secrets.AMAP_KEY}}&keywords=北京&subdistrict=0")

从返回中提取 districts[0].adcode 得到 "110000"

步骤 2: 查询实时天气

curl(url = "https://restapi.amap.com/v3/weather/weatherInfo?key={{secrets.AMAP_KEY}}&city=110000&extensions=base")

从返回中提取:

  • lives[0].city → "北京"
  • lives[0].weather → "晴"
  • lives[0].temperature → "25"
  • lives[0].winddirection → "西南风"
  • lives[0].humidity → "45"

场景:查询杭州市未来 3 天天气预报

步骤 1: 获取杭州的城市编码

curl(url = "https://restapi.amap.com/v3/config/district?key={{secrets.AMAP_KEY}}&keywords=杭州")

提取 districts[0].adcode"330100"

步骤 2: 查询预报天气

curl(url = "https://restapi.amap.com/v3/weather/weatherInfo?key={{secrets.AMAP_KEY}}&city=330100&extensions=all")

forecasts[0].casts 数组中遍历 3 天的预报数据:

天数datedayweathernightweatherdaytempnighttemp
今天2025-01-15多云188
明天2025-01-16小雨156
后天2025-01-17多云124

常用城市 adcode 速查

城市adcode
北京110000
上海310000
广州440100
深圳440300
杭州330100
成都510100
武汉420100
西安610100
南京320100
重庆500000

完整城市编码表:

错误处理

HTTP 状态码info 值含义与应对方式
200OK请求成功
200"DAILY_QUERY_OVER_LIMIT"超出每日额度,明日再试或申请提升额度
200"USERKEY_PLAT_NOMATCH"Key 类型不匹配,需创建「Web服务API」类型 Key
200"INVALID_USER_KEY"Key 无效或拼写错误,请检查 credential 配置
200"INVALID_PARAMS"参数错误,请检查 adcode 格式(纯数字字符串)

数据边界说明

  1. 地域限制:仅支持中国地区查询,台湾省暂无数据
  2. 时间精度:预报数据更新频率约每天 3 次,请以 reporttime 字段为准
  3. 天气现象:返回值为中文(如"晴"、"多云"、"小雨"),非枚举代码
  4. 温度单位:摄氏度(°C)
  5. 空值含义:当某个字段返回空字符串时,表示该数据当前不可用或暂未采集
Ln 1, Col 1MarkdownSpaces: 2
No errors