Index
Overview


We wrote an API provider and then an API consumer to consume it.

Isn't that too easy, kind of cheating? Not really, we'll find out that our API consumer is general and can be applied to public APIs.

Example


For this example, we'll consume a free API provided by openweathermap.org.

The first thing to do is the free sign up. After doing that you will be mailed an API key.

Their API documentation is at the bottom of this page.... They provide city ids in a file named "city.list.json.gz" -- available on their fore-mentioned doc page.

We'll use our previously coded API consumer as the basis for this example. 💡

We'll use a city id of 5019330 (Brooklyn Park, MN) for this example.

As before, the most important thing is the URL.

The URL we'll use to get forecast data is shown below. You would replace YOUR-API-KEY with your actual API key.
http://api.openweathermap.org/data/2.5/forecast?id=5019335&units=imperial&appid=YOUR-API-KEY,


This is the only change we need to make to our existing logic.

Because we have a different API result data set, we need to access it differently.

We'll often need to tinker here with param "result" (unless public API gives us very clear spec). You could start off replacing method body here with just "log(result);". That will allow you to see the entire result and you can then drill into it, like we do here e.g., "result.list".
static showApiResult(result) {
	const list = result.list;
	const main = list[0].main;
	const temperature = main.temp;
	for (var key of Object.keys(main))
		log(key + ': ' + main[key]);
}
After running the node app in the normal way, we should see output similar to this.

This is an elementary example, but gives us an idea of how to write an API consumer.
temp: 60.76
feels_like: 59.88
temp_min: 60.76
temp_max: 61.23
pressure: 995
sea_level: 995
grnd_level: 965
humidity: 71
temp_kf: -0.26
Here is the example project source code.

The project will not require npm init nor npm install.