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-keyenv-varsproduction
Cách bảo mật API Key trong production

API key bị lộ = mất tiền thật

Key bị lộ trên GitHub bị bot quét trong 30 giây. Đã có developer mất $12,000 trong 2 giờ. Đây là cách bảo vệ.

Level 1: Environment variables

Bash
# .env (thêm vào .gitignore!)
IZZI_API_KEY=izzi-YOUR_KEY_HERE
Python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["IZZI_API_KEY"],
    base_url="https://api.izziapi.com/v1"
)

Level 2: .gitignore

Bash
# .gitignore
.env
.env.local
.env.production
*.key
*.pem

Level 3: Pre-commit hook

Bash
#!/bin/sh
# .git/hooks/pre-commit
if git diff --cached -U0 | grep -E '(izzi-|sk-|gsk_)'; then
  echo "LỖI: Phát hiện API key trong code!"
  exit 1
fi

Level 4: Key rotation mỗi 90 ngày

  1. Dashboard → API Keys → Tạo key mới
  2. Cập nhật environment/secret manager
  3. Verify key mới hoạt động
  4. Xóa key cũ

Checklist bảo mật

CheckMức độ
Không hardcode key trong sourceBắt buộc
.env trong .gitignoreBắt buộc
Pre-commit hookQuan trọng
Secret manager (production)Quan trọng
Rotate key mỗi 90 ngàyBest practice

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