ตัวอย่างที่ใช้กับ Phaya API เวอร์ชันปัจจุบัน
เลือกภาษาและคำขอที่ต้องการ ตัวอย่างทั้งหมดด้านล่างอ้างอิง media route และ response field ที่ backend ปัจจุบันรองรับ
เลือกสิ่งที่ต้องการรัน
ภาษาโปรแกรม
คำขอ
ใช้ platform key ที่ขึ้นต้นด้วย pk_ สำหรับโค้ดฝั่ง server ให้เก็บ key ไว้ใน environment variable และอย่า commit key จริง
อัปโหลดไฟล์สื่อ
ส่ง multipart form data ด้วย field ชื่อ file ระบบจะตอบกลับ media_id, url และ expires_at
phaya-example.js
// file is a File selected by the user.
async function uploadMedia(file, apiKey) {
const form = new FormData()
form.append('file', file)
const response = await fetch(
'https://api.phaya.io/api/v1/media/upload',
{
method: 'POST',
headers: { Authorization: `Bearer ${apiKey}` },
body: form,
},
)
if (!response.ok) {
throw new Error(`Upload failed: ${response.status}`)
}
const media = await response.json()
console.log(media.media_id, media.url, media.expires_at)
return media
}
const apiKey = 'pk_your_platform_key'
// const media = await uploadMedia(selectedFile, apiKey)เวิร์กโฟลว์สั้นและติดตามได้
อัปโหลดไฟล์ ส่ง URL ที่ได้รับไปสร้าง media job แล้วตรวจสอบงานจนเสร็จ
อัปโหลดสื่อ
สร้างงาน
ตรวจสอบสถานะ
ก่อนเริ่มใช้งาน
- เปลี่ยนเฉพาะ placeholder key และ URL หรือไฟล์ input
- ตรวจสอบ response.ok หรือ HTTP status ก่อนอ่าน success payload
- URL จากการอัปโหลดเป็นแบบชั่วคราว ให้ใช้ expires_at จาก upload response