These are the topics for week 1:
- Application Programming Interface (API)
- Public/private APIs
- Connecting with APIs
- Asynchronous JavaScript and XML (AJAX)
- JavaScript Object Notation (JSON)?
- Stringifying and parsing JSON
- XMLHttpRequest (XHR)
- Modules & Libraries
- What's a module?
- What's a library?
- An example of a library
- How to use a library
Your teacher Stasel has made video lectures for this week's material. You can find them here: Videos 1 - 5
Whenever we talk about software development, we'll inevitably end up talking about Application Programming Interfaces, or APIs for short. But what is all the fuss about?
The first thing we need to understand is that API means different things to different people. Some people use it to refer to a complete application (frontend + backend), others use it to only refer to the server, or there's even people who use it to refer to any part of an application (i.e. "frontend API"/"server API")
For our purposes it's useful to stick to one definition, while keeping in mind that others will use it differently. Here's the definition we'll use:
An Application Programming Interface (API) is an interface to an application. It's the point of connection for any other application, in order to communicate with it. The API defines the terms of how to connect to it.You can think of an API as a wall socket:
As you can see on the image, the wall socket has a certain shape. This shape defines in what way something can connect to it. If you were to use a plug that had a different shape, it would never fit and thus never be able to connect. But if you had a plug that was in the correct shape, you got plug it in and proceed to connect to whatever is behind the socket (which in this case is the service of electricity).
In a way, you could say that an API is the frontend to an application. It's similar to the frontend part of a website. The biggest difference is, however, that instead of giving a way for human users to interact with it, an API gives a way for other applications to interact with it.
For more research, check out the following resources:
- APIs Are Like User Interfaces - Just With Different Users in Mind
- What are APIs - series
- APIs for Beginners
There are 2 different types of APIs: public and private APIs.
An API is public when software companies publish parts of their software to be freely used by developers from the outside world. If you were to integrate the Facebook API as a login system in your application, you would be using their API as a public API.
Conversely, there are also private APIs: software companies that grant access to parts of their backend applications to internal developers only, in order to develop new services to be used either internally or for the outside world.
In reality, there are way more private than public APIs. This is because it's usually in the company's best interest to keep their code base hidden from the public eye: it would be like giving your secret recipe away for free.
Keep this in mind: in the real world programming is only a means to serving a business end. In this course you're learning how to program, to make nice-looking, well-functioning applications. However, this is always done within a business context. This is to say: does this software lead to making more money/gaining more popularity/or the achievement of any other business goal?
A big part of what applications do is moving data from one place to another. Let's say you are on the HackYourFuture website and feel like donating some money. First of all, that's very nice of you! You head out to the website and click on the donate button. You type in the amount and click on "donate". You'll notice you immediately get redirected to a different website, namely checkout.stripe.com. How did Stripe know how to do this?
It's because the HackYourFuture website sends a HTTP Request to Stripe. The request basically says "Hey Stripe, some user from the HackYourFuture site wants to make a digital payment, can you handle that?". As a response Stripe answers "Of course, send the user to this specific URL and I'll take it from there!".
Anytime a request to an API is made this is called a
HTTP Request. However, in practice people use different terms for the same thing. Synonyms forHTTP RequestareAPI call/request,Network call/request,Web request/callorHTTP call. Which do you prefer?
A HTTP Request has to be made using a special method. The browser gives us two of them: XMLHttpRequest and Fetch API. XMLHttpRequest (or XHR for short) is the older, more verbose method. It looks like this:
// 1. Create a new XMLHttpRequest object
const xhr = new XMLHttpRequest();
// 2. Configure it: GET-request for the URL /article/.../load
xhr.open('GET', '/article/xmlhttprequest/example/load');
// 3. Send the request over the network
xhr.send();
// 4. This will be called after the response is received
xhr.onload = function() {
if (xhr.status != 200) {
// analyze HTTP status of the response
alert(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
} else {
// show the result
alert(`Done, got ${xhr.response.length} bytes`); // response is the server
}
};
xhr.onprogress = function(event) {
if (event.lengthComputable) {
alert(`Received ${event.loaded} of ${event.total} bytes`);
} else {
alert(`Received ${event.loaded} bytes`); // no Content-Length
}
};
xhr.onerror = function() {
alert('Request failed');
};This way of making HTTP Requests is outdated (and not recommended to use), but it's good to be aware of it as you might still see it in old code bases.
The newer way of making HTTP Requests involves using the Fetch API. You'll learn more about that next week!
For further study of how to make HTTP Requests, check out the following resources:
AJAX is the idea that data can be loaded into a webpage without refreshing the entire website. It's a web development technique when building websites, NOT a technology or programming language.
The term is an acronym for asynchronous JavaScript and XML. Let's pick that apart:
- Asynchronous JavaScript often refers to the act of using an asynchronous function to make an HTTP Request to fetch data. As we've learned in the previous module, an asynchronous function allows the browser to do multiple things simultaneously. In this way we fetch data in the background, while the user is still able to navigate the webpage.
- XML is a data format used to send information from a server to a client, and vice versa.
This technique was used back in the days when the web wasn't that advanced. Back then we used XML is the standard format we used to structure our data in. Nowadays we have replaced it with another data format: JSON.
JSON stands for JavaScript Object Notation and is a very JavaScript-like data format. Here's a small example:
{
"first name": "Noer",
"last name": "Paanakker",
"age": 28,
"address": {
"street address": "Strekkerweg 79",
"city": "Amsterdam",
"postal code": "1033 DA"
}
}If you look closely it almost looks exactly like a regular JavaScript object. There are 2 big differences: (1) in a JSON object everything is turned into a string (als known as "stringified"), and (2) it's not tied to the JavaScript language. Actually, many other languages can work with JSON!
In AJAX we make a HTTP Request to a web server, that then responds back with information to be used in the frontend. Generally speaking, this data will be send in JSON format. The web server "stringifies" (makes into a string) the data to be send first before it sends it.
JSON is the modern web standard data format to send and receive data in. In order to make something into JSON format we need to stringify it: make the whole object into one string. Luckily, JavaScript gives us a way to do this:
const noer = {
firstName: 'Noer',
lastName: 'Paanakker',
};
const noerJSON = JSON.stringify(noer);
console.log(noerJSON); // Result: {"firstName":"Noer","lastName":"Paanakker"}Here's another way of looking at the "stringifying" process: let's say you want to send your mother a gift, a brand new HackYourFuture T-shirt. Would you just put the shirt right into the mailbox, like that? Of course not! You would wrap it up nicely and put it into a box. Then you put it in the mailbox and off it goes!
This act of putting something into a box is what's happening when we stringify data (either on the client-side or server-side).
After the JSON data has been send, the receiver has to be able to interpret it. This process of making JSON interpretable by the programming language within that environment is called parsing. As we're using JavaScript, it doesn't seem like a big stretch. But what if we're using some other programming language like Python or Java?
To follow our analogy, this is basically your mother unpacking her T-shirt from out of the box you put it in!
Again, in JavaScript we can use another method gained from the global JSON object in order to parse our JSON data:
const noer = {
firstName: 'Noer',
lastName: 'Paanakker',
};
const noerJSON = JSON.stringify(noer);
const noerParsed = JSON.parse(noerJSON);
console.log(noerParsed); // Result: { firstName: 'Noer', lastName: 'Paanakker' };Nowadays we use JSON to perform asynchronous operations using JavaScript. So, technically speaking, the term would actually be AJAJ. However, the industry has decided to stick with the term AJAX to refer to these processes. Keep that in mind whenever someone asks you about it!
Go through the following to learn more about JSON and AJAX:
Traditionally, in order to make use of the AJAX technique we need to make use of a special type of object, called XMLHttpRequest(shortened to XHR). It's an object predefined for us by the window object in the browser.
The
windowobject is the most top-level object available to us in the browser. It contains thedocument, which contains all the HTML/CSS and JavaScript we write. Besides this, thewindowalso contains a lot of other things we use when writing frontend code:setTimeout(),alert()and it even contains a reference to theconsole(from which we getconsole.log()). Try it out in the console if you want to see for yourself!
By creating a new instance of this object we can start making HTTP requests!
const xhr = new XMLHttpRequest();Making XHR requests is the primary way of making HTTP Requests. It allows us to send and retrieve data from other services.
However, this method is outdated and we use more modern means now (using the Fetch Web API or a solution like axios). You will learn about that next week!
Check the following resources to learn more about XHR.

