| name | security-secure-api-consumption |
| description | null |
Secure Api Consumption
Category: security · Status: 🟢 Active
When to use
Khi gọi API từ FE: cấu hình transport, lỗi, secret, CORS và giới hạn tần suất.
Steps
- Luôn HTTPS; không gửi dữ liệu nhạy cảm qua query string.
- KHÔNG để secret/API key bí mật trong bundle FE — gọi qua BFF/route server.
- Xử lý lỗi rõ ràng: phân biệt 4xx/5xx, không lộ chi tiết nội bộ ra UI.
- Tôn trọng rate limit: backoff/retry có giới hạn, debounce request người dùng.
- Cấu hình CORS đúng (allowlist origin); gửi credentials chỉ khi cần.
- Validate response (xem input-validation) trước khi dùng.
Template
const res = await fetch('/api/bff/orders', { credentials: 'include' });
if (!res.ok) throw new ApiError(res.status);
const data = OrdersSchema.parse(await res.json());
Example
Good: key ở server/BFF, retry có backoff, CORS allowlist.
Avoid: nhúng secret vào NEXT_PUBLIC_*, retry vô hạn, CORS * kèm credentials.
Checklist