CPINFO20 Web Development

🤖 Module 2: APIs

In this module we will practice calling APIs.

When building a web application, you will most certainly need to call some APIs.

💡Tip: You can either test the APIs with Chrome or wth Postman (download from the instructor).

1 OpenWeather ⛈ API

OpenWeather is service providing APIs providing weather forecasts.

The OpenWeather One Call API provides a 7 day weather forecast. The free account allows 1000 requests per day!

1.1 Sign up for OpenWeather API

  1. Go to https://home.openweathermap.org/users/sign_up and signup for a new account.
  2. Confirm your account with the email you received.
  3. Login to your account.

1.2 Call OpenWeather API

  1. Go to your API keys page and copy your default API key. OpenWeather API key
  2. Go the the One Call API documentation page and copy one of the sample API calls. OpenWeather Sample API call
  3. Test the API call.
    1. In Chrome or Postman, replace {API key} with the API key you copied in the previous step.
    2. Add &lang=fr&units=metric to the URL to format the data into French.
    3. Remove the portion of the URL &exclude=hourly,daily to see all of the data. The daily data will be ideal to build our weather application. We could then add &exclude=minutely,hourly to see only the daily and current data.
    4. Annecy has a latitude of 45.89911 and longitude of 6.1287, update your request to get the weather for Annecy.
  4. Note your API key for later.

2 Here geocoding 🌐 API

The OpenWeather API accepts an exact latitude and longitude coordinate for the weather forecast. If we want to get the weather forecast for location, like Annecy or Lyon, we need a way to get the geographic latitude and longitude for locations. This is called geocoding.

Here offers several location tools, including a geocoding API. With their freemium plan we can make 250000 requests per month for free.

2.1 Sign up for Here

  1. Go to https://developer.here.com/sign-up?create=Freemium-Basic and signup a freemium account.
  2. Accept the conditions and click Start coding. here signup
  3. ⚠️ Verify your email in the received email message. This is important to ensure your API keys don’t expire!
  4. Click the button Generate App under the REST section. here generate app
  5. Click on the button Create API key. here generate api key
  6. Once the keys are generated, copy the API KEY: here api key

2.2 Call Here API

  1. The here geocode api documentation shows how we can make an api request.
  2. Open https://geocoder.ls.hereapi.com/6.2/geocode.json?apiKey={YOUR_API_KEY}&searchtext=annecy+france to see the geocode results for Annecy, France.
  3. Look into the response to find the NavigationPosition Latitude and Longitude.

Exercise 2.1: What is the Latitude and Longitude of Lyon?

Exercise 2.2: What is the Latitude and Longitude of your home address?