IzziAPI
TipsApr 4, 20267 min read

Cách bảo mật API Key trong production

Biến môi trường, secret manager, key rotation và scope limiting để bảo vệ thông tin API của bạn.

Izzi API Team
Engineering & DevRel
bảo mậtapi keyproductionbest practices
Cách bảo mật API Key trong production

API key bị lộ = thảm họa

API key bị lộ có thể dẫn đến: chi phí phát sinh hàng nghìn dollar, dữ liệu bị truy cập trái phép, và service bị lạm dụng. Dưới đây là 5 bước bảo mật API key.

1. Biến môi trường — không hardcode

Text
# ❌ KHÔNG BAO GIỜ
api_key = "izzi-xxx-real-key-here"

# ✅ ĐÚNG CÁCH
import os
api_key = os.environ["IZZI_API_KEY"]

2. File .env + .gitignore

Text
# .env (local development)
IZZI_API_KEY=izzi-xxx-your-key

# .gitignore — BẮT BUỘC
.env
.env.local
.env.production

3. Secret Manager cho production

Dùng AWS Secrets Manager, GCP Secret Manager, hoặc Doppler:

Text
# AWS Secrets Manager
aws secretsmanager get-secret-value \
  --secret-id izzi-api-key \
  --query SecretString \
  --output text

4. Key rotation (xoay vòng key)

  • Tạo key mới trên Izzi API Dashboard
  • Cập nhật secret manager
  • Deploy lại service
  • Xóa key cũ
  • Lặp lại mỗi 90 ngày

5. Tách key theo môi trường

  • izzi-dev-xxx — cho development
  • izzi-staging-xxx — cho staging
  • izzi-prod-xxx — cho production

Checklist bảo mật

  • .env nằm trong .gitignore
  • ☐ Không có API key trong source code
  • ☐ Production dùng secret manager
  • ☐ Key rotate mỗi 90 ngày
  • ☐ Tách key theo môi trường
  • ☐ Đã cấu hình usage alert

Sẵn sàng bắt đầu?

Truy cập 38+ model AI qua một API duy nhất. Gói miễn phí — không cần thẻ tín dụng.

MORE

Bài viết liên quan