▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼▲▼
tags : #flutter #coding
references : Coding
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼▲▼### 🚀 What is an API?
An API (Application Programming Interface) is a bridge that allows different applications,
services, or systems to communicate with each other. Instead of directly accessing a database
or service, applications request data through an API.
Think of it like a waiter in a restaurant:
1. You (the client) order food.
2. The waiter (API) takes the request to the kitchen (server).
3. The kitchen prepares the meal (data processing).
4. The waiter brings your meal back to you (response).
🔥 Why Do We Use APIs?
APIs are used to:
Fetch and send data from/to a database or server.
Allow different apps/services to work together (e.g., Google Maps in Uber).
Securely expose functionality without revealing internal code.
Enable real-time updates (e.g., stock prices, social media feeds).
🛠️How APIs Work (Step by Step)
APIs follow a request-response model:
1. Client (App) Sends a Request
The app makes an HTTP request (GET, POST, etc.) to an API endpoint (URL).
It includes parameters (e.g., user ID) and headers (e.g., authentication token).
2. API Server Processes the Request
The server checks if the request is valid.
It fetches or modifies data in the database.
It prepares a response.
3. Server Sends a Response
The API returns data in JSON or XML format.
If something goes wrong, it sends an error message (e.g., 404 Not Found).
4. Client Uses the Response
The app updates the UI or processes the data.
🛠️Types of APIs
APIs come in different forms based on architecture and usage:
1️⃣ Web APIs (Most Common)
These are APIs used over the internet using HTTP.
Examples:
REST API (Representational State Transfer) ✅ Most popular
SOAP API (Simple Object Access Protocol)
GraphQL API (Flexible alternative to REST)
2️⃣ Library & Framework APIs
These are built into programming languages to interact with system components.
Examples:
Flutter SDK APIs (e.g., http , sqflite )
Android/iOS APIs (e.g., camera, location services)
3️⃣ Operating System APIs
APIs that let apps interact with the OS.
Examples:
Windows API (WinAPI)
Linux Kernel API
4️⃣ Hardware APIs
Used to interact with physical devices.
Examples:
Bluetooth API
Camera API
🌍 REST API (The Most Used API)
REST (Representational State Transfer) is the most common type of API. It follows these
principles:
✅ Stateless – Each request is independent.
✅ Uses HTTP methods – GET, POST, PUT, DELETE.
✅ Uses JSON or XML for data transfer.
✅ Follows a URL structure – /users/1 fetches user with ID 1.
🌐 HTTP Methods in REST API
HTTP Method Purpose Example
GET Retrieve data /users (Get all users)
POST Create new data /users (Add new user)
PUT Update data /users/1 (Update user with ID 1)
DELETE Remove data /users/1 (Delete user with ID 1)
📡 API Data Flow (How Data Moves)
Example: Your Spense app wants to sync expenses with a backend.
1️⃣ Request
Your app (client) sends a request to https://api.spense.com/expenses with:
{
"userId": 123,
"amount": 50,
"category": "Food"
}
2️⃣ Processing
The API backend:
Authenticates the user.
Validates the data (e.g., amount is a number).
Saves the data to the database.
3️⃣ Response
The API returns:
{
"status": "success",
"expenseId": 789
}
4️⃣ Client Updates UI
Your app updates the expense list and balance.
🛡️API Security
APIs need protection to prevent unauthorized access.
Common security methods:
🔒 API Keys – A secret key sent with requests.
🔒 OAuth2 – Secure authentication (Google/Facebook login).
🔒 JWT (JSON Web Token) – Encrypted tokens for authentication.
⚡ API Tools & Libraries
Here are some tools to work with APIs in Flutter:
Dio (Recommended) – Powerful API client
http – Simple and lightweight
Postman – API testing tool
Swagger – API documentation
🎯 Final Thoughts
APIs connect your app to the world! They enable real-time updates, external data integration,
and secure storage.
Since your Spense app is 70% complete, integrating an API can help:
✅ Sync expenses across multiple devices.
✅ Store user data securely.
✅ Fetch exchange rates or financial insights.
👉 Do you want to build your own backend (Node.js, Firebase, Supabase) or just consume
external APIs? 🚀