No history yet

Introduction to APIs

What is an API?

Think of a restaurant. You, the customer, don't walk into the kitchen to cook your own meal. Instead, you're given a menu of available dishes. You place an order with a waiter, who then communicates your request to the kitchen. The kitchen prepares your meal, and the waiter brings it back to you.

In this story, the waiter is the API. It acts as an intermediary, taking a specific request from you and delivering a response from the kitchen, which is the system that does the work. You don't need to know how the kitchen operates, what ingredients are used, or how to cook the dish. You just need to know what's on the menu and how to ask for it.

API

noun

Stands for Application Programming Interface. It's a set of rules and protocols that allows different software applications to communicate with each other.

An API is a contract. It defines the specific ways a programmer can request services from an operating system, application, or other service. It allows developers to access another company's data or functionality without having to understand the complex code behind it. This saves a massive amount of time, as developers don't have to build every single feature from scratch.

An Application Programming Interface (API) serves as an intermediary that allows two applications to communicate.

How APIs Work

The communication process through an API is typically a two-way street known as a request-response cycle. A client application sends a request for information or an action to a server. The API receives this request, tells the system what to do, and then returns a response to the client. The response contains the requested information or confirms that the action was completed.

For example, when you use a flight booking website, you enter your departure city, destination, and dates. The website (the client) sends a request through an API to various airlines' systems (the servers). The airlines' systems process the request and send back available flights, prices, and times. The website then displays this information to you. You're interacting with multiple complex systems, but the API makes it feel like a single, seamless experience.

A Common Approach: REST

There are many types of APIs, but one of the most popular and widely used styles for web services is REST, which stands for Representational State Transfer. It's not a strict protocol, but rather a set of architectural principles for designing networked applications.

A key idea behind REST is that it treats everything as a resource. A resource could be a user profile, a blog post, a product, or a weather forecast. Each resource is identified by a unique URL, like a web address.

RESTful APIs use standard HTTP methods, the same ones your web browser uses to navigate the internet.

These standard methods define what action you want to perform on a resource:

MethodActionDescription
GETReadRetrieve a resource.
POSTCreateCreate a new resource.
PUTUpdate/ReplaceUpdate or replace an existing resource.
DELETEDeleteRemove a resource.

When you use a RESTful API, you send an HTTP request specifying the method and the URL of the resource. The server then sends back a response, often in a format like JSON, which is lightweight and easy for machines to parse and for humans to read.

{
  "user": {
    "name": "Alex Taylor",
    "id": 12345,
    "status": "active"
  }
}

This simple, standardized approach is why REST APIs are so common. They form the backbone of countless applications, from social media feeds to complex enterprise software.

Lesson image

Let's test your understanding of these core API concepts.

Quiz Questions 1/5

In the restaurant analogy for APIs, what does the waiter represent?

Quiz Questions 2/5

What is the primary function of an API?

APIs are the invisible connectors of the digital world, enabling the services we use every day to work together seamlessly.