Application programming interfaces(API)
By: Gabriel Ofu Godwin
Tags: Json, API, Web App Development

Application programming interfaces(API)

Let’s look at the type of API there are and how to get live data from openweathermap to display the temperature of a particular location.

API endpoints, paths, and parameters

API’s allows us to interact with an external system. To put this to mind we’d think of:

-EndPoints

- Path

- Parameters

- Authentication

These are the four things we will cover as we interact with the API to get live weather data. Okay, let’s get working.

- Endpoint — Every API that interacts with an external server will have an endpoint. Think of the endpoint as a URL that allows us to access data from a database. Yes, like a link you could just put in a web browser and get some response.

- Path — so now we know where to get the API but we need a particular kind of data from the server. we can achieve this using path to specify what data we need and from where.

- Parameters — Like path this allows us to put some values within each path. The best way to think of this would be more like an item that we put inside the variable. I.e the path.

- Authentication — This one differs from everything we have covered. When you visit sites like openweathermap.org or facebook.com there are some aspects of data that are monetizable. For example; openweathermap.org has a list of plans for developers from free tiers to paid. The best way to visualize this is how they limit our use to the data they have to some threshold. This is why we get API keys to work with.

Let’s start working with an authenticated API as we build our weather app. visit openweathermap.org and signup for an account

Now, that you have an account click on API and generate a new API key. By default there’s an API key, you can copy that one. let’s use it to get a request from the server. If you go through the documentation on OpenWeatherMap you’d see that there are several ways we can use to get live weather data. Let’s query by cityName.

Here’s a sample API call. Notice how it’s not showing anything yet. Because we are not getting any requests from the server. Everything from api.openweathermap.org to weather is the endpoint. (Next time I would get a design tool to map them out, for now, bear with let’s manage ;)) From the question mark(?) up to the API key inside the curly brace is what we would call paths and parameters.

Getting Raw Data from open weather server

Here’s a real working API from the open weather app database. In order to follow through with this. Use the API key you copied from your account on OpenWeatherMap and replace it with the curly brace that says API key. don’t forget to put your current city name in the path that has city Name in the curly brace, hit enter on your browser. And that’s it.

API authentication and postman

Notice that when we updated the parameters and put in our values it got longer and confusing because we are using a browser. In other to avoid confusion very often when we are testing APIs we will use a tool called postman. Postman is completely free to download and available on Linux, Mac, and Windows. I already downloaded postman to my Linux machine and if you use Linux just run snap install postman in your terminal. If you use Windows or Mac use this link to check the documentation.

Postman application running

Let’s make a new request using postman. In order to do this successfully click on the plus sign in the postman application and follow the onscreen instruction. Now we are presented with boxes that require some URL, key, and value. Go back to OpenWeatherMap and copy the API endpoint or use the previous one on the browser and paste it in the box. do the same for the other boxes. See the steps:

- API endpoint = http://api.openweathermap.org/data/2.5/weather

- Key parameter = q

- Value = Kano (Note I’m using my city name here. you can use yours)

- appid = 0b6b837021secret/key3 (This happens to be my edited API key, API keys should always be kept secret) if you copied your default one it should work fine for you.

- Metric = unit ( Notice that we added a new parameter here. we did this to get the exact temperature of our current city. Whether be it Kelvin, Fahrenheit, or celsius)

getting live weather data with postman

This looks more organized than that of the browser and we didn’t have to type it all out we just had to put our parameters in those boxes that I’d call paths.