SDK(Python / TypeScript)与 REST
SDK 是 REST 的薄封装(每个方法都对应 1 个端点)。共同行为:
- 所有变更调用自动附加
Idempotency-Key(重试安全) - 429 / 5xx 按指数退避自动重试。409(乐观锁)不重试 — 契约是重新读取后重新应用
- 错误连同 problem+json 的
suggested_fix一起异常化(LLM 可以直接读) - 实务 helper:
jobs.wait()/tables.iter_rows()/webhooks.verify_signature()
Python
Section titled “Python”pip install d2b-sdkfrom d2b import D2BClient, ConflictError
client = D2BClient(api_key="d2b_pat_...", base_url="https://d2b.dev")wb = client.workbooks.create(title="monthly")["id"]client.sources.upload(wb, "sales.xlsx", wait=True)
page = client.tables.rows(wb, "sales")try: client.tables.upsert_rows(wb, "sales", rows=[{"product": "apple", "qty": 3}], expected_version=page["edit_version"])except ConflictError: page = client.tables.rows(wb, "sales") # re-read → re-apply资源: workbooks / sources / tables / query / transforms / versions / jobs / reviews / sheets / charts / export / webhooks。
TypeScript
Section titled “TypeScript”npm install d2b-sdkimport { D2BClient } from "d2b-sdk";
const client = new D2BClient({ apiKey: "d2b_pat_...", baseUrl: "https://d2b.dev" });const wb = (await client.workbooks.create({ title: "monthly" })).id as string;await client.sources.upload(wb, fileBlob, { filename: "sales.xlsx", wait: true });const grid = await client.tables.a1(wb, "sales", "A1:D10"); // read in Excel coordinatesawait client.sheets.put(wb, "summary", [ { kind: "heading", text: "Monthly summary" }, { kind: "table_view", table: "sales" },]);const xlsx = await client.sheets.render(wb, "summary");包的发布位置
Section titled “包的发布位置”| 包 | 源码与 Issue | |
|---|---|---|
| Python + CLI | PyPI 上的 d2b-sdk | github.com/600/d2b-sdk(python/) |
| TypeScript | npm 上的 d2b-sdk | github.com/600/d2b-sdk(typescript/) |
两个仓库中的发布名都是 d2b-sdk。Python 的 import 包名和命令都是 d2b(PyPI 上的 d2b 是另一个无关项目),因此用 uvx 时写作 uvx --from d2b-sdk d2b …。GitHub 仓库是只读镜像(SDK 在 D2B 主仓库中开发):那里的 Pull Request 无法合并,欢迎提交 Issue。每次发布会把同一个标签(sdk-py-v* / sdk-ts-v*)同步到镜像,并从镜像发布到两个仓库(npm 带 provenance)。
直接调用 REST
Section titled “直接调用 REST”- Base URL:
https://d2b.dev(路径为/api/v1/...,租户管理为/api/control/...) - 认证:
Authorization: Bearer <PAT>(/api/v1),Account API key(/api/control) - 变更类支持
Idempotency-Keyheader(相同 key + 相同 body 时重放首次响应) - OpenAPI 3.1: openapi.json — API 参考由它生成