Beginner15 min read

What is an API?

Learn what APIs are, how they work, and why they are the foundation of modern web development.

API — Application Programming Interface

An API (Application Programming Interface) is a set of rules that defines how two pieces of software can talk to each other. Think of it as a contract between a client and a server: the client agrees to ask for things in a specific format, and the server agrees to respond in a specific format.

The best way to understand an API is with the restaurant analogy. Imagine you are sitting in a restaurant (the client). You want food from the kitchen (the server/database), but you cannot just walk into the kitchen yourself. Instead, you use a waiter (the API). You look at the menu (the API documentation), choose what you want, and tell the waiter. The waiter takes your order to the kitchen, the kitchen prepares it, and the waiter brings it back to your table. You never need to know how the kitchen works — you just need to know what is on the menu and how to order.

APIs work the same way in software. Your frontend does not directly access the database. Instead, it sends requests to API endpoints, and the backend processes those requests and returns the data. This separation is powerful because it means the same backend API can serve multiple clients — a website, a mobile app, a smartwatch app, or even another server.

Real-world APIs are everywhere. Weather apps use a weather API to get forecasts. Google Maps provides an API so other apps can embed maps. Stripe provides a payment API so websites can process credit cards. Twitter's API lets developers build tools that read and post tweets. Every modern application you use is built on dozens of APIs talking to each other behind the scenes.

REST APIs

REST (Representational State Transfer) is the most common architecture style for building web APIs. A REST API organizes data into resources — things like users, posts, products, or orders. Each resource is accessible at a specific URL called an endpoint.

The URL structure follows a predictable pattern. You start with a base URL (like https://api.example.com), then add a path that identifies the resource:

  • https://api.example.com/users — the collection of all users
  • https://api.example.com/users/123 — a specific user with ID 123
  • https://api.example.com/users/123/posts — all posts by user 123
  • https://api.example.com/posts/456/comments — all comments on post 456

Notice the pattern: resources are nouns (users, posts, comments), not verbs. You do not create an endpoint called /getUser or /deletePost. Instead, you use the same URL and change the HTTP method to indicate what action you want to perform.

REST APIs also follow important principles. They are stateless — each request contains all the information the server needs to process it. The server does not remember previous requests. They use standard HTTP — the same protocol your browser uses to load web pages. And they return data in JSON format — a lightweight, human-readable text format that both humans and computers can easily understand.

This combination of predictable URLs, standard HTTP methods, and JSON responses makes REST APIs intuitive to learn and easy to work with. Once you understand the pattern, you can interact with almost any REST API without reading extensive documentation.

HTTP Methods

HTTP methods are the verbs of your API. They tell the server what action you want to perform on a resource. There are four main methods you will use constantly:

GET — Read data Retrieves information without modifying anything. It is safe to call GET as many times as you want — it just reads. Examples: loading a user's profile, fetching a list of products, getting search results.

  • GET /users — Get a list of all users
  • GET /users/42 — Get user with ID 42
  • GET /posts?category=tech — Get posts filtered by category

POST — Create new data Sends data to the server to create a new resource. The server receives the data, validates it, saves it to the database, and returns the newly created resource. Examples: creating a new account, publishing a blog post, placing an order.

  • POST /users — Create a new user (send user data in the request body)
  • POST /posts — Create a new blog post

PUT / PATCH — Update existing data Modifies a resource that already exists. PUT replaces the entire resource, while PATCH updates only specific fields. Examples: updating your profile picture, editing a post, changing a password.

  • PUT /users/42 — Replace all data for user 42
  • PATCH /users/42 — Update specific fields (e.g., just the email)

DELETE — Remove data Deletes a resource from the server. Examples: deleting a comment, removing a product, canceling an order.

  • DELETE /posts/15 — Delete post with ID 15
  • DELETE /users/42/avatar — Remove user 42's avatar

Think of these methods as a CRUD mapping: Create (POST), Read (GET), Update (PUT/PATCH), Delete (DELETE). Nearly every operation in a web application maps to one of these four actions.

Example API Endpoints for a Blog

html
<h3 style="font-family:sans-serif;">Blog API Endpoints</h3>
<table style="border-collapse:collapse; width:100%; font-family:sans-serif; font-size:14px;">
  <thead>
    <tr style="background:#1e293b; color:white;">
      <th style="padding:10px; text-align:left; border:1px solid #334155;">Method</th>
      <th style="padding:10px; text-align:left; border:1px solid #334155;">Endpoint</th>
      <th style="padding:10px; text-align:left; border:1px solid #334155;">Description</th>
    </tr>
  </thead>
  <tbody>
    <tr style="background:#f8fafc;">
      <td style="padding:8px; border:1px solid #e2e8f0; color:#16a34a; font-weight:bold;">GET</td>
      <td style="padding:8px; border:1px solid #e2e8f0; font-family:monospace;">/posts</td>
      <td style="padding:8px; border:1px solid #e2e8f0;">Get all blog posts</td>
    </tr>
    <tr>
      <td style="padding:8px; border:1px solid #e2e8f0; color:#16a34a; font-weight:bold;">GET</td>
      <td style="padding:8px; border:1px solid #e2e8f0; font-family:monospace;">/posts/1</td>
      <td style="padding:8px; border:1px solid #e2e8f0;">Get a single post by ID</td>
    </tr>
    <tr style="background:#f8fafc;">
      <td style="padding:8px; border:1px solid #e2e8f0; color:#2563eb; font-weight:bold;">POST</td>
      <td style="padding:8px; border:1px solid #e2e8f0; font-family:monospace;">/posts</td>
      <td style="padding:8px; border:1px solid #e2e8f0;">Create a new blog post</td>
    </tr>
    <tr>
      <td style="padding:8px; border:1px solid #e2e8f0; color:#d97706; font-weight:bold;">PUT</td>
      <td style="padding:8px; border:1px solid #e2e8f0; font-family:monospace;">/posts/1</td>
      <td style="padding:8px; border:1px solid #e2e8f0;">Update an existing post</td>
    </tr>
    <tr style="background:#f8fafc;">
      <td style="padding:8px; border:1px solid #e2e8f0; color:#dc2626; font-weight:bold;">DELETE</td>
      <td style="padding:8px; border:1px solid #e2e8f0; font-family:monospace;">/posts/1</td>
      <td style="padding:8px; border:1px solid #e2e8f0;">Delete a blog post</td>
    </tr>
  </tbody>
</table>

Which HTTP method is used to create a new resource?

Ready to practice?

Create your free account to access the interactive code editor, run challenges, and track your progress.