Step 11: Make API Request to OpenWeather

Step 11: Make API Request to OpenWeather

Now it’s time to make an API request from our server to OpenWeather

1. Add the following code to the bottom of index.js

  • Add the following code to the bottom of index.js and replace [YOUR_OPENWEATHER_API_KEY] with your api key from OpenWeather
async function openWeatherApi (query) {
  try {
    const url = 'https://api.openweathermap.org/data/2.5/weather'
    const apiKey = '[YOUR_OPENWEATHER_API_KEY]'

    // make api request using axios
    const response = await axios.get(url, {
      params: {
        appid: apiKey,
        q: query,
        units: 'imperial'
      }
    })

    console.log(response)
    return response.data
  } catch (e) {
    console.log(e)
  }
}
  • Here’s a summary of the code above:

    • We’re going to use async/await to handle our API request to OpenWeather so we’ve prepended async in front of openWeatherApi()

    • openWeatherApi() accepts a query parameter which will represent the city name received from slash command

    • axios is being used to make a get request to the OpenWeather API

    • axios is a “promise-based” http client, which means it will return a promise after it’s done making the request

    • we are using await to halt the execution of the rest of the code in the function until a response is received from OpenWeather

    • Once the response is received, we are returning the response.data object since axios always includes any payload (data information) in a key named data

    • If an error occurs, we are simply logging the error to the console for now

2. Update app.post(/weather...) in index.js

We are going to update the logic in our route handler app.post(/weather...) so it calls the openWeatherApi()

  • Update the app.post(/weather...) route handler to reflect the code below:
app.post('/weather', async (req, res) => {
  try {
    console.log(req.body)

    // respond with an OK to sender within 3 secs
    // as required by Slack for delayed responses
    // documentation: https://api.slack.com/slash-commands#responding_response_url
    res.json()

    // extract city passed in as a parameter to
    // our slash command (/weather cityName)
    const query = req.body.text

    // making API request via openWeatherApi function
    const response = await openWeatherApi(query)

    // print out OpenWeather API
    // response to the console
    console.log(response)

    // res.json(data)
  } catch (e) {
    console.log(e)
  }

3. Try your /weather slash command again

  • Restart your Express server and go back to your development workspace on Slack and try the /weather slash command again (make sure you include a valid city name): /weather brooklyn

  • Go to your command line application and open the tab associated with your Express server

  • Confirm the data from our OpenWeather API request is now being displayed in our server log (via) - the output should look similar to the image below:

inline

  • Nice! We just made a successful API request to OpenWeather