2 minute read

What is API?

API stands for Application Programming Interface.

An API is a way for two software applications to communicate with each other.

Instead of directly accessing another application’s data or database, you send a request to its API. The API processes your request and returns a response.

How does API work?

Here’s a simplified flow:

Client
   │
   │ HTTP Request
   ▼
API Server
   │
   │ Query Database
   ▼
Database
   │
   ▼
API Server
   │
   │ HTTP Response (JSON)
   ▼
Client

The client sends a request.

The server processes the request.

Finally, the server sends a response back.

Request and Response

Suppose we want information about a user.

The client sends this request:

GET /users/1

The server responds with:

{
  "id": 1,
  "name": "John Doe",
  "country": "Indonesia"
}

This response is written in JSON, the most common format used by APIs.

HTTP Methods

Most web APIs use HTTP methods.

GET

Retrieve data.

GET /users

Example Give me all users.

POST

Create new data.

POST /users

Example Create a new user.

PUT

Replace existing data.

PUT /users/1

Example Replace all information about user #1.

PATCH

Update part of existing data.

PATCH /users/1

Example Only update the user's email.

DELETE

Delete data.

DELETE /users/1

Example Delete user #1.

HTTP Status Codes

After processing your request, the server returns a status code.

Some common examples are:

Status Code Meaning
200 OK
201 Created
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
500 Internal Server Error

For example:

GET /users/999

Response:

404 Not Found

This means the requested user doesn’t exist.

A Real Example

GitHub provides a public API.

Open this URL in your browser:

https://api.github.com/users/intodarkmoon

You’ll receive a JSON response like this:

{
  "login": "intodarkmoon",
  "id": 74888877,
  "followers": 2
}

Instead of returning a webpage, GitHub returns structured data that other applications can understand.

Where Are APIs Used?

Once I understood APIs, I realized I was using them every day without knowing it.

Whenever you:

  • Log into Spotify
  • Watch YouTube
  • Check the weather
  • Browse Instagram
  • Use ChatGPT
  • Create an EC2 instance with Terraform

An API is involved.

For example, when Terraform creates an EC2 instance, it doesn’t magically create the server.

Terraform sends API requests to AWS.

AWS receives those requests, creates the EC2 instance, and returns the result.

The same idea applies to Kubernetes.

When you run:

kubectl apply -f deployment.yaml

kubectl communicates with the Kubernetes API Server.

The API Server processes the request and creates the resources inside the cluster.

Tags: ,

Categories:

Updated: