开发者文档

面向代理和仪表盘的读取 API

当仪表盘、脚本或 AI 代理需要 SaleHero 掌握的准确数字时,使用读取 API:在会计系统与仓库系统之间对账后的库存、单位成本、所有渠道的订单、发票及其已收款项、销售和利润。它只能读取,所以密钥泄露也无法改动任何数据。

访问方式

项目值
基础 URLhttps://salehero.ro/api/ai/v1
密钥权限范围为 ai:read 的 API 密钥(标签为“AI agent, read-only”)。完全访问的代理密钥也可使用;普通 v1 密钥会被以 403 拒绝。
方法GET
格式JSON,UTF-8。时间戳为 UTC 的 ISO 8601;台账和报表日期为 YYYY-MM-DD。
速率限制每个密钥每分钟 120 次请求;超出时返回 429,并带 Retry-After

订单和退货包含客户的姓名、邮箱、电话和收货地址,所以请像对待任何客户数据访问一样对待这个密钥。每次调用(包括查询字符串)都会写入审计日志。

约定

  • 新鲜度:每个响应都带有 data_as_of,表示其中数据最近一次刷新的时间(从未刷新则为 null)。在根据某个数字下结论之前,请先检查它以及 GET /sync-status。
  • 分页:列表接受 limit(默认 100,最大 500),并返回 next_cursor。把它作为 cursor 传回即可取下一页;null 表示已是最后一页。
  • 日期:from 和 to 为 YYYY-MM-DD,包含首尾,按 UTC 日计,与“分析”页面使用的日期相同。
  • 错误响应使用统一的响应体,{ "error": { "code", "message" } }。代码:invalid_request (400)、unauthorized (401)、forbidden (403)、sku_not_found、warehouse_not_found 和 not_found (404)、rate_limited(429,见 Retry-After)、internal_error (500)。
  • 只要 SaleHero 的目录、库存或台账中有某个 SKU,就算已知 SKU。已知 SKU 在所查范围内没有数据时,返回 200 和空列表,而不是 404。

库存

GET/api/ai/v1/stock/{sku}

某个 SKU 在各仓库的库存,按 SaleHero 掌握的数据给出:会计读数、仓库系统读数以及两者的差异。请对 SKU 做 URL 编码。

请求

curl -s https://salehero.ro/api/ai/v1/stock/SKU-001 \
  -H "Authorization: Bearer $SALEHERO_READ_KEY"

响应

{
  "sku": "SKU-001",
  "name": "Example product",
  "data_as_of": "2026-11-10T08:00:00.000Z",
  "readings": { "smartbill": "2026-11-10T08:00:00.000Z", "base": "2026-11-10T08:10:00.000Z" },
  "main_source": "smartbill",
  "warehouses": [
    {
      "warehouse": "Main",
      "quantity": 33,
      "smartbill_quantity": 33,
      "pending_invoice_quantity": 0,
      "smartbill_effective_quantity": 33,
      "base_quantity": 36,
      "difference": -3
    }
  ]
}
  • quantity 是 SaleHero 实际采用的库存:有效的会计数字;如果仓库系统是你的主库存来源,则采用仓库系统的数字。difference 不为零的地方,正是库存差错检查应当关注的地方。所有数值都为 0 的仓库不会列出。
GET/api/ai/v1/stock/{sku}/movements

某个 SKU 的库存台账,最新的在前。

参数

from, toYYYY-MM-DD
包含首尾的日期范围。
warehousestring
仓库名称,与 SaleHero 中显示的完全一致。
limit, cursorinteger 1–500, string
分页。

请求

curl -s "https://salehero.ro/api/ai/v1/stock/SKU-001/movements?from=2026-10-01&limit=2" \
  -H "Authorization: Bearer $SALEHERO_READ_KEY"

响应

