เอกสาร API อัตราแลกเปลี่ยนเงินตรา (Currency Exchange Rates API Documentation)
นี่คือเอกสาร API ที่จำเป็นสำหรับนักพัฒนาที่ต้องการดึงข้อมูลอัตราแลกเปลี่ยนเงินตราไปใช้งานในแอปพลิเคชันอื่นๆ
ภาพรวม (Overview)
บริการนี้เป็น RESTful API สำหรับการดึงข้อมูลอัตราแลกเปลี่ยนเงินตรา
- Base URL:
https://your-domain.com/wp-json/currencyxrates/v1 - Namespace:
/wp-json/currencyxrates/v1
การยืนยันตัวตน (Authentication) และความปลอดภัย (Security)
- Authentication: ไม่จำเป็น ต้องมีการยืนยันตัวตน (None). endpoints ทั้งหมดเป็นแบบสาธารณะ (public)
- ความปลอดภัย: API จะไม่เปิดเผยรหัสภายใน (IDs) หรือข้อมูลเมตาที่อ่อนไหว (sensitive metadata) เช่น วันที่สร้างหรืออัปเดต เพื่อป้องกันการรั่วไหลของข้อมูล
การจำกัดอัตราการร้องขอ (Rate Limiting)
เพื่อป้องกันการใช้งานที่มากเกินไป API มีการจำกัดการร้องขอ:
- จำกัด: 100 requests per minute ต่อ IP address
- การตอบกลับเมื่อเกินกำหนด
จะได้รับ HTTP Status Code 429 พร้อมข้อความ{ "message": "Too many requests. Please try again later." }
รูปแบบการตอบกลับ (Response Format)
| ประเภทการตอบกลับ | HTTP Status Code | รูปแบบ |
|---|---|---|
| สำเร็จ (Success) | 200, 201, 204 | {"data": { ...ข้อมูลจริง... }} |
| ข้อผิดพลาด (Error) | 4xx, 5xx | {"message": "ข้อความแสดงข้อผิดพลาด"} |
ข้อมูลที่ใช้ใน Response
| ชื่อ Field | ชนิดข้อมูล (Data Type) | คำอธิบาย |
|---|---|---|
| buying_rate, selling_rate | float | อัตราการซื้อและขาย |
| min_exchange_amount | integer | ปริมาณการแลกเปลี่ยนขั้นต่ำ |
| currency, denomination | string | รหัสสกุลเงิน และชื่อเต็มของสกุลเงิน |
Endpoints สำหรับการดึงข้อมูลที่สำคัญ
| ลำดับ | Endpoint | Method | คำอธิบาย |
|---|---|---|---|
| 1 | /health | GET | ตรวจสอบสถานะการทำงานของ API |
| 2 | /currencies | GET | ดึงรหัสสกุลเงินทั้งหมดที่มีอยู่ |
| 3 | /rates | GET | ดึงอัตราแลกเปลี่ยนทั้งหมด พร้อมรองรับการแบ่งหน้า (Pagination) |
| 4 | /rates/{currency} | GET | ดึงอัตราแลกเปลี่ยนสำหรับสกุลเงินที่ระบุเท่านั้น |
รายละเอียด Endpoints
1. Health Check (ตรวจสอบสถานะ)
- Endpoint:
/health - ตัวอย่าง Response (200 OK):
{
"data": {
"status": "ok",
"time": "2025-11-09 14:30:00",
"plugin": "currency-exchange-rates"
}
}2. Get All Currencies (ดึงสกุลเงินทั้งหมด)
- Endpoint: /currencies
- ตัวอย่าง Response (200 OK):
{
"data": [
"USD",
"EUR",
"GBP",
"CAD"
]
}3. Get All Exchange Rates (ดึงอัตราแลกเปลี่ยนทั้งหมด)
-
Endpoint:
/rates -
Query Parameters (พารามิเตอร์เสริม):
-
per_page(integer): จำนวนผลลัพธ์ต่อหน้า (0 = ดึงทั้งหมด) -
page(integer): หมายเลขหน้า (ค่าเริ่มต้น: 1)
-
-
ตัวอย่าง Response (200 OK):
{
"data": {
"rates": [
{
"icon": "https://example.com/.../usd.png",
"currency": "USD",
"denomination": "United States Dollar",
"buying_rate": 109.50,
"selling_rate": 110.00,
"min_exchange_amount": 100,
"meta": { ... }
},
// ... อัตราแลกเปลี่ยนอื่นๆ
],
"total": 50,
"per_page": 0,
"page": 1
}
}4. Get Exchange Rates by Currency (ดึงอัตราแลกเปลี่ยนตามสกุลเงิน)
-
Endpoint:
/rates/{currency} -
URL Parameter:
-
currency(string, Required): รหัสสกุลเงิน (เช่นUSD,EUR)
-
-
ตัวอย่าง Response (200 OK):
{
"data": {
"currency": "USD",
"rates": [
{
"icon": "https://example.com/.../usd.png",
"currency": "USD",
"denomination": "United States Dollar",
"buying_rate": 109.50,
"selling_rate": 110.00,
"min_exchange_amount": 100,
"meta": { ... }
}
],
"count": 1
}
}ตัวอย่าง Response (404 Not Found):
{
"message": "No exchange rates found for currency: XYZ"
}การจัดการข้อผิดพลาด (Error Handling)
API ใช้ HTTP Status Codes มาตรฐาน:
| Status Code | คำอธิบาย |
|---|---|
| 200 | สำเร็จ (Success) |
| 400 | คำร้องขอไม่ถูกต้อง (Bad Request) |
| 404 | ไม่พบทรัพยากร (Not Found) |
| 429 | ร้องขอมากเกินไป (Too Many Requests - Rate Limit) |
| 500 | ข้อผิดพลาดภายในเซิร์ฟเวอร์ (Internal Server Error) |
