跳转到主要内容
Rust SDK 提供方便的异步优先访问,用于从 Rust 应用程序访问 Dodo Payments REST API。它提供了强类型请求和响应、内置分页助手以及可配置的超时和环境。

安装

使用 Cargo 将 SDK 添加到您的项目中:
或者手动将其添加到您的 Cargo.toml 中:
The SDK requires Rust 1.75 or later, leveraging modern async features for optimal performance.

快速开始

客户端默认从 DODO_PAYMENTS_API_KEY 环境变量中读取您的 API 密钥。初始化客户端并创建您的第一个结账会话:
Always store your API keys securely using environment variables. Never hardcode them in your source code.

核心功能

Built on Tokio and reqwest with async/await support throughout Strongly typed requests and responses for compile-time safety Stream every item across pages or advance one page at a time Configure environments, timeouts, and base URLs per client

配置

环境变量

默认情况下,Client::from_env()DODO_PAYMENTS_API_KEY 环境变量中读取您的 API 密钥,并使用默认基本 URL,除非您设置 DODO_PAYMENTS_BASE_URL
您也可以显式配置客户端。Client::new 返回一个 Result,因此在返回 dodopayments::Result 的函数中与 ? 一起解包它:

环境

默认基本 URL 是 https://live.dodopayments.com。使用 Environment 枚举选择其他环境,而不是硬编码 URL:
要通过 from_env() 继续从 DODO_PAYMENTS_API_KEY 读取 API 密钥,同时将目标放在非默认环境中,请在配置中覆盖它:

超时

默认请求超时为 30 秒。为每个客户端覆盖它:

常用操作

创建结账会话

生成一个结账会话:

管理客户

创建和检索客户信息:

处理订阅

创建和管理定期订阅:
billing requires at minimum the two-letter ISO country code. customer is a CustomerRequest enum — pass AttachExisting for an existing customer or New for a new one. Amount fields such as product_price are in the lowest currency denomination (e.g., 2500 = $25.00 USD).

基于使用量的计费

引入使用事件

跟踪自定义事件:

列出使用情况事件

分页

List endpoints return a typed page whose items field holds the current page of results. Stream every item across all pages with into_stream:
Or advance one page at a time with get_next_page:

错误处理

Every method returns a dodopayments::Result<T>. Failures are represented by the dodopayments::Error enum. Match on it to handle API errors distinctly from transport errors:

未记录的端点

要调用尚未作为类型化方法公开的端点,请使用应用身份验证和基本 URL 的低级 request 构建器:

资源

View source code and contribute View the published crate and versions Complete API documentation Get help and connect with developers

支持

需要 Rust SDK 的帮助吗?

贡献

欢迎贡献!查看 contributing guidelines 开始。
最后修改于 2026年6月26日