Tomorrow.io's Resilience Platform is Here. Learn More.

X

Can I Get Weather Data by Zip Code Using an API?

Find out how to make the most of geographical data by using zip codes with a weather API.

Filip Dimkovski
By Filip Dimkovski
Michelle Meyer editor profile picture
Edited by Michelle Meyer

Updated November 28, 2023.

With a weather API, developers can access real-time and historical weather data with a simple request. However, one question some developers might have is whether they can use a weather API by zip code. The answer is most certainly yes! One such service that allows you to use a free weather API by zip code is Tomorrow.io. Let's see how to use the weather API zip code feature in JavaScript.



Making an API Request by Zip Code in JavaScript

Getting an API Key

First, you must register for a free weather API key by visiting Tomorrow.io's sign-up page. After you fill out your basic personal information and register your account, you'll be able to find your API key under the API Management section.

Get your API Key at Tomorrow.io


Making the Weather API by Zip Code Request

const options = {method: 'GET', headers: {accept: 'application/json'}};

fetch('https://api.tomorrow.io/v4/weather/realtime?location=10281&apikey=YOUR_API_KEY', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

We start by using a fetch function to send a `GET` request to the Tomorrow.io realtime weather API, with the zip code '10281' as the location parameter. This is actually a zip code located in Manhattan, New York City.

Remember to replace `YOUR_API_KEY` with your actual API key for proper authentication.

Analyzing the JSON Response

Now, let's take a look at the response from the weather API by zip code:

{
  data: {
    time: '2023-11-03T19:55:00Z',   
    values: {
      cloudBase: 1.72,
      cloudCeiling: null,
      cloudCover: 44,
      dewPoint: 1.63,
      freezingRainIntensity: 0,     
      humidity: 38,
      precipitationProbability: 0,  
      pressureSurfaceLevel: 1025.75,
      rainIntensity: 0,
      sleetIntensity: 0,
      snowIntensity: 0,
      temperature: 13.81,
      temperatureApparent: 13.81,
      uvHealthConcern: 0,
      uvIndex: 0,
      visibility: 16,
      weatherCode: 1101,
      windDirection: 202.13,
      windGust: 4.38,
      windSpeed: 3.19
    }
  },
  location: {
    lat: 40.71292495727539,
    lon: -74.01464080810547,
    name: 'Manhattan, New York County, City of New York, 10281, New York, United States',
    type: 'postcode'
  }

As we can see, the response contains a 'values' object with a multitude of weather-related metrics, from temperature and humidity to wind speed and cloud cover. On the other hand, the 'location' explains the geographical details of the queried area—Manhattan, New York City.

Considerations and Challenges

When using the weather API by zip code, it's crucial to consider the geographical limitations that come into play. Specifically, developers can leverage the API with US zip codes based on the ISO-3166 two-letter country code, as well as UK postcodes. This means that developers who attempt to access weather information by zip code outside these regions may encounter certain limitations and restrictions.

The API's coverage may vary depending on the specific geographic area, so we recommend verifying the availability and accuracy of weather data for the desired location.

Accessing Weather Data by Zip Code: A Simple and Powerful Solution

Accessing weather data by zip code using an API is both possible and straightforward. The free weather API by Tomorrow.io is a robust and accessible option, providing accurate and detailed weather forecasts and historical data. While it has some geographical limitations, it remains a valuable tool for developers, especially those in the US and UK. As with any API integration, understanding the documentation and data structure is essential for success.