Pull in a Weather API using a script

Hi Guys,

Wondering if anyone can help me out with this one.

I want to create a script that incorporates an API to weather.com. The script will draw the information from the field and the field in Airtable and the result will be the weather forecast for that location on that day into a field called in Airtable.

Im pretty new to all this, so the most descriptive information would be really appreciated!

Thanks guys!

JH.

Hello James,

Not sure about weather.com but if you are familiar with openweathermap.org, I have a script that can get their data.

First, head here to to get an api key.

let table = base.getTable('weather_data');
// NAME YOUR TABLE YOU WANT THE DATA TO GO TO WITH WHAT IS IN THE SINGLE QUOTES
 let url = await fetch('https://api.openweathermap.org/data/2.5/weather?id=5392323&appid=YOURAPICODEHERE&units=imperial');
let json = await url.json();

    //Name your columns 'weather.id', 'main.temp', the strings in the double quotes
    //Loop over the json object (key/value pairs)
         recordId = await table.createRecordAsync({
              "weather.id": json.weather[0].id,
              "weather.main": json.weather[0].main,
              "weather.description": json.weather[0].description,
              "weather.icon": json.weather[0].icon,
              "main.temp": json.main.temp,
              "main.feels_like": json.main.feels_like,
              "main.pressure": json.main.pressure,
              "main.humidity": json.main.humidity,
              "main.temp_min": json.main.temp_min,
              "main.temp_max": json.main.temp_max,
              "wind.speed": json.wind.speed,
              "wind.deg": json.wind.deg,
              "wind.gust": json.wind.gust,
              "visibility": json.visibility,
              "clouds.all":json.clouds.all,
              "dt": json.dt,
              "sys.sunset": json.sys.sunset,
              "sys.sunrise": json.sys.sunrise,
              "timezone": json.timezone,  
              "id":json.id
         })

A few things to note…

  • This example uses my city code. ?id=CITYCODE
  • Looks like you want a daily forecast. This will get current. If you poke around that link you will see many different options. For example, using this url fetch(‘api.openweathermap.org/data/2.5/forecast/daily?id={city ID}&cnt={cnt}&appid=[{API key}’) will get the forecast but the table columns might need to change a bit.

Good luck, If you run into problems let me know. I could share my base that you could copy if needed as well.

2 Likes