Orders API 参考

概览

Orders API 让你以编程方式创建、查询、更新与删除订单,适合电商、票务、预约等需要订单流转的业务。

所有请求都通过 HTTPS 发送,请求体与响应体均为 application/json。时间字段统一使用 Unix 时间戳(秒);金额字段统一以最小货币单位(分)表示,例如 39600 表示 ¥396.00。

基础 URL

Base URLtext
https://api.orders.example.com/v1

核心对象:Order

所有端点围绕 order 对象展开。下表列出其字段,后续各端点的响应均基于此结构。

字段类型说明
idstring订单唯一标识,前缀 ord_
objectstring固定为 "order"
customer_idstring下单客户标识,前缀 cust_
statusstringpending / paid / fulfilled / cancelled / refunded
currencystring三位 ISO 货币代码,默认 CNY
itemsarray订单明细数组,见 items 结构
amount_subtotalinteger商品小计(分)
amount_totalinteger订单总额(分)
shipping_addressobject收货地址对象
metadataobject自定义键值对,最多 20 个
created_atinteger创建时间(Unix 秒)
updated_atinteger最近更新时间(Unix 秒)
items 结构:每个元素包含 product_id(string)、name(string)、quantity(integer)、unit_price(integer,分)。

鉴权

Orders API 使用 Bearer Token 进行身份认证。每个请求必须在 Authorization 请求头中携带你的 API 密钥。

请求头http
Authorization: Bearer sk_live_********************************
  • 密钥以 sk_live_(生产)或 sk_test_(测试)开头,在控制台的「设置 → API 密钥」中创建。
  • 测试密钥产生的订单仅存在于沙箱,不会触发真实扣款或履约。
  • 密钥等同于账户权限,请勿提交到代码仓库或前端代码中。
鉴权失败:缺少或无效的令牌会返回 401 Unauthorized,错误码为 authentication_error。详见 错误码

限流说明

每个 API 密钥默认限制为 每分钟 100 次请求。超出后返回 429,需等待重置后重试。

每次响应都会在头部返回当前限流状态,便于你在客户端做退避处理:

响应头说明
X-RateLimit-Limit当前窗口允许的最大请求数(100)
X-RateLimit-Remaining当前窗口剩余可用请求数
X-RateLimit-Reset限流窗口重置时间(Unix 秒)
Retry-After触发 429 时返回,秒数,表示需等待多久
示例:限流响应头http
HTTP/1.1 429 Too Many Requests
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1754000460
Retry-After: 12
建议:收到 429 时,使用 Retry-After 的值进行指数退避重试,避免雪崩。批量操作请使用 列表端点 的分页而非高频单查。

GET /orders

返回当前账户下的订单列表,支持分页与多维筛选。结果按创建时间倒序返回。

查询参数

参数类型必填说明
limitinteger可选返回数量,默认 10,最大 100
cursorstring可选分页游标,取上一页响应的 next_cursor
statusstring可选按状态筛选:pending|paid|fulfilled|cancelled|refunded
customer_idstring可选按客户筛选
created_afterinteger可选仅返回该时间戳之后创建的订单
created_beforeinteger可选仅返回该时间戳之前创建的订单

请求示例

列出订单curl
curl https://api.orders.example.com/v1/orders?limit=2&status=paid \
  -H "Authorization: Bearer sk_live_********************************"

响应示例

200 · 订单列表json
{
  "object": "list",
  "data": [
    {
      "id": "ord_8f3k2m1n4p",
      "object": "order",
      "customer_id": "cust_9a2b7c",
      "status": "paid",
      "currency": "CNY",
      "items": [
        {
          "product_id": "prod_x12",
          "name": "陶瓷手作体验券",
          "quantity": 2,
          "unit_price": 19800
        }
      ],
      "amount_subtotal": 39600,
      "amount_total": 39600,
      "shipping_address": {
        "line1": "深圳市宝安区新安街道xx号",
        "city": "深圳",
        "state": "广东",
        "postal_code": "518100",
        "country": "CN"
      },
      "metadata": {},
      "created_at": 1754000000,
      "updated_at": 1754000120
    }
  ],
  "has_more": false,
  "next_cursor": null
}

状态码

状态码含义
200成功返回订单列表
401鉴权失败
429触发限流

POST /orders

创建一个新订单。订单初始状态为 pending,需另行调用支付或更新接口推进状态。

请求体参数

参数类型必填说明
customer_idstring必填客户标识,前缀 cust_
itemsarray必填至少一个明细项,每项含 product_idquantity
shipping_addressobject必填收货地址,含 line1/city/state/postal_code/country
currencystring可选货币代码,默认 CNY
metadataobject可选自定义键值对,最多 20 个

请求示例

创建订单curl
curl https://api.orders.example.com/v1/orders \
  -H "Authorization: Bearer sk_live_********************************" \
  -H "Content-Type: application/json" \
  -d '{
    "customer_id": "cust_9a2b7c",
    "currency": "CNY",
    "items": [
      { "product_id": "prod_x12", "quantity": 2, "unit_price": 19800 }
    ],
    "shipping_address": {
      "line1": "深圳市宝安区新安街道xx号",
      "city": "深圳",
      "state": "广东",
      "postal_code": "518100",
      "country": "CN"
    }
  }'

响应示例

