การเรียก API ครั้งแรก
มาเรียก API ครั้งแรกกัน เพื่อตรวจสอบว่าการตั้งค่า Brevo API ของคุณทำงานได้อย่างถูกต้อง
สิ่งที่ต้องเตรียม
- บัญชี Brevo
- คีย์ API จากแดชบอร์ดของคุณ
- เครื่องมือสำหรับส่งคำขอ HTTP (curl, Postman หรือเครื่องมือที่คล้ายกัน)
ดึงข้อมูลบัญชี
การเรียกครั้งแรกที่ง่ายที่สุดคือการดึงข้อมูลบัญชีของคุณ
ใช้ cURL
curl -X GET "https://api.brevo.com/v3/account" \ -H "Accept: application/json" \ -H "api-key: YOUR_API_KEY"ใช้ JavaScript (Node.js)
const fetch = require('node-fetch');
const getAccount = async () => { try { const response = await fetch('https://api.brevo.com/v3/account', { method: 'GET', headers: { 'Accept': 'application/json', 'api-key': 'YOUR_API_KEY' } });
const data = await response.json(); console.log('Account Info:', data); } catch (error) { console.error('Error:', error); }};
getAccount();ใช้ Python
import requests
url = "https://api.brevo.com/v3/account"headers = { "Accept": "application/json", "api-key": "YOUR_API_KEY"}
response = requests.get(url, headers=headers)print(response.json())ใช้ PHP
<?php$url = 'https://api.brevo.com/v3/account';$headers = [ 'Accept: application/json', 'api-key: YOUR_API_KEY'];
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);curl_close($ch);
echo $response;?>การตอบกลับที่คาดหวัง
หากสำเร็จ คุณจะได้รับการตอบกลับในลักษณะนี้
{ "firstName": "John", "lastName": "Doe", "companyName": "Your Company", "address": { "street": "123 Main Street", "city": "New York", "zipCode": "10001", "country": "United States" }, "plan": [ { "type": "payAsYouGo", "credits": 10000, "creditsUsed": 1500 } ]}การจัดการข้อผิดพลาด
หากพบข้อผิดพลาด ให้ตรวจสอบปัญหาที่พบบ่อยเหล่านี้
คีย์ API ไม่ถูกต้อง (401 Unauthorized)
{ "code": "unauthorized", "message": "Invalid API key provided"}วิธีแก้ไข: ตรวจสอบว่าคีย์ API ของคุณถูกต้องและมีสิทธิ์การใช้งานที่เหมาะสม
เกินขีดจำกัดอัตราการเรียก (429 Too Many Requests)
{ "code": "too_many_requests", "message": "Rate limit exceeded"}วิธีแก้ไข: รอสักครู่ก่อนส่งคำขอถัดไป หรืออัปเกรดแพ็กเกจของคุณ
ข้อผิดพลาดของเซิร์ฟเวอร์ (500 Internal Server Error)
{ "code": "internal_error", "message": "An internal error occurred"}วิธีแก้ไข: ตรวจสอบหน้าสถานะระบบ ของเรา หรือติดต่อฝ่ายสนับสนุน
ขั้นตอนถัดไป
เมื่อคุณเรียก API สำเร็จเป็นครั้งแรกแล้ว
- ตั้งค่าการยืนยันตัวตน ให้เรียบร้อย
- ติดตั้ง SDK สำหรับภาษาที่คุณใช้
- ทำความเข้าใจขีดจำกัดอัตราการเรียก
- สำรวจ endpoint สำหรับอีเมล