API (Application Programming Interface) Basics
An API is a set of defined rules and protocols that allows two separate software systems to communicate and exchange data. Simply put, it is the request and response window provided by a server.
1. Role and Analogy
Think of an API as a waiter in a restaurant.
- Customer (Client/Browser): Requests a specific item (data) from the waiter.
- Waiter (API): Takes the order to the kitchen (Server).
- Kitchen (Server): Prepares the dish (processes the data).
- Waiter (API): Delivers the result back to the customer.
The API hides the complex internal workings, allowing the user to access only the necessary functionality easily.
2. How Web APIs Work (Focusing on REST)
Most web APIs follow the REST (Representational State Transfer) architectural style, utilizing URLs and HTTP methods.
① Request
The client sends a request to a specific server URL using one of the defined HTTP methods.
| HTTP Method | Role | Database Analogy (CRUD) |
|---|---|---|
| GET | Retrieve a resource (Read data) | Read |
| POST | Create a new resource (Add data) | Create |
| PUT/PATCH | Modify an existing resource | Update |
| DELETE | Remove a resource | Delete |
② Response
After processing the request, the server sends back a Status Code indicating success or failure, along with the requested data (often in JSON format).
- Success Examples:
200 OK,201 Created - Failure Examples:
400 Bad Request,503 Service Unavailable