201 · 已创建json
{
  "id": "ord_8f3k2m1n4p",
  "object": "order",
  "customer_id": "cust_9a2b7c",
  "status": "pending",
  "currency": "CNY",
  "items": [
    {
      "product_id": "prod_x12",
      "name": "陶瓷手作体验券",
      "quantity": 2,
      "unit_price": 19800
    }
  ],
  "amount_subtotal": 39600,
  "amount_total": 39600,
  "shipping_address": {
    "line1": "深圳市宝安区新安街道xx号",
    "city": "深圳",
    "state": "广东",
    "postal_code": "518100",
    "country": "CN"
  },
  "metadata": {},
  "created_at": 1754000000,
  "updated_at": 1754000000
}

状态码

状态码含义
201订单创建成功
400参数校验失败
401鉴权失败
429触发限流

GET /orders/{id}

按订单 ID 查询单个订单的完整信息。

路径参数

参数类型必填说明
idstring必填订单 ID,前缀 ord_

请求示例

查询订单curl
curl https://api.orders.example.com/v1/orders/ord_8f3k2m1n4p \
  -H "Authorization: Bearer sk_live_********************************"

响应示例

200 · 订单详情json
{
  "id": "ord_8f3k2m1n4p",
  "object": "order",
  "customer_id": "cust_9a2b7c",
  "status": "paid",
  "currency": "CNY",
  "items": [
    {
      "product_id": "prod_x12",
      "name": "陶瓷手作体验券",
      "quantity": 2,
      "unit_price": 19800
    }
  ],
  "amount_subtotal": 39600,
  "amount_total": 39600,
  "shipping_address": {
    "line1": "深圳市宝安区新安街道xx号",
    "city": "深圳",
    "state": "广东",
    "postal_code": "518100",
    "country": "CN"
  },
  "metadata": { "source": "wechat_mini" },
  "created_at": 1754000000,
  "updated_at": 1754000120
}

状态码

状态码含义
200查询成功
401鉴权失败
404订单不存在
429触发限流

PATCH /orders/{id}

对订单进行部分更新。仅传入需要修改的字段,未传字段保持不变。常用于推进订单状态或更正收货地址。

路径参数

参数类型必填说明
idstring必填订单 ID

请求体参数

参数类型必填说明
statusstring可选目标状态,需符合状态机流转规则
shipping_addressobject可选完整覆盖收货地址
metadataobject可选合并更新自定义字段
状态机:订单只能按 pending → paid → fulfilled 正向流转,或退回 cancelled / refunded。非法流转会返回 422

请求示例

更新订单状态curl
curl https://api.orders.example.com/v1/orders/ord_8f3k2m1n4p \
  -X PATCH \
  -H "Authorization: Bearer sk_live_********************************" \
  -H "Content-Type: application/json" \
  -d '{ "status": "fulfilled" }'

响应示例

200 · 更新后的订单json
{
  "id": "ord_8f3k2m1n4p",
  "object": "order",
  "customer_id": "cust_9a2b7c",
  "status": "fulfilled",
  "currency": "CNY",
  "items": [
    {
      "product_id": "prod_x12",
      "name": "陶瓷手作体验券",
      "quantity": 2,
      "unit_price": 19800
    }
  ],
  "amount_subtotal": 39600,
  "amount_total": 39600,
  "shipping_address": {
    "line1": "深圳市宝安区新安街道xx号",
    "city": "深圳",
    "state": "广东",
    "postal_code": "518100",
    "country": "CN"
  },
  "metadata": { "source": "wechat_mini" },
  "created_at": 1754000000,
  "updated_at": 1754005300
}

状态码

状态码含义
200更新成功
401鉴权失败
404订单不存在
422状态流转非法或字段冲突
429触发限流

DELETE /orders/{id}

取消并删除一个订单。仅 pendingpaid 状态可被删除;已履约(fulfilled)的订单需走退款流程而非删除。

路径参数

参数类型必填说明
idstring必填订单 ID

请求示例

删除订单curl
curl https://api.orders.example.com/v1/orders/ord_8f3k2m1n4p \
  -X DELETE \
  -H "Authorization: Bearer sk_live_********************************"

响应示例

200 · 删除确认json
{
  "id": "ord_8f3k2m1n4p",
  "object": "order",
  "deleted": true
}

状态码

状态码含义
200删除成功
401鉴权失败
404订单不存在
409状态冲突,订单不可删除
429触发限流

错误码

所有错误响应均使用 application/json 并返回统一的错误结构。通过 error.code 进行程序化处理。

错误响应结构json
{
  "error": {
    "code": "validation_error",
    "message": "items is required and must contain at least one item",
    "field": "items"
  }
}

通用错误码表

HTTP 状态码error.code说明
400validation_error请求参数缺失或格式错误,详见 field
401authentication_error缺少或无效的 API 密钥
403permission_error密钥无权访问该资源
404not_found订单或资源不存在
409conflict_error资源状态冲突,操作无法执行
422unprocessable_error语义校验失败,如非法状态流转
429rate_limit_exceeded请求过于频繁,参考限流说明
500api_error服务端内部错误,可安全重试
503service_unavailable服务暂时不可用,稍后重试

版本变更

文档随 API 版本推进。当前版本 v1.2.0,基础 URL 路径为 /v1

v1.2.0 · 2026-07-15

  • order 对象新增 metadata 自定义字段(最多 20 个)。
  • 列表端点 GET /orders 新增 created_aftercreated_before 时间筛选。

v1.1.0 · 2026-03-01

  • 新增 PATCH /orders/{id},支持订单部分更新。
  • 订单新增 amount_subtotal 字段。

v1.0.0 · 2025-11-01

  • 首次发布:支持创建、列出、查询、删除订单。
  • 引入 Bearer Token 鉴权与每分钟 100 次限流。