What Is an API? — Illustrated Guide
API stands for Application Programming Interface. It's a way for two programs to talk to each other over the internet.
The Restaurant Analogy
Think of a restaurant:
| Restaurant | Web API |
|---|---|
| You (customer) | Your mobile app |
| Waiter | The API |
| Kitchen | The backend server |
| Menu | API documentation |
| Your order | A request |
| Your food | A response |
You don't walk into the kitchen to cook. You tell the waiter what you want, and they bring it back. An API works the same way — your app sends a request, and the server sends back a response.
The Client-Server Model
┌─────────┐ Request (HTTP) ┌─────────┐
│ Client │ ──────────────────> │ Server │
│ (mobile) │ │ (Flask) │
│ │ <────────────────── │ │
└─────────┘ Response (JSON) └─────────┘
- Client: Your React Native mobile app (or browser, or another server)
- Server: Your Flask backend that processes requests and talks to the database
HTTP Methods
When your app talks to a server, it uses HTTP methods to describe what it wants to do:
| Method | Action | Example |
|---|---|---|
| GET | Read data | Get all chat messages |
| POST | Create data | Send a new message |
| PUT/PATCH | Update data | Edit a message |
| DELETE | Remove data | Delete a message |
The Request-Response Cycle
When your mobile app sends a message:
1. User types "Hello!" and taps Send
2. Mobile app sends a POST request to http://server:5000/chat
with body: {"message": "Hello!"}
3. Flask backend receives the request
4. Backend saves the message to SQLite database
5. Backend calls the OpenAI API to get a response
6. Backend saves the AI's response to the database
7. Backend sends back JSON: {"reply": "Hi there! How can I help?"}
8. Mobile app receives the response and displays it
What is JSON?
JSON (JavaScript Object Notation) is how data is formatted when sent between client and server.
{
"id": 1,
"message": "Hello!",
"sender": "user",
"timestamp": "2025-01-15T10:30:00Z"
}
It looks like a Python dictionary or a JavaScript object. Both languages can easily convert to/from JSON.
What is an Endpoint?
An endpoint is a specific URL + method combination:
| Endpoint | What it does |
|---|---|
GET /api/messages |
Get all messages |
POST /api/chat |
Send a message, get AI reply |
DELETE /api/messages/5 |
Delete message with ID 5 |
Status Codes
Every response includes a status code that tells you if it worked:
| Code | Meaning |
|---|---|
| 200 | OK — success |
| 201 | Created — successfully created a resource |
| 400 | Bad Request — you sent something wrong |
| 404 | Not Found — that URL doesn't exist |
| 500 | Server Error — something broke on the server |
Visual Summary
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Mobile │ POST │ Flask │ POST │ OpenAI │
│ App │ ──────> │ Server │ ──────> │ API │
│ │ │ │ │ │
│ Send │ JSON │ Save to │ JSON │ Generate │
│ "Hello!" │ │ SQLite │ │ reply │
│ │ <────── │ │ <────── │ │
│ │ JSON │ │ JSON │ │
│ Display │ reply │ Send │ reply │ │
│ reply │ │ back │ │ │
└──────────┘ └──────────┘ └──────────┘
Key Takeaway
An API is just a structured way for programs to talk to each other. Your app sends a request, the server processes it, and sends back a response. That's it.