跳转到内容
simail文档v0

接入

SDK(TypeScript / Python)

simail API 的官方 TypeScript 与 Python 客户端:安装、配置、幂等发送和错误处理。

docs.simail.ai/zh/sdk

两个 SDK 都是盖在 REST API 上的薄客户端,没有额外运行时依赖。它们从你的代码读 key(从 SIMAIL_API_KEY 加载),替你设置头,并把 problem+json 错误变成带 agent 所需字段的异常。资源方法由 GET https://api.simail.ai/v0/openapi.json 的 OpenAPI 文档生成,所以始终和 API 参考一致;每个客户端也提供通用的 request(),可以打任意端点。

两个 SDK 还没有发布到 npm 和 PyPI,发布与否还没定,所以现在 npm install @simail/sdk 和 pip install simail 都装不上。在那之前:

  • 直接用 curl 或 fetch 调 REST API。每个 SDK 方法就是 API 参考里的一个 HTTP 请求。
  • 手上有 simail 源码仓库的,按下面的步骤从仓库构建。

在 simail 仓库里打出 tarball(Node 22.18 或更高,npm 11),再装进你的项目:

# 在 simail 仓库里
npx -y npm@11.21.1 ci
npx -y npm@11.21.1 pack -w @simail/sdk # 生成 simail-sdk-0.0.0.tgz(编译好的 JavaScript 和类型)
# 在你的项目里
npm install /path/to/simail-sdk-0.0.0.tgz

可在 Node 22 及以后、Cloudflare Workers 和其他带标准 fetch 的运行时使用。没有运行时依赖。在服务器上用,不要放进浏览器:它需要你的 API key。

import { Simail, SimailApiError } from "@simail/sdk";
const simail = new Simail({ apiKey: process.env.SIMAIL_API_KEY });
// Call this first: during the private beta, sign-up is closed.
const beta = await simail.beta();
// Any endpoint through request(method, path, body?, { idempotencyKey? })
const inbox = await simail.request("POST", "/v0/inboxes", { username: "shop-helper", client_id: "shop-helper" }, { idempotencyKey: "create-shop-helper" });
try {
await simail.request(
"POST",
"/v0/inboxes/shop-helper@simail.email/messages/msg_…/reply",
{ text: "Thanks! Your order shipped today." },
{ idempotencyKey: "reply-msg_…-1" },
);
} catch (e) {
if (e instanceof SimailApiError && !e.retryable) {
// e.status, e.code ("thread_locked", "send_blocked", …), e.requestId, e.body (full problem+json)
}
throw e;
}
选项默认
apiKey无(没有 key 时只能调公开端点:GET /v0/beta(beta())、POST /v0/waitlist、POST /v0/agent/sign-up、GET /v0/health、GET /v0/openapi.json;其余操作都要 API key)
baseUrlhttps://api.simail.ai
fetch全局 fetch(代理或测试时传入你自己的)
userAgentsimail-sdk-ts/<version>

new Simail() 的类型里只有 beta() 和 request()。想带类型地调用生成的资源方法,请用 createSimail(),它返回同一个客户端,类型是 SimailClient。方法名取自 OpenAPI 的 operationId:TypeScript 是 camelCase(listInboxes、getInbox("inb_…")),Python 是 snake_case(list_inboxes、get_inbox("inb_…"))。参数顺序:先路径参数,写操作再跟请求体,最后是 options(TS:{ query, idempotencyKey };Python:query= 和 idempotency_key=)。query 里的列表值(例如 labels)会用逗号拼成一个值发出(types=a,b);空字符串、.、.. 作为路径参数会在发请求前被拒绝。

import { createSimail } from "@simail/sdk";
const simail = createSimail({ apiKey: process.env.SIMAIL_API_KEY });
const { inboxes } = (await simail.listInboxes({ query: { limit: 10 } })) as { inboxes: unknown[] };
await simail.listInboxThreads("inb_…", { query: { labels: ["urgent", "billing"] } }); // 两个标签同时满足(AND)

从 simail 仓库安装:

# 在 simail 仓库里
pip install ./packages/sdk-py

Python 3.10 或更高,只用标准库。

import os
from simail import Simail, SimailApiError
simail = Simail(api_key=os.environ["SIMAIL_API_KEY"])
simail.beta()
try:
simail.request(
"POST",
"/v0/inboxes/shop-helper@simail.email/messages/msg_…/reply",
{"text": "Thanks! Your order shipped today."},
idempotency_key="reply-msg_…-1",
)
except SimailApiError as e:
if not e.retryable:
print(e.status, e.code, e.request_id) # e.body has the full problem+json
raise

Simail(api_key=None, base_url="https://api.simail.ai");请求 30 秒后超时。

两个 SDK 对任何非 2xx 响应都抛 SimailApiError:

字段来自
statusHTTP 状态
codeproblem+json 的 code(见错误码)
retryableproblem+json 的 retryable
requestId / request_idrequest_id,也在 x-request-id 头里
body整个 problem+json 对象,有的话包括 human_message、reasons 或 reason

SDK 不会自己重试,所以发送不会在你背后再发一次。只有 retryable 为真时才重试,等过 Retry-After(TS 用 e.retryAfterSeconds,Python 用 e.retry_after_seconds;都先读响应头,没有再用 body 的 retry_after_seconds),并用同一把幂等键。见幂等。

不要把 key 写死,也不要通过提示词传递。从环境变量或密钥管理器加载,并给每个 agent 自己的、作用域落在邮箱上的 key(认证)。