Bỏ qua để đến nội dung

Agent API Reference

Thông tin nhanh

  • Base URL: https://api.salehay.com
  • Auth: Header X-Agent-Token: agent_xxx (tạo tại Admin → Settings → Agent Tokens)
  • Rate Limit: Mặc định 60 req/phút (cấu hình per-token)
  • Scoped Permissions: Mỗi token có danh sách scopes riêng
  • Response: application/json

Xem thêm: Agent Tokens · Webhooks · MCP Server

Mỗi request phải gửi header X-Agent-Token:

Terminal window
curl -H "X-Agent-Token: agent_abc123def456..." https://api.salehay.com/api/agent/analytics

Token cần scope phù hợp cho mỗi endpoint:

ScopeQuyền
leads:readTìm kiếm, đọc leads
leads:writeTạo mới, cập nhật leads
bookings:readĐọc bookings
calls:writeGhi nhận cuộc gọi
messages:writeGửi/log tin nhắn
analytics:readĐọc analytics
*Toàn quyền

POST /api/agent/leads

Scope: leads:write

Request Body:

{
"phone": "0901234567",
"name": "Nguyễn Văn A",
"email": "a@gmail.com",
"source": "agent",
"service_interest": "Massage Trị Liệu",
"note": "Khách quan tâm gói cao cấp",
"priority": "high",
"custom_fields": { "budget": "5tr" }
}
FieldTypeBắt buộcMô tả
phonestringSĐT Việt Nam (bắt đầu 0, 9-11 số)
namestringTên khách hàng
emailstringEmail
sourcestringNguồn: facebook, google, tiktok, zalo, website, agent (default: agent)
service_intereststringDịch vụ quan tâm
notestringGhi chú
prioritystringlow, normal (default), high
custom_fieldsobjectFields tùy chỉnh { "key": "value" }

Response 201:

{
"ok": true,
"lead_id": "uuid",
"customer_id": "uuid",
"status": "new"
}

GET /api/agent/leads/search?q=0901&status=new&limit=20

Scope: leads:read

ParamTypeBắt buộcMô tả
qstring❌*Tìm theo SĐT hoặc tên (ILIKE)
statusstring❌*Filter: new, contacted, appointment_set, qualified, converted, lost
limitnumberMax kết quả (default 20, max 50)

*Ít nhất một trong q hoặc status là bắt buộc.

Response 200:

{
"leads": [
{
"id": "uuid",
"status": "new",
"priority": "high",
"source": "facebook",
"service_interest": "Massage",
"phone": "0901234567",
"name": "Nguyễn Văn A",
"email": "a@gmail.com",
"created_at": "2026-03-17T..."
}
],
"total": 1
}

PATCH /api/agent/leads/:id

Scope: leads:write

Request Body:

{
"status": "contacted",
"priority": "high",
"note": "Đã gọi, khách đồng ý hẹn thứ 5"
}
FieldTypeBắt buộcMô tả
statusstring❌*Trạng thái mới
prioritystring❌*low, normal, high
notestring❌*Ghi chú cập nhật

*Ít nhất một field là bắt buộc.

Trạng thái hợp lệ:

new → contacted → appointment_set → showed_up → qualified → converted
→ no_show → reschedule
→ lost

Response 200:

{ "ok": true, "lead_id": "uuid", "status": "contacted" }

POST /api/agent/calls/log

Scope: calls:write

Request Body:

{
"lead_id": "uuid",
"outcome": "answered",
"duration_seconds": 120,
"note": "Khách đồng ý hẹn thứ 5",
"sub_outcome": "appointment_set"
}
FieldTypeBắt buộcMô tả
lead_iduuid❌*UUID của lead
booking_iduuid❌*UUID của booking (thay thế lead_id)
outcomestringanswered, no_answer, busy, voicemail, rejected
duration_secondsnumberThời lượng cuộc gọi (giây)
notestringGhi chú cuộc gọi
sub_outcomestringKết quả phụ
reject_reasonstringLý do từ chối (nếu outcome=rejected)

*Ít nhất lead_id hoặc booking_id là bắt buộc.

Response 201:

{ "ok": true, "call_id": "uuid" }

GET /api/agent/bookings/today

Scope: bookings:read

Response 200:

{
"bookings": [
{
"id": "uuid",
"status": "pending",
"service": "Massage Trị Liệu",
"date": "2026-03-17",
"time_slot": "09:00",
"note": "Lần đầu",
"phone": "0901234567",
"name": "Nguyễn Văn A",
"email": null
}
],
"stats": {
"total": 8,
"pending": 3,
"confirmed": 5,
"arrived": 0,
"completed": 0,
"cancelled": 0
}
}

POST /api/agent/messages/send

Scope: messages:write

Request Body:

{
"lead_id": "uuid",
"channel": "zalo",
"message_text": "Xin chào anh A, em nhắc lịch hẹn ngày mai nhé ạ!",
"template_name": "booking_reminder"
}
FieldTypeBắt buộcMô tả
lead_iduuid❌*UUID của lead
customer_iduuid❌*UUID của customer (thay thế lead_id)
channelstringzalo, whatsapp, sms, telegram
message_textstringNội dung tin nhắn
template_namestringTên template nếu dùng mẫu

*Ít nhất lead_id hoặc customer_id là bắt buộc.

Response 200:

{ "ok": true, "logged": true }

GET /api/agent/analytics

Scope: analytics:read

Response 200:

{
"pipeline": {
"new": 12,
"contacted": 8,
"appointment_set": 5,
"showed_up": 3,
"converted": 15,
"lost": 7
},
"today": {
"new_leads": 3,
"bookings": { "total": 8, "pending": 3, "confirmed": 5 }
},
"hot_leads": [
{
"id": "uuid",
"phone": "0901234567",
"name": "Nguyễn Văn A",
"status": "new",
"priority": "high",
"source": "facebook",
"service_interest": "Massage",
"created_at": "2026-03-17T..."
}
],
"conversion_30d": {
"converted": 15,
"total": 50,
"rate": "30.0%"
}
}
FieldMô tả
pipelineSố lượng leads theo từng status
today.new_leadsLeads mới tạo hôm nay
today.bookingsTổng hợp bookings hôm nay
hot_leadsTop 10 leads ưu tiên cao (status new/contacted, priority high)
conversion_30dTỷ lệ chuyển đổi 30 ngày gần nhất

StatusÝ nghĩa
200Thành công
201Tạo mới thành công
400Request không hợp lệ (thiếu field, sai format)
401Agent token thiếu hoặc không hợp lệ
403Token thiếu scope cần thiết
404Không tìm thấy (lead, booking)
429Rate limit exceeded — thử lại sau
500Database error

Error Response Format:

{ "error": "Mô tả lỗi cụ thể" }

Mỗi Agent API endpoint được expose qua MCP Server dưới dạng tool:

MCP ToolEndpointMô tả
crm_create_leadPOST /api/agent/leadsTạo lead mới
crm_searchGET /api/agent/leads/searchTìm kiếm leads
crm_update_leadPATCH /api/agent/leads/:idCập nhật lead
crm_log_callPOST /api/agent/calls/logGhi nhận cuộc gọi
crm_get_bookingsGET /api/agent/bookings/todayBookings hôm nay
crm_send_messagePOST /api/agent/messages/sendGửi/log tin nhắn
crm_get_analyticsGET /api/agent/analyticsAnalytics

Xem chi tiết: MCP Server docs