{
  "sku": "SKU-001",
  "data_as_of": "2026-11-10T09:00:00.000Z",
  "coverage": { "movements_imported_until": "2026-10-26", "sales_imported_until": "2026-10-26", "invoices_synced_at": "2026-11-10T09:00:00.000Z" },
  "movements": [
    { "date": "2026-10-28", "type": "sale", "raw_type": "invoice", "document": "SER 12345", "source": "smartbill", "warehouse": "Main", "quantity": -1, "unit_cost": 21.4, "balance_after": null, "order_id": 123456789 }
  ],
  "next_cursor": null
}
  • type 为 sale、receipt、reversal(冲销,包括客户退货)、transfer_in、transfer_out、adjustment 或 other。quantity 为正表示入库,为负表示出库。台账的完整程度取决于最近一次导入:依赖它之前请先读取 coverage。

订单

GET/api/ai/v1/orders

所有渠道(BaseLinker、Shopify、手工、B2B)的订单,最新确认的在前。订单的重复副本会合并到被复制的订单中。你给出的每个筛选条件都必须匹配。

参数

qstring
自由文本,按邮箱、电话、订单号或平台订单号、AWB 或姓名来解读。
number, email, phone, name, awbstring
精确查找。phone 按最后 9 位数字匹配,不论格式。
sku, skusstring
订单行的 SKU,或最多 100 个以逗号分隔的 SKU。
from, toYYYY-MM-DD
确认日期。
statusstring
一个角色(new、processing、ready、in_delivery、delivered、cancelled、return、other)或状态名称的一部分。
channel, order_source, countrystring
销售渠道、BaseLinker 订单来源、收货国家(ISO-2)。
paidtrue | false
是否已收款。
limit, cursorinteger 1–500, string
分页。

请求

curl -s "https://salehero.ro/api/ai/v1/orders?paid=false&from=2026-11-01&limit=1" \
  -H "Authorization: Bearer $SALEHERO_READ_KEY"

响应

{
  "data_as_of": "2026-11-10T08:00:00.000Z",
  "orders": [
    {
      "id": "cm…",
      "source": "base",
      "number": "ORDER-1001",
      "channel": "shopify",
      "status": { "name": "Shipped", "role": "IN_DELIVERY" },
      "confirmed_at": "2026-11-05T10:00:00.000Z",
      "country": "RO",
      "currency": "RON",
      "total": 249.9,
      "total_ron": 249.9,
      "payment": { "method": "cod", "received": false, "paid_amount": null },
      "invoice": { "series": "SER", "number": 1234, "paid": false },
      "customer": { "name": "Example Customer", "email": "[email protected]", "phone": "0700000000" },
      "awb": "1234567890",
      "lines": [ { "sku": "SKU-001", "name": "Example product", "quantity": 1 } ],
      "lines_count": 1,
      "returns_count": 0
    }
  ],
  "next_cursor": null
}
  • total_ron 按确认当日的 BNR 汇率折算,没有汇率时为 null。lines 包含前 20 行。
接口返回内容
GET /orders/{ref}一个完整订单:所有带价格的订单行、收货和账单地址、带 AWB 和物流跟踪的包裹、带已付和未付金额的发票、快递代收货款结算、退货以及最近 50 条事件。ref 可以是 SaleHero id、BaseLinker id,或订单号、平台订单号(多个订单使用同一个号码时返回 409 ambiguous_order)。不会实时从 BaseLinker 获取任何内容。
GET /returns, GET /returns/{ref}退货单,最新的在前(q、origin、attention、from、to),以及按 id 或 RET 编号查询的单个退货单,并给出每一步的进展:BaseLinker 退货、DPD 取件、monday 条目。

商品与成本

接口返回内容
GET /products?q=… or ?skus=…按 SKU、名称或条码查找商品:先精确匹配,再文本匹配(limit 默认 50,最大 200;truncated 表示还有更多匹配)。skus 一次最多接受 100 个精确 SKU。
GET /products/{sku}商品资料:目录数据、SaleHero 为它保存的所有价格(含增值税)、成本基础以及最近 20 次价格变动。
GET /products/{sku}/cost每个仓库的平均单位成本、最近一次入库成本以及 pricing_cost_basis:SaleHero 计算利润率所依据的成本,适合用来实现“绝不低于成本报价”。
curl -s "https://salehero.ro/api/ai/v1/products?skus=SKU-001,SKU-002" \
  -H "Authorization: Bearer $SALEHERO_READ_KEY"

