首页 › 技术专题 › API对接

包网平台API对接全流程指南

对接延期占项目延期的三成左右,这篇是主要解药。对接前问三个问题、对接中防三个坑、对接后用错误码验收——拿来就能用。

Home › Tech › API Integration

API integration: the full process, stage by stage

Integration slips account for roughly a third of project delay — this page is the main cure. Three questions before, three traps during, error-code acceptance after.

API对接是包网搭建里最依赖外部配合的环节,也是延期重灾区。问题的根源不是技术难,是约定不清。这篇按对接前、中、后三个阶段给清单。

对接前:三个问题必须问

  1. 文档版本号是多少,最后更新在哪天?文档过期是对联调延误贡献最大的单一因素。拿到手第一件事看版本和日期,超过半年没更新的,要求对方重新确认。
  2. 沙箱环境能不能复现生产边界场景?空数据沙箱测不出任何问题。要求提供带边界数据的测试环境,或者至少给一套可导入的种子数据。
  3. 对接人是谁,响应时效什么水平?「有问题联系技术支持」等于没有对接人。要名字、要时区、要响应时效承诺,写进合同更好。

对接中:三个坑

坑一:验签

十个体对接失败的项目里,四个栽在验签。常见原因:参数排序规则不一致(字典序、大小写敏感)、时间戳容差没对齐(建议 ±300 秒)、编码差异(URL encode 的空格处理)。解法是拿对方的示例代码逐字节比对签名串,先对齐再联调。

坑二:幂等

网络超时后重试,是分布式系统的日常。如果接口不幂等,一次重试就是两笔订单。对接前问清楚:这个接口幂等吗?幂等键是什么?答不上来的接口,按不幂等处理——自己生成唯一请求号带上。

坑三:超时与重试

超时时间不能拍脑袋。读取超时建议 3-5 秒,重试 2-3 次,退避间隔翻倍(1s、2s、4s)。重试超过 3 次还没有成功,进人工处理队列,别让机器硬扛。

对接后:错误码验收

层级职责验收标准
HTTP 状态码大类:4xx 客户端错、5xx 服务端错禁止业务错误返回 200
业务错误码细节:六位数字,前三位模块、后三位具体错误错误码表全覆盖,无「未知错误」
错误响应体排查线索code、message、traceId 三字段齐全

验收时专门做一轮错误注入测试:传错参数、带过期签名、模拟超时,看返回是否规范。「成功路径能通」只完成了一半的验收,错误路径才是日常运维要面对的。

错误码之外的三个约定

  • 时间格式:ISO 8601 + UTC,别接受「yyyy-MM-dd」这种没时区的格式。
  • 分页:游标分页优于页码分页,数据量大的列表尤其明显。
  • 回调:回调地址必须可公网访问,回调失败的重试策略要写进文档。

和架构的关系

接口层的重试、熔断、降级策略,属于架构层的议题,展开在高可用架构。安全层面的验签算法与密钥管理,见安全专题。联调周期怎么排缓冲,看周期拆解——那篇里联调计划两周实际五周的数据,就是这份清单想预防的事。

一个真实案例的教训

去年看过一个项目,联调阶段卡了整整四周,原因说出来不值一提:双方的签名算法实现都没错,但一方对参数做 URL encode 时把空格编码成 +,另一方按 RFC 3986 编码成 %20。两行代码的差异,四周的工期。这个案例的通用教训是:联调第一件事不是测业务逻辑,是用最小请求把签名、时间戳、编码这三件基础设施的事对齐。对齐之后再谈功能,效率完全不同。

API integration is the part of a platform build most dependent on someone else's cooperation, and the most reliable source of delay. The root cause is rarely technical difficulty — it is unspecified expectations. This page provides the checklist in three stages: before, during and after.

Before integration: three questions, non-negotiable

  1. What is the documentation version, and when was it last updated? Stale documentation is the single largest contributor to integration slip. Check the version and date first; anything more than six months old should be re-confirmed in writing.
  2. Can the sandbox reproduce production edge cases? An empty sandbox tests nothing. Require an environment with boundary data, or at minimum an importable seed set.
  3. Who is the counterpart, and how fast do they respond? "Contact tech support if you have questions" means there is no counterpart. Get a name, a time zone and a response-time expectation — in the contract if possible.

During integration: three traps

Trap one: signature verification

Of ten failed integrations, four die here. The usual causes: parameter-sorting rules differ (lexicographic order, case sensitivity), timestamp tolerance is misaligned (aim for ±300 seconds), or encoding differs (how URL encoding treats spaces). The fix is unglamorous: compare your signature string against their sample code byte by byte, and align before anything else.

Trap two: idempotency

Retry-after-timeout is daily life in distributed systems. If an endpoint is not idempotent, one retry becomes two orders. Ask before integrating: is this endpoint idempotent, and what is the idempotency key? No answer means treat it as non-idempotent — generate your own unique request ID and send it along.

Trap three: timeouts and retries

Do not guess timeouts. Read timeout of 3-5 seconds, two to three retries, exponential backoff (1s, 2s, 4s). Past three failed retries, route to a human queue — do not let the machine keep insisting.

After integration: acceptance by error codes

LayerResponsibilityAcceptance bar
HTTP statusThe category: 4xx client error, 5xx server errorBusiness errors never return 200
Business codesThe detail: six digits — three for module, three for the errorCode table complete; no "unknown error"
Error bodyThe diagnostic trailcode, message and traceId all present

Run a dedicated fault-injection round at acceptance: malformed parameters, expired signatures, simulated timeouts. "The happy path works" is half the acceptance — the error path is what operations lives with daily.

Three conventions beyond error codes

  • Time format: ISO 8601 in UTC. Reject formats like "yyyy-MM-dd" that carry no zone.
  • Pagination: cursor pagination beats page numbers, especially on large lists.
  • Callbacks: callback URLs must be publicly reachable, and the retry policy for failed callbacks belongs in the documentation.

Where this connects

Retry, circuit-breaking and degradation policies at the interface layer are architecture topics — see high-availability architecture. Signature algorithms and key management are covered in the security topic. For how much buffer an integration stage needs, see the timeline breakdown — the two-weeks-planned-five-weeks-actual figure there is exactly what this checklist exists to prevent.

A lesson from a real project

One project we watched last year lost four full weeks in integration for a reason that sounds trivial: both sides implemented the signature algorithm correctly, but one URL-encoded spaces as "+" and the other followed RFC 3986 and encoded them as "%20". Two characters of difference, four weeks of calendar. The general lesson: the first integration task is not business logic. It is aligning the plumbing — signatures, timestamps, encoding — with a minimal request. Only after that do functional tests run at a sane pace.

正在对接某个具体接口?

把接口文档发来,我们按清单逐条过。

立即联系我们

Mid-integration on a specific endpoint?

Send the documentation over — we will walk it against the checklist.

Contact us