C# SDK
Tomba NuGet 包是 Tomba API 的官方 .NET 客户端。每个 API 功能区对应一个服务类,每个方法发送 1 个请求,并返回原始的 HttpResponseMessage。
要求
- .NET 10。该包的 1.0.1 版本仅面向
net10.0。 Newtonsoft.Json,作为依赖项安装。
安装
Code
这些类位于 Tomba 命名空间中。
认证
在 API keys 页面创建 API 密钥并复制您的 Secret,然后将它们都导出:
Code
SDK 不会读取环境变量。将这些值传给 SetKey() 和 SetSecret(),客户端会将它们作为 X-Tomba-Key 和 X-Tomba-Secret 请求头发送。每个 setter 在每个客户端上只能调用一次:第二次调用会抛出 ArgumentException。有关密钥过期和轮换,请参阅认证。
首个请求
验证一个邮箱地址。
Code
该示例是一个使用顶级语句的 dotnet new console 程序。方法返回未经解析的 HttpResponseMessage,因此需要您自行读取并解析 JSON 响应体。API 会将有效负载包装在 data 对象中(响应格式);验证状态位于 data.email.status。
错误
当 API 返回 400 或更高的状态码时,以及请求失败时,SDK 会抛出 TombaException。对于 API 错误,该异常是嵌套的:外层 TombaException 的 Message 是 API 的 errors.message,其 InnerException 是一个 TombaException,其中 Code 为 HTTP 状态码,Response 为原始响应体。
Code
如果请求在收到响应之前就已失败,inner 为 null。原始 Response 是 JSON 错误响应体;其中每个 errors.type 的说明请参阅错误。
速率限制和重试
SDK 不会重试。Client.ParseRateLimit(response) 会将成功响应的速率限制响应头读取到一个 RateLimit 中,它包含 int? 类型的属性 SecondRateLimit、MinuteRateLimit、DailyRateLimit、MinuteRequestLeft、DailyRequestLeft、MinuteResetSeconds、DailyResetSeconds 和 RetryAfter,以及 string 类型的属性 RateLimitPolicy 和 RateLimitValue。
收到 429 时会抛出 TombaException,但不包含响应头。按照处理 429 响应中的说明,等待一段时间后再重试。
请求在 120 秒后超时。
方法参考
使用客户端创建各个服务,例如 new Verifier(client)。每个方法都返回 Task<HttpResponseMessage>。
| 方法 | 端点 |
|---|---|
Account.GetAccount() | GET /me |
Usage.GetUsage() | GET /usage |
Logs.GetLogs(int? page = null, int? limit = null) | GET /logs |
Format.EmailFormatAsync(string domain) | GET /email-format |
Count.EmailCount(string domain) | GET /email-count |
Verifier.EmailVerifier(string email, string webhookUrl = null) | GET /email-verifier |
Sources.EmailSources(string email) | GET /email-sources |
Phone.FinderAsync(Dictionary<string, object> parameters, string webhookUrl = null) | GET /phone-finder |
Phone.ValidatorAsync(string phone, string countryCode = null) | GET /phone-validator |
Status.DomainStatus(string domain) | GET /domain-status |
Status.AutoComplete(string query) | GET /domain-suggestions |
Keys.GetKeys() | GET /keys |
Keys.GetKey(string id) | GET /keys/{id} |
Keys.CreateKey() | POST /keys |
Keys.ResetKey(string id) | PUT /keys/{id} |
Keys.DeleteKey(string id) | DELETE /keys/{id} |
Leads.ListLeadsAsync(int? page = 1, int? limit = 10, string domain = null) | GET /leads |
Leads.GetLeadAsync(string id) | GET /leads/{id} |
Leads.CreateLeadAsync(Dictionary<string, object> data) | POST /leads |
Leads.UpdateLeadAsync(string id, Dictionary<string, object> data) | PUT /leads/{id} |
Leads.DeleteLeadAsync(string id) | DELETE /leads/{id} |
LeadsLists.GetLists() | GET /leads_lists |
LeadsLists.DeleteListId(string id) | DELETE /leads_lists/{id} |
Bulk.ListAsync(string type, Dictionary<string, object> parameters = null) | GET /bulk/{type} |
Bulk.CreateAsync(string type, Dictionary<string, object> data) | POST /bulk/{type} |
Bulk.GetAsync(string type, string id) | GET /bulk/{type}/{id} |
Bulk.ProgressAsync(string type, string id) | GET /bulk/{type}/{id}/progress |
Bulk.DownloadAsync(string type, string id) | GET /bulk/{type}/{id}/download |
Bulk.RenameAsync(string type, string id, string name) | PUT /bulk/{type}/{id}/rename |
type 是批量类型中列出的 {type} 路径段。CreateAsync 发送 JSON 请求体,因此无法创建需要上传 CSV 的任务,例如邮箱查找器任务。
源代码
github.com/tomba-io/csharp(Apache-2.0)。版本说明见 CHANGELOG.md。