1. 概述与基础地址
开放 API 用于让您的自有系统(ERP、BI、财务中台等)以机器对机器(M2M)方式安全读取或写入咖莱翼各业务数据。所有接口均为 HTTPS,返回统一 JSON 信封。
生产基础地址(Base URL):
https://open.calay.com.cn/api/open/v1
open.calay.com.cn 即接入入口,所有开放接口均挂在 /api/open/v1 前缀下。测试/演示环境路径带 /v050 前缀(如 https://test.calay.com.cn/v050/api/open/v1),字段完全一致。能力范围
利润中心(读/写) 人事中心(读) 报表中心(读) 渠道订单(读) 共享商业(读)
2. 鉴权方式
每个请求必须在 Authorization 头携带 Bearer 令牌。支持两种获取/携带方式:
方式 A:OAuth2 client_credentials(推荐服务端调用)
用 client_id + client_secret 调用令牌接口换取短期 JWT(默认 1 小时有效),再以 JWT 调业务。
# 1) 换取令牌
curl -X POST https://open.calay.com.cn/api/open/v1/oauth/token \
-H "Content-Type: application/json" \
-d '{
"grant_type": "client_credentials",
"client_id": "calay_a6856eec8010e5fa87d93084",
"client_secret": "sk_336a257b366abb9a8410c502099f1761fe38fb3d61d4e297"
}'
# 2) 用返回的 access_token 调业务
curl https://open.calay.com.cn/api/open/v1/profit/dashboard \
-H "Authorization: Bearer <access_token>"
方式 B:PAT 个人访问令牌(sk_ 直连)
直接用密钥本身(sk_ 前缀)作为 Bearer 令牌调用业务,无需先换 JWT,适合简单脚本。
curl https://open.calay.com.cn/api/open/v1/profit/dashboard \
-H "Authorization: Bearer sk_336a257b366abb9a8410c502099f1761fe38fb3d61d4e297"
3. 响应格式(统一信封)
所有接口返回统一结构:
| 字段 | 类型 | 说明 |
|---|---|---|
code | int | 0 = 业务成功;1 = 业务失败 |
bizCode | string | 业务码,如 SUCCESS / APIKEY_INVALID / SCOPE_DENIED |
message | string | 人类可读描述 |
data | object/array | 业务数据载体 |
tenant_id | string | 数据所属租户 |
// 成功
{
"code": 0,
"bizCode": "SUCCESS",
"message": "ok",
"data": { ... },
"tenant_id": "tenant_xxxx"
}
// 失败
{
"code": 1,
"bizCode": "APIKEY_INVALID",
"message": "client_id or client_secret invalid",
"data": null,
"tenant_id": null
}
code 配合判断:鉴权/授权类失败通常返回 401/403,参数错误返回 400;请同时检查 HTTP 状态码与 bizCode。4. 错误码(bizCode)
| bizCode | HTTP | 含义 | 处理建议 |
|---|---|---|---|
PARAM_INVALID | 400 | 必需参数缺失或格式错误 | 检查请求体字段是否完整 |
UNSUPPORTED_GRANT | 400 | grant_type 非 client_credentials | 固定传 client_credentials |
AUTH_REQUIRED | 401 | 未携带 Authorization 头 | 补充 Authorization: Bearer <token> |
APIKEY_INVALID | 401 | client_id / client_secret 不匹配 | 核对密钥,或重新创建 |
APIKEY_REVOKED | 401 | 密钥已被吊销 | 在控制台重新生成密钥 |
SCOPE_DENIED | 403 | 当前密钥 scope 不包含该资源 | 在控制台为密钥追加对应 scope |
NOT_FOUND | 404 | 资源不存在(如门店/月份无数据) | 确认参数 org_id/store_id/month 正确 |
5. 权限 Scope 矩阵
密钥创建时按以下 scope 子集授权,调用对应接口时由网关强制校验:
| Scope | 说明 | 覆盖接口 |
|---|---|---|
profit:read | 利润中心只读 | dashboard / stores / store profit / profit report |
profit:write | 利润中心写入 | 创建门店 / 营收录入 / 成本录入 |
hr:read | 人事中心只读 | hr dashboard / employees |
report:read | 报表中心只读 | 利润趋势 / 月度利润 / 人事薪资 / 账单发票 |
channel:read | 渠道订单只读 | channel orders |
sb:read | 共享商业只读 | sb agreements / bills / snapshots |
/keys 系列)不走 scope 体系,需控制台登录且具备 config 权限(owner/admin)。5.1 令牌换取
请求体参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
grant_type | string | 是 | 固定 client_credentials |
client_id | string | 是 | 控制台生成的客户端 ID(calay_ 前缀) |
client_secret | string | 是 | 密钥明文(sk_ 前缀) |
响应 data 字段
| 字段 | 类型 | 说明 |
|---|---|---|
access_token | string | JWT,用于后续业务调用的 Bearer 令牌 |
token_type | string | 固定 Bearer |
expires_in | int | 有效期(秒),默认 3600 |
scope | array | 该密钥被授予的 scope 列表 |
5.2 密钥管理(控制台内操作)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
scopes | array | 是 | scope 子集,如 ["profit:read","report:read"] |
role | string | 否 | operator(默认)/ owner |
响应 data:client_id、client_secret(仅创建时明文返回一次)、scopes、role。
5.3 利润中心(scope: profit:read / profit:write)
返回利润总览(汇总指标)。data 典型字段:total_revenue、total_cost、net_profit、store_count 等。
门店列表。data 为数组,元素含 store_id、store_name、org_id 等。
路径参数
| 参数 | 说明 | 示例 |
|---|---|---|
org_id | 组织/门店编码 | org_1001 |
month | 会计月份 YYYY-MM | 2026-09 |
返回该门店当月损益。data 典型字段:org_id、month、revenue、cost、profit。
路径参数
| 参数 | 说明 | 示例 |
|---|---|---|
store_id | 门店 ID | store_001 |
month | 会计月份 YYYY-MM | 2026-09 |
返回单店明细报表(按层 A/B/C/D/E/F/I 结构)。
请求体参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
store_name | string | 是 | 门店名称 |
org_id | string | 是 | 组织编码 |
创建门店,返回新建 store_id。
请求体参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
store_id | string | 是 | 门店 ID |
month | string | 是 | 会计月份 YYYY-MM |
amount | number | 是 | 营收金额(元) |
请求体参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
store_id | string | 是 | 门店 ID |
month | string | 是 | 会计月份 YYYY-MM |
amount | number | 是 | 成本金额(元) |
5.4 人事中心(scope: hr:read)
人事总览指标:employee_count、payroll_total 等。
员工列表。data 数组元素含 employee_id、name、department、status 等。
5.5 报表中心(scope: report:read)
| 参数 | 说明 | 示例 |
|---|---|---|
store_id | 门店 ID | store_001 |
months | 回溯月数 | 12 |
单店利润趋势(按月序列)。
| 参数 | 说明 | 示例 |
|---|---|---|
months | 回溯月数 | 12 |
全部门店合并利润趋势。
| 参数 | 说明 | 示例 |
|---|---|---|
month | 会计月份 YYYY-MM | 2026-09 |
指定月份利润报表(多店合并)。
| 参数 | 说明 | 示例 |
|---|---|---|
month | 会计月份 YYYY-MM | 2026-09 |
人事薪资报表。
账单与发票报表(只读汇总)。
5.6 渠道订单(scope: channel:read)
渠道(外卖/第三方平台)订单列表。支持分页查询参数 ?page=1&pageSize=50。data 数组含 order_id、channel、amount、created_at 等。
5.7 共享商业(scope: sb:read)
共享商业合作协议列表。
共享商业账单列表。
共享商业分红快照列表。
6. 完整对接示例(curl)
以下演示从「换取令牌 → 读取利润总览 → 读取月度报表」的完整链路:
# 步骤 1:换取访问令牌
TOKEN=$(curl -s -X POST https://open.calay.com.cn/api/open/v1/oauth/token \
-H "Content-Type: application/json" \
-d '{"grant_type":"client_credentials","client_id":"calay_替换为你的ID","client_secret":"sk_替换为你的密钥"}' \
| python -c "import sys,json;print(json.load(sys.stdin)['data']['access_token'])")
# 步骤 2:读取利润总览
curl https://open.calay.com.cn/api/open/v1/profit/dashboard \
-H "Authorization: Bearer $TOKEN"
# 步骤 3:读取 2026-09 月度利润报表
curl "https://open.calay.com.cn/api/open/v1/report/profit-month/2026-09" \
-H "Authorization: Bearer $TOKEN"
# 步骤 4(可选):用 PAT 直连,跳过换令牌
curl https://open.calay.com.cn/api/open/v1/profit/stores \
-H "Authorization: Bearer sk_替换为你的密钥"
calay_... / sk_... 为占位,请替换为控制台「开放平台」中实际创建的密钥。密钥仅在创建时明文展示一次,请妥善保存。7. 限流与最佳实践
- 令牌缓存:JWT 默认 1 小时有效,请在服务端缓存并重用,避免每次请求都换取(换取接口限流 20 次/分钟/IP)。
- 最小权限:为每个对接系统创建独立密钥,仅授予所需 scope;下线时及时吊销。
- 密钥保管:
client_secret等同密码,请勿写入前端代码或公开仓库;泄露立即吊销。 - 错误重试:遇
401/403先检查 scope 与令牌有效性;遇网络错误采用指数退避重试。 - 时间参数:所有月份参数统一
YYYY-MM格式。