Skip to content

ตัวอย่างโค้ดและ SDK หลากภาษา

Fotobots ทำงานผ่าน RESTful OpenAPI 3.1 มาตรฐาน คุณสามารถใช้ภาษาโปรแกรมมิ่งใดก็ได้ในการเชื่อมต่อ


1. Node.js / TypeScript

การอัปโหลดภาพขึ้นระบบ

import axios from 'axios';
import fs from 'fs';
const API_BASE = 'https://api.fotobots.dev/openapi';
const API_KEY = 'fb_test_YOUR_KEY';
const ALBUM_ID = 'YOUR_ALBUM_UUID';
async function uploadPhoto(filePath: string) {
// 1. ขอ Presigned URL สำหรับอัปโหลด
const { data: signData } = await axios.post(
`${API_BASE}/photos/photo/signed`,
{
albumId: ALBUM_ID,
filename: 'runner_001.jpg',
contentType: 'image/jpeg',
},
{ headers: { 'x-api-key': API_KEY } }
);
// 2. ยิงไบนารีภาพตรงไปยัง Cloudflare R2 / AWS S3
const fileBuffer = fs.readFileSync(filePath);
await axios.put(signData.uploadUrl, fileBuffer, {
headers: { 'Content-Type': 'image/jpeg' },
});
// 3. ยืนยันการอัปโหลดสำเร็จเพื่อให้ AI เริ่มทำงาน
const { data: successData } = await axios.post(
`${API_BASE}/photos/photo/success`,
{
albumId: ALBUM_ID,
photoId: signData.photoId,
},
{ headers: { 'x-api-key': API_KEY } }
);
console.log('ส่งภาพเข้าคิว AI สำเร็จ:', successData);
}

2. Python 3

import requests
API_BASE = "https://api.fotobots.dev/openapi"
API_KEY = "fb_test_YOUR_KEY"
ALBUM_ID = "YOUR_ALBUM_UUID"
def search_face(probe_image_path: str):
# 1. ขอ URL อัปโหลดภาพตัวอย่างใบหน้า
sign_res = requests.post(
f"{API_BASE}/photos/search/face/signed",
headers={"x-api-key": API_KEY},
json={"albumId": ALBUM_ID, "filename": "probe.jpg"}
).json()
# 2. อัปโหลดภาพใบหน้า
with open(probe_image_path, "rb") as f:
requests.put(sign_res["uploadUrl"], data=f)
# 3. ยิงค้นหาเวกเตอร์ใบหน้า
results = requests.post(
f"{API_BASE}/photos/search/face/list",
headers={"x-api-key": API_KEY},
json={
"albumId": ALBUM_ID,
"searchPhotoId": sign_res["searchPhotoId"],
"threshold": 0.80
}
).json()
print(f"พบภาพถ่ายที่ตรงกันทั้งหมด {len(results.get('photos', []))} ภาพ!")
return results