API
This page contains information about APIs and the Spotify API. You may need an understanding of JavaScript before understanding this page fully.
Overview
For this semester project you will also need to learn about APIs. But what exactly is an API? An API (Application Programming Interface) allows different devices to connect to each other and send, update and request data from one to the other. You can think of it as a waiter in a restaurant. The waiter takes and understands your order, delivers it to the kitchen and comes back with the requested order. APIs work in a similar way.

Why Do We Need APIs?
APIs are all about sharing data over the internet. For example, let's consider a weather app. The weather app needs to retrieve data about current weather conditions and forecast from a weather service provider. The app cannot access this information directly because it resides on the weather service provider's servers, and the app does not have access to those servers.
This is where APIs come in. The weather service provider exposes its data and functionality through an API, which the weather app can access using a set of pre-defined requests and responses. The weather app sends a request to the API, asking for the current weather conditions or forecast for a particular location. The API processes the request and sends back a response containing the requested information.
Without APIs, the weather app would have to create its own weather forecasting system, which would be time-consuming and expensive. APIs provide a simple and efficient way for the weather app to access the weather service provider's data and functionality, making it easier to develop the app and provide accurate and up-to-date weather information to users.
The weather service provider may share its data through APIs for a variety of reasons, including:
Generating revenue: The weather service provider may offer their API for a fee, generating revenue from companies and developers who want to use their data in their applications.
Encouraging innovation: By making their data available through APIs, the weather service provider can encourage innovation and creative uses of their data, which may lead to new and useful applications.
Establishing themselves as a leader: By offering APIs, the weather service provider can establish themselves as a leader in their industry, promoting their brand and reputation.
Overall, sharing data through APIs can provide many benefits for the weather service provider, as well as for the developers and users of applications that integrate their API.
How do we get the data?
Every API is unique but if you understand how to obtain output from one API you will know how to get data from all. For the most part, APIs are accessed through URLs, and the specifics of how to query these URLs change based on the specific service you are using. For example, the OpenWeatherMap API, that we will be using in our project, has several types of data that you can request. To get the current weather in London, type in the following URL into your browser.
api.openweathermap.org/data/2.5/weather?q=London
You will get an error message, prompting you to use an API key. Go ahead and create an account to obtain an API key from their free tier. Once the key has been activated, which can take up to 2 hours, try making a new request with the city of your choice AND the API key passed in as query string parameters, like the example above.
api.openweathermap.org/data/2.5/weather?q=London&APPID=YOUR_API_KEY_HERE
Congrats, you have made your first API request!
So how do we actually get the data from an API into our code?
In JavaScript we use the Fetch API to make our API call. You will learn more about how to use it in section 28 of the Udemy course which will also go over JSON, the text format of the weather data. To provide you with a template for later on and as a reminder, this is one way of how to deal with fetch:
async function exampleAPICall() {
try {
const response = await fetch(
"https://api.openweathermap.org/data/2.5/weather?q=London&AAPID=YOUR_API_KEY_HERE"
);
const data = await response.json();
/* your code here to work with the data */
} catch (error) {
console.log(error);
}
}