# What is an API?

> The network bridge that enables front-end clients to securely request data or trigger actions on a remote back-end server.

Subject: Internet | Lesson 14 of 35 | 4 min read
Interactive lesson: https://codewithanimation.in/internet/what-is-an-api
Previous: [HTTPS & TLS](https://codewithanimation.in/internet/https-and-tls.md) | Next: [RESTful Principles](https://codewithanimation.in/internet/restful-principles.md)

## What it is

An API (Application Programming Interface) is a set of rules and protocols that allows different software applications to communicate with each other.

In the context of web development, a Web API (often an HTTP API or REST API) acts as a gateway allowing a client (like a web browser) to request data or perform actions on a remote server over the internet using the HTTP protocol. Instead of returning full HTML webpages, APIs expose specific URLs (endpoints) that return structured data, typically in JSON format.

## Why it matters

APIs form the backbone of modern web architecture by enabling a strict **Separation of Concerns** between the front-end (client) and the back-end (server).

By using APIs, front-end developers can build rich, interactive user interfaces without needing to know how the underlying database works or how the server logic is implemented. This decoupling allows teams to work independently, enables multiple clients (like a website, an iOS app, and an Android app) to consume the exact same data source, and makes scaling applications significantly easier.

## How it works

APIs expose **Endpoints**: specific URLs exposed by the server (e.g., `https://api.example.com/users`) that represent resources or actions.

Clients use **HTTP Methods** to tell the server what action to perform on the endpoint (`GET` to retrieve, `POST` to create, `PUT`/`PATCH` to update, `DELETE` to remove).

The server typically responds with **JSON (JavaScript Object Notation)**, a lightweight, human-readable text format that natively converts into usable JavaScript objects on the front-end.

## Common mistake: Common mistake

**Confusing the API with the database itself.** Beginners often think an API *is* the database, assuming their front-end code is directly querying the database tables. In reality, the API is an intermediary-a protective layer of code written by back-end developers that securely interacts with the database on behalf of the client. It ensures validation, security, and proper formatting.

## Tip: Pro tip

Use the **Restaurant Waiter Analogy**. The database/server is the kitchen, the client (your front-end app) is a customer, and the API is the waiter. You don't go into the kitchen and cook your own food (direct database access). Instead, you look at the menu (the API documentation) and give your order (an HTTP Request) to the waiter (the API). The waiter securely takes your order to the kitchen, waits for the food, and brings the prepared meal (the HTTP Response / JSON data) back to your table.

## Try it: Try it

Construct a request by dragging HTTP methods and endpoints together. Watch the API waiter deliver the JSON response from the server kitchen!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/what-is-an-api

## Check yourself

### Question 1

What is the primary role of a Web API in modern front-end development?

- A. To style the HTML elements on the page.
- B. To store user data permanently inside the browser.
- C. To allow the front-end application to request data from and communicate with a backend server.
- D. To compile JavaScript code into machine code for faster execution.

Correct answer: C

A Web API acts as a network bridge that enables the front-end client to securely request data or trigger actions on a remote back-end server over the internet.

- Why not A: Styling is handled by CSS, not APIs.
- Why not B: Data storage inside the browser is handled by APIs like LocalStorage or IndexedDB, but not remote Web APIs.
- Why not D: This is the job of the JavaScript Engine inside the browser, like V8.

### Question 2

Which of the following HTTP methods is typically used to create a new resource on the server?

- A. GET
- B. POST
- C. DELETE
- D. PUT

Correct answer: B

In standard RESTful APIs, the POST method is used to send data to the server with the intention of creating a new record, such as registering a new user or publishing a new blog post.

- Why not A: GET is used exclusively to retrieve or fetch existing data.
- Why not C: DELETE is used to remove an existing resource.
- Why not D: PUT is typically used to update or completely replace an existing resource.

### Question 3

Why is JSON heavily used with Web APIs?

- A. It is a robust database querying language.
- B. It automatically encrypts data for secure transmission.
- C. It is a lightweight, human-readable text format that is natively easy for JavaScript to parse.
- D. It replaces HTML for structuring visual layouts.

Correct answer: C

JSON (JavaScript Object Notation) has become the universal standard for Web APIs because it is incredibly lightweight, easy for humans to read, and can be instantly converted into usable JavaScript objects on the front-end without complex parsing.

- Why not A: SQL is a querying language; JSON is strictly a data formatting standard.
- Why not B: HTTPS provides encryption; JSON itself is just plain text.
- Why not D: JSON represents raw data, not visual structure or layout.

## Remember this

- Web APIs bridge the gap between clients and servers using standard HTTP.
- They return structured data (usually JSON) instead of HTML.
- APIs are not databases; they are protective, logical intermediaries.
