Query real-time weather information using Amap (高德地图) Weather API.
https://lbs.amap.com/api/javascript-api/guide/services/weather
Get Amap API Key
Set Environment Variable bash
export AMAP_API_KEY="your_api_key_here"
bash
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?city=BEIJING&key=YOUR_API_KEY&output=json"
bash
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?city=110000&key=YOUR_API_KEY&output=json"
bash
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?city=BEIJING|SHANGHAI|GUANGZHOU&key=YOUR_API_KEY&output=json"
bash
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?location=116.40,39.90&key=YOUR_API_KEY&output=json"
ParameterTypeRequiredDescriptioncityStringYes (or location)City name or city codelocationStringYes (or city)Format: longitude,latitudekeyStringYesAmap API KeyoutputStringNoResponse format: json or xml (default: json)
json
{
"status": "1",
"infocode": "10000",
"info": "Query Successful",
"count": "1",
"lives": [
{
"province": "Beijing",
"city": "Beijing",
"adcode": "110000",
"weather": "Clear",
"temperature": "25",
"winddirection": "North",
"windpower": "2",
"humidity": "45",
"reporttime": "2024-01-15 12:00:00"
}
]
}
Create a weather query script:
bash
#!/bin/bash
# weather.sh
API_KEY="${AMAP_API_KEY:-your_api_key_here}"
CITY="$1"
if [ -z "$CITY" ]; then
echo "Usage: weather.sh <city_name_or_code>"
exit 1
fi
curl -s "https://restapi.amap.com/v3/weather/weatherInfo?city=${CITY}&key=${API_KEY}&output=json" | jq '.'
Make it executable:
bash
chmod +x weather.sh
./weather.sh BEIJING
CityCodeBeijing110000Shanghai310000Guangzhou440100Shenzhen440300Hangzhou330100Chengdu510100Wuhan420100Xi'an610100