API Documentation
Base URL: https://giaphoangtn.io.vn/api
Giới thiệu
API cho phép bạn lấy sản phẩm số sau khi mua. Tất cả response đều là JSON. Mỗi lần mua hàng, bạn nhận một API key duy nhất qua email.
Authentication
Truyền API key qua query parameter key:
GET /api/product?key=gh_xxxxxxxxxxxxxxxx&product_id=1
Với admin endpoints, dùng Bearer token trong header Authorization.
Validate API Key
GET
/api/validate
Public
Kiểm tra API key còn hiệu lực không.
Query params:| Param | Bắt buộc | Mô tả |
|---|---|---|
key | Yes | API key cần kiểm tra |
{
"valid": true,
"product_id": 1,
"expires_at": "2025-12-31T00:00:00Z",
"used_count": 3,
"remaining_uses": -1
}
Test ngay:
Lấy sản phẩm
GET
/api/product
API Key required
Lấy nội dung sản phẩm số từ kho.
Query params:| Param | Bắt buộc | Mô tả |
|---|---|---|
key | Yes | API key hợp lệ |
product_id | Yes | ID sản phẩm |
curl "https://giaphoangtn.io.vn/api/product?key=gh_xxx&product_id=1"
Response:
{
"product": {
"id": 1,
"name": "AI Prompt Pack Pro",
"type": "digital",
"content": "# AI Prompt Pack Pro\n\n..."
}
}
Webhook đơn hàng
POST
/api/webhook/order
Webhook
Endpoint nhận webhook từ sàn thương mại. Tự động cấp API key cho khách sau khi thanh toán thành công.
Request body (JSON):{
"order_id": "ORDER_123",
"product_id": 1,
"customer_email": "khach@email.com",
"secret": "webhook-secret-key"
}
Response:
{
"success": true,
"api_key": "gh_xxxxxxxxxxxxxxxxxxxxxxxx",
"message": "API key đã được gửi tới khach@email.com"
}
⚠️ Đặt WEBHOOK_SECRET trong Cloudflare Worker env vars để bảo mật endpoint này.
Danh sách sản phẩm
GET
/api/products
Public
curl "https://giaphoangtn.io.vn/api/products"
Danh sách bài viết
GET
/api/posts
Public
Query params (optional):
| Param | Mô tả |
|---|---|
category | Lọc theo category |
limit | Số bài trả về (default 10) |
Admin — Thêm sản phẩm
POST
/api/admin/product
Admin only
curl -X POST "https://giaphoangtn.io.vn/api/admin/product" \
-H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Tên sản phẩm",
"description": "Mô tả",
"content": "Nội dung đầy đủ...",
"type": "digital",
"price": 199000
}'
Admin — Cấp API Key thủ công
POST
/api/admin/issue-key
Admin only
curl -X POST "https://giaphoangtn.io.vn/api/admin/issue-key" \
-H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"product_id": 1,
"customer_email": "khach@email.com",
"expires_days": 365,
"max_uses": -1
}'
Error Codes
| Status | Code | Mô tả |
|---|---|---|
| 400 | MISSING_PARAMS | Thiếu tham số bắt buộc |
| 401 | INVALID_KEY | API key không hợp lệ |
| 403 | EXPIRED_KEY | API key đã hết hạn |
| 403 | EXCEEDED_USES | Vượt giới hạn số lần dùng |
| 403 | WRONG_PRODUCT | Key không có quyền với product này |
| 404 | NOT_FOUND | Không tìm thấy |
| 429 | RATE_LIMITED | Quá nhiều request |
Rate Limits
| Endpoint | Giới hạn |
|---|---|
| GET /api/product | 100 req/min per key |
| GET /api/validate | 60 req/min per IP |
| POST /api/webhook/* | 30 req/min per IP |
| Admin endpoints | 10 req/min |