• 4 heures
  • Facile

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 30/03/2022

Get Started With APIs

What Is An API?

You wake up late on a Sunday morning, and while you know you should get up, you just want to keep warm under your duvet. You think to yourself that in this cold month of February, jetting off to enjoy warmer temperatures in Mexico wouldn’t be such a bad idea. So you open up a flight comparison app, and voila! ✨ You find lots of offers from different airlines for flights to Cancún! You got all the flight info from Air France, American Airlines, and United Airlines without having to search for the information from each airline’s website. Not bad, huh?

You’re used to having all this data scroll before your eyes as if it’s magic, whether it’s on social media or any comparison application. From your point of view, it’s pretty simple – the information is delivered to you when you want it with no effort involved, but behind the scenes, it’s a different story! A lot of work goes on to make this happen, and for everything to run smoothly, you need one important tool: an API

Okay, okay – you’re talking a lot about APIs, but what does it actually mean? 🤔

API is an abbreviation that stands for application programming interface. In simple terms, it’s a way for different pieces of software to communicate with each other, whether that’s between different components of an application or two different applications. 

Right, but what does an API have to do with me scrolling non-stop through Instagram until 2 a.m. or comparing flights to Mexico? 🤔

How Do APIs Work?

Well, to answer that question, you have to know a little more about how APIs work. But before that, let’s discuss the basics of server-client communication.

Let’s use Aeromexico, a Mexican airline, as an example. Somewhere in the world, Aeromexico’s servers have access to all of the data you want to see for journeys between the U.S. and Mexico: different planes, the prices, flight status, etc. For you to access this, your browser - which we call the client - needs to get this information from somewhere.

And that somewhere is the server. So the client needs to have a conversation with the server. 

A typical conversation between client and server. On the left the client computer sends a request to the server on the right. Then the server on the right returns a response to the client.
A typical conversation between client and server

This conversation looks something like this:

  • Client: “Hey, server, can I see the flight times and prices from Chicago to Cancún on February 10?” 🤔

  • Server: “Here you go, here are all of the flights available from Chicago to Cancún on February 10!” 🎉

Or, if the server doesn’t find the data, it could reply something like:

  • Server: “Sorry, there aren’t actually any flights available on February 10.” 🙁

This is what’s called communication between client and server: the client requests data, and the server responds with data requested (if possible). 

So, where do APIs fit in with all of this? And what do they have to do with Aeromexico?

An API facilitates the interaction between a database and a client so they can communicate with one another: the API acts as an intermediary. The client requests information from the API, which searches for this information in the database and then returns it to the client.

A conversation between the client, the API and the database. The API in the middle retrieves data from the DB on the right, then the DB returns data to the API.
A conversation between the client, the API, and the database

APIs can communicate between many different components of your application, as well as between components of your application and other developers. They act as a go-between that delivers messages through requests and responses. 

Let’s return to our Aeromexico example. ✈️

We’ve created a flight comparison application that we’re going to call FlyScanner. This application cannot directly access information from Aeromexico or any other airline – it does not have access to their databases. But, if Aeromexico has an API, we can use that API to ask for data from Aeromexico without having to talk directly to their database. FlyScanner can request data from the Aeromexico API and share that data with the user! ✈️

FlyScanner can also use APIs from other airlines like Air France and Delta to compare prices from many different airlines running a flight on February 10 to Mexico City. Now all you have to do is enjoy those delicious tacos! ✨

APIs can communicate:

  • Between two different applications. 

  • Between a client and a server.

  • Between an application and developers. 

You've probably already seen one example of how an API is used to communicate between applications and developers. For example, have you used your Google or Facebook accounts to log in to certain websites without needing to create a username and password?

It's because they have built APIs that other developers can use in their own websites to take care of the user registrations and logins for them!

Screenshot of an interface proposing an identification by an external service such as connection with Facebook, Google, Apple or e-mail address.
APIs make these logins possible!

But how is that technically possible? 🤔

APIs create standardized and reusable methods that allow developers to access specific data when building applications. 

Let’s use an example. When you eat out, the restaurant menu shows a specific amount of predetermined options. This menu makes it easier for you, as it means you know what you can order, and therefore you get your food quicker. It also means the chef can better understand what you want. All you have to do is ask the waiter (the API) for a dish from the menu, which they then communicate to the kitchen. The kitchen prepares your dish and gives it back to the waiter who delivers it to you. 🙌

How Can You Use APIs as a Developer?

As a developer, you’ll certainly find yourself using various APIs in your professional life or for your personal projects. There are two main types: private APIs and public APIs. Let’s take a look at these together! 🕵🏻‍♀️

Private APIs

Private APIs make sure that people outside your organization or application don’t have access to the data available from your database. For example, if the OpenClassrooms developers wanted to build an internal application for HR employees to manage and analyze hiring information, the employees would want to view, create, and edit a lot of data. In order for the users to interact with the data in this way, OpenClassrooms developers could build an API so HR employees could access the hiring data through their application without giving this access to platform users like you and me.

An API can be used as a layer between the database and the person who wants to access or manipulate the data. A direct and uncontrolled database query could cause chaos! 🤯 

What if someone accidentally deletes a course or edits something they shouldn't have? A database is a bedrock for all the data in an application, so you wouldn't want it to be easily accessible or manipulated by just anyone. It’s a security issue! 🔥

An API allows for an extra level of security to better manage the data being accessed or edited by giving authorization to those who need them. This security means you can be in charge of which users have access to the database. 

In other words, you are the only one able to edit your personal data on your OpenClassrooms profile.

A private API enables only the authorized users within your organization or application to use the API to access the database. 

Public APIs

Unlike private APIs, these are APIs that others can use, whether they are on your application or not. They allow developers to get data from other applications to enhance their own projects. There are many ways you can use data from third-party (or external) APIs, but here are a few:

  1. You want to build a website that lists the weather conditions at different ski resorts. Instead of collecting your own weather data, you can use a Weather API and get the data from there! 🌤

  2. If you’re a musician looking to build a website so your fans can listen to your music, instead of building your own music streaming player, you can use the Spotify API and play your music directly on your website! 🎵

  3. You want to build a fan page for your favorite TV show (Game of Thrones, of course) by aggregating all the different cast Instagram accounts into one website - guess what, there’s an Instagram API that can help you do that! 🌅

There are also some APIs somewhere in the middle of public and private, which can happen when different API requests are only possible depending on your access level. 🔐 We'll talk about this later when we cover authentication. 😉

There are thousands of public APIs developers can use in different ways to enhance their projects. Here’s a list of those publicly available APIs you can use! 

Let’s Recap!

  • APIs are used to communicate data.

  • They allow communication between different components of your application and between your application and other developers using requests and responses.

  • They provide a means to access data in a reusable and standardized way.

  • Developers can use public and private APIs. 

  • Public APIs can be used by anyone online.

  • Private APIs can only be used by those with access and permission.

Why use a REST API rather than another API? There are various advantages to REST – we’ll go over them in the next chapter!

Exemple de certificat de réussite
Exemple de certificat de réussite