{
  "data_as_of": "2026-11-10T03:20:00.000Z",
  "products": [
    { "sku": "SKU-001", "name": "Example product", "barcode": "5940000000017",
      "list_price": 499.9, "currency": "RON", "state": "ACTIV",
      "active": true, "archived": false, "image_url": "https://…" }
  ],
  "truncated": false
}

发票、应收款、销售和利润

接口返回内容
GET /invoicesSmartBill 发票,最近开具的在前。筛选条件:number(系列和号码,任何常见写法均可)、numbers(最多 100 个)、order、client、from、to、paid。每张发票包含 series、number、issued_at、client、total、paid_amount、unpaid_amount、paid、payment_checked_at 以及它所涵盖的订单。
GET /invoices/{series}/{number}一张发票,含发票行、为其付款的快递结算,以及匹配的 e-Factura 消息。
GET /receivables客户仍欠的款项:total_outstanding、invoice_count、账龄区间(0–30、31–60、61–90、90 天以上)以及前 50 位客户。unchecked_invoices 统计从未轮询过付款状态的发票,这些发票不会计入报表。
GET /profit按 SKU 的利润,含完整单位成本(发票成本、关税、增值税、EPR、运输、佣金),区间为 from … to,最长 31 天;请按月逐月调用。可按 channel 和 country 筛选;用 limit 和 cursor 分页。warnings 说明哪些内容无法定价或换算。
GET/api/ai/v1/sales

某个时期的订单数、件数、收入和估算利润,按你选择的方式分组。订单与“分析”页面相同:每个真实订单计一次,按确认日期,已取消和已退货的状态不计入。收入为订单行含增值税的价格,按每日 BNR 汇率换算为 RON。

参数

from, toYYYY-MM-DD · 必填
时期,最长 366 天。
group_byday | month | channel | order_source | country | sku
默认为月。
channel, order_source, country, skustring
筛选条件。country 为 ISO-2。
limitinteger 1–500
限制分组数量;发生截断时 truncated 会指出。

请求

curl -s "https://salehero.ro/api/ai/v1/sales?from=2026-10-01&to=2026-10-31&group_by=channel" \
  -H "Authorization: Bearer $SALEHERO_READ_KEY"

响应

{
  "data_as_of": "2026-11-10T08:00:00.000Z",
  "from": "2026-10-01",
  "to": "2026-10-31",
  "group_by": "channel",
  "currency": "RON",
  "cost_basis": "cog",
  "totals": {
    "orders": 1200, "units": 2100, "revenue": 410000.5, "cost_estimate": 250000.2,
    "profit_estimate": 160000.3, "margin_pct": 39.0,
    "units_without_price": 0, "units_without_cost": 14, "units_unconverted": 0
  },
  "groups": [ { "key": "shopify", "orders": 800, "units": 1300, "revenue": 300000.1, "…": "…" } ],
  "truncated": false
}
  • cost_estimate 是件数乘以商品的货物成本;profit_estimate 只计入同时有价格和成本的行。units_without_price、units_without_cost 和 units_unconverted 说明这些数字遗漏了什么。
GET/api/ai/v1/sync-status

每个数据源最近一次运行的时间以及是否成功。信任某个数字之前请先检查。

请求

curl -s https://salehero.ro/api/ai/v1/sync-status \
  -H "Authorization: Bearer $SALEHERO_READ_KEY"

响应

{
  "data_as_of": "2026-11-10T10:15:00.000Z",
  "sources": [
    { "source": "base", "domain": "orders", "status": "ok",
      "last_run": "2026-11-10T10:00:00.000Z", "last_success": "2026-11-10T10:00:00.000Z", "last_error": null }
  ]
}
  • source 为 smartbill、base 或 shopify;domain 是同步的内容(orders、products、inventory、invoices、payments 等)。last_error 只在该数据源失败期间才有值。

获取 API 密钥

创建账户,连接你的工具,然后在“设置”中创建密钥。