Chuyển tới nội dung chính

API Documentation

Tài liệu REST API của Smart Forecast Backend.


Swagger UI

Interactive API Documentation

Backend cung cấp Swagger UI để test API trực tiếp:

Swagger UI cho phép bạn:

  • Xem tất cả endpoints với mô tả chi tiết
  • Test API trực tiếp từ trình duyệt
  • Xem request/response schemas

Authentication

Tất cả API endpoints (trừ login/register) yêu cầu JWT token trong header.

Login

POST /api/v1/auth/login
Content-Type: application/json

{
"email": "user@example.com",
"password": "password123"
}

Response:

{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "uuid",
"email": "user@example.com",
"role": "ADMIN"
}
}

Sử dụng Token

GET /api/v1/weather/current
Authorization: Bearer <JWT_TOKEN>

Google OAuth (Mobile)

POST /api/v1/auth/google
Content-Type: application/json

{
"idToken": "google_id_token_from_mobile_app"
}

Lấy thông tin user hiện tại

GET /api/v1/auth/me
Authorization: Bearer <JWT_TOKEN>

Response:

{
"id": "uuid",
"email": "user@example.com",
"name": "Nguyễn Văn A",
"role": "USER",
"avatarUrl": "https://...",
"createdAt": "2025-01-15T10:00:00Z"
}

Roles & Permissions

RoleQuyền hạn
ADMINToàn quyền quản lý hệ thống
USERXem dữ liệu, nhận cảnh báo, gửi incidents

Response Format

Success Response

{
"data": {...},
"message": "Success",
"statusCode": 200
}

Error Response

{
"statusCode": 400,
"message": "Validation failed",
"error": "Bad Request",
"errors": [
{
"field": "email",
"message": "Invalid email format"
}
]
}

Pagination Response

{
"data": [...],
"meta": {
"total": 150,
"page": 1,
"limit": 20,
"totalPages": 8
}
}

Performance Notes

EndpointLatencyCache
/weather/current100-500msRedis 5 phút
/weather/forecast100-500msRedis 30 phút
/weather/history50-200msNo cache
/air-quality/current100-500msRedis 5 phút
/incident20-100msNo cache
/alert20-100msNo cache
/dashboard/summary50-200msRedis 1 phút

Testing với cURL

# 1. Login
TOKEN=$(curl -s -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"admin@smartforecast.com","password":"admin123"}' \
| jq -r '.access_token')

# 2. Get current weather
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/weather/current

# 3. Get air quality history
curl -H "Authorization: Bearer $TOKEN" \
"http://localhost:8000/api/v1/air-quality/history?page=1&limit=10"

# 4. Create alert
curl -X POST http://localhost:8000/api/v1/alert \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"title":"Test Alert","severity":"low","type":"weather"}'

# 5. Get dashboard summary
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/dashboard/summary

API Sections

APIMô tả
WeatherDữ liệu thời tiết hiện tại, dự báo và lịch sử
Air QualityChất lượng không khí và AQI
AlertQuản lý cảnh báo và ngưỡng cảnh báo
IncidentBáo cáo và xử lý sự cố
StationsQuản lý trạm quan trắc
IngestionThu thập dữ liệu từ nguồn bên ngoài
UsersQuản lý người dùng
FilesUpload và quản lý files
DashboardDữ liệu tổng hợp cho dashboard
ReportsXuất báo cáo PDF/CSV
Public NGSI-LDAPI công khai truy cập dữ liệu NGSI-LD (không cần auth)

Tiếp theo