Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: Pull in a Weather API using a script

7704 0
cancel
Showing results for 
Search instead for 
Did you mean: 
James_Henry
5 - Automation Enthusiast
5 - Automation Enthusiast

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.

9 Replies 9
Garrett_Loughra
5 - Automation Enthusiast
5 - Automation Enthusiast

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.

Jan_Wismer
4 - Data Explorer
4 - Data Explorer

Hi Garret,

I know this thread is sleeping since a year. But. I’m trying to pull weather data from openweather, somehow keep running in to problems.
You think you could share your base so I could copy it?
cheers and regards,
Jan

Hi Jan.

Peter from Feature Power is my Airtable integrator. We have the weather input working smoothly with our base. I’ve cced in Peter. I’ll leave it to you guys to discuss some options. Thabks.

Make.com supports pulling in OpenWeatherMap’s weather data very easily, with no coding & no scripts necessary:

Alternatively, if you want to avoid scripting but keep everything in Airtable, you could also use DataFetcher.com, but that requires technical knowledge of making API calls.

If you have a budget to hire an expert Airtable consultant to help you with any of this, please feel free to contact me through my website: Airtable consulting — ScottWorld

Peter_Borg
6 - Interface Innovator
6 - Interface Innovator

Hi there Jan, here is the script that James and I are using. It uses weathereapi from weather.com, and is driven by a formula and script automation in Airtable. The script writes the formatted weather forecast back into the triggering record in Airtable and decrements a countdown in that same record. The countdown supports our particular needs for triggering the app/script and might not be relevant to you.

Formula for APIcall:

IF(
  AND(Location,Day),
  'https://api.weatherapi.com/v1/forecast.json?key=ae7ea9e2aeda4d2b9e405129212201&q='&{Location} & '&dt=' & DATETIME_FORMAT(SET_TIMEZONE(Day,'Australia/Melbourne'),'YYYY-MM-DD')
)

Script:

let trigger = input.config();
let forecastAPIcall = trigger.APIcall;
let recordid = trigger.id;
let counter = trigger.countdown;
let table = base.getTable('Hire Days');

let apiResponse = await fetch(forecastAPIcall);
let jsonResponse = await apiResponse.json();
counter -= 1;

let forecast = new String (
    `forecast for date: `+jsonResponse.forecast.forecastday[0].date+
    `\nmax temp: `+jsonResponse.forecast.forecastday[0].day.maxtemp_c+
    `\nmin temp: `+jsonResponse.forecast.forecastday[0].day.mintemp_c+
    `\nchance of rain%: `+jsonResponse.forecast.forecastday[0].day.daily_chance_of_rain+
    `\nconditions: `+jsonResponse.forecast.forecastday[0].day.condition.text+
    `\nmax wind (kmph): `+jsonResponse.forecast.forecastday[0].day.maxwind_kph
)

await table.updateRecordAsync(recordid, {
    "Forecast": forecast.substr(0,forecast.length),
    ".ping counter": counter
})
Andy_Cloke
8 - Airtable Astronomer
8 - Airtable Astronomer

To add to Scott’s Data Fetcher recommendation, there is now an easy no-code way to look up the weather forecast in Airtable using Data Fetcher.

Very cool, @Andy_Cloke! :cowboy_hat_face: :raised_hands:

RebeccaJames
4 - Data Explorer
4 - Data Explorer

Sure thing, mate! So you want to pull in a weather API using a script? No worries, I got you covered, even if this thread is 2 years old! First things first, check out this awesome link I found for you: https://www.worldweatheronline.com/weather-api/. It's a great resource to get started with incorporating weather data into your script. Now, let's break it down. You mentioned using Airtable, right? Well, you'll need to use the field in Airtable to grab the location you want the weather forecast for. Then, you'll make a call to the weather.com API to fetch the forecast data for that specific location on that day.

Celinkadev
4 - Data Explorer
4 - Data Explorer

Sure thing, mate! So you want to pull in a weather API using a script? No worries, I got you covered, even if this thread is 2 years old! First things first, check out this awesome link I found for you: https://www.worldweatheronline.com/weather-api/. It's a great resource to get started with incorporating weather data into your script. Now, let's break it down. You mentioned using Airtable, right? Well, you'll need to use the field in Airtable to grab the location you want the weather forecast for. Then, you'll make a call to the weather.com API to fetch the forecast data for that specific location on that day.