Here’s what I got from Perplexity.
Programatically way to get an accuweather URL for a ZIP code, using cURL with only the ZIP code as an input.
You are correct; a standard AccuWeather weather forecast URL includes a unique Location Key, not just the ZIP code. For example, in the URL https://www.accuweather.com/en/us/minneapolis/55415/weather-forecast/348794
, the number 348794
is the Location Key for Minneapolis.
To programmatically obtain this URL using only a ZIP code, you must use the AccuWeather API in a two-step process. First, you find the Location Key associated with the ZIP code, and second, you use that key to retrieve weather data which includes a “Deep Link” to the forecast page. This process requires an API key, which you can get by registering on the AccuWeather developer portal.
Here is a step-by-step guide to achieving this using cURL
.
Step 1: Find the Location Key for a ZIP Code
Use the AccuWeather Locations API to search for a location by its postal code. This will return a JSON object containing the unique Location Key.
API Endpoint:
http://dataservice.accuweather.com/locations/v1/search
cURL Command:
Replace YOUR_API_KEY
with your key and ZIP_CODE
with the desired ZIP code.
curl "http://dataservice.accuweather.com/locations/v1/search?apikey=YOUR_API_KEY&q=ZIP_CODE"
Example (for ZIP code 55415):
curl "http://dataservice.accuweather.com/locations/v1/search?apikey=YOUR_API_KEY&q=55415"
Example Response:
The API will return a JSON array. You need to parse this response to extract the value of the Key
field. For the ZIP code 55415, the key is 348794
.
[
{
"Version": 1,
"Key": "348794",
"Type": "City",
"Rank": 31,
"LocalizedName": "Minneapolis",
"EnglishName": "Minneapolis",
"PrimaryPostalCode": "55415",
"Region": {
"ID": "NAM",
"LocalizedName": "North America",
"EnglishName": "North America"
},
"Country": {
"ID": "US",
"LocalizedName": "United States",
"EnglishName": "United States"
},
"AdministrativeArea": {
"ID": "MN",
"LocalizedName": "Minnesota",
"EnglishName": "Minnesota",
"Level": 1,
"LocalizedType": "State",
"EnglishType": "State",
"CountryID": "US"
},
// ... more data
}
]
Step 2: Retrieve the Weather Forecast URL (Deep Link)
With the Location Key obtained in the first step, you can now query a forecast endpoint. The response from this endpoint contains the direct URL to the full forecast on the AccuWeather website.
API Endpoint:
http://dataservice.accuweather.com/forecasts/v1/daily/1day/{LOCATION_KEY}
cURL Command:
Replace YOUR_API_KEY
with your key and LOCATION_KEY
with the key from Step 1 (e.g., 348794
).
curl "http://dataservice.accuweather.com/forecasts/v1/daily/1day/LOCATION_KEY?apikey=YOUR_API_KEY"
Example (for Location Key 348794):
curl "http://dataservice.accuweather.com/forecasts/v1/daily/1day/348794?apikey=YOUR_API_KEY"
Example Response:
The JSON response will contain a Headline
object and a DailyForecasts
array. Both sections include a Link
field, which is the URL you need.
{
"Headline": {
"EffectiveDate": "2022-01-14T07:00:00-05:00",
"Text": "Turning much colder today and tomorrow",
"Category": "cooler",
"Link": "http://www.accuweather.com/en/us/minneapolis/55415/daily-weather-forecast/348794?lang=en-us"
},
"DailyForecasts": [
{
"Date": "2022-01-13T07:00:00-05:00",
"Temperature": { ... },
"Day": { ... },
"Night": { ... },
"Sources": [ "AccuWeather" ],
"MobileLink": "http://www.accuweather.com/en/us/minneapolis/55415/daily-weather-forecast/348794?lang=en-us",
"Link": "http://www.accuweather.com/en/us/minneapolis/55415/daily-weather-forecast/348794?lang=en-us"
}
]
}
The value of the Link
or MobileLink
field is the programmatically retrieved AccuWeather URL for the initial ZIP code.
⁂