SDK Architecture & Interfaces
Relevant source files
The following files were used as context for generating this wiki page:
The Resend .NET SDK provides a structured wrapper for the Resend API, enabling .NET applications to send emails and manage communications resources. The architecture relies on an interface-driven design that promotes testability and integrates with the standard .NET Dependency Injection (DI) ecosystem.
Applications interact primarily with the IResend interface, which defines the available service operations. The implementation, ResendClient, uses a partial class structure to organize API endpoints into logical modules like Emails, Contacts, and Webhooks.
Sources: README.md:1-50, src/Resend/IResend.cs:1-10
Core Interface: IResend
The IResend interface acts as the central contract for the SDK. It defines asynchronous methods for every supported Resend API capability.
Functional Modules
The interface groups methods into specific functional regions:
- Email: Handles single, batch, and scheduled email delivery.
- Contacts & Segments: Manages audience data and recipient grouping.
- Domains: Controls domain verification and DNS settings.
- Templates: Manages reusable email content and versioning.
- Webhooks: Configures and monitors event delivery to external endpoints.
Sources: src/Resend/IResend.cs:6-1240
Request Execution Flow
The SDK follows a consistent pattern for executing API requests. It constructs an HttpRequestMessage, applies the necessary JSON content or query parameters, and executes the request through a centralized handler.
This diagram shows the logic flow from a high-level interface call to the internal execution of an HTTP request.
Sources: src/Resend/ResendClient.Templates.cs:9-40, src/Resend/ResendClient.Contacts.cs:10-35
Client Implementation
The ResendClient class implements IResend. Developers distribute the implementation across several files using the partial keyword to maintain code readability and separation of concerns.
| Component | Responsibility | Source File |
|---|---|---|
| Templates | CRUD operations for email templates and publishing versions. | ResendClient.Templates.cs |
| Topics | Management of subscription topics and contact preferences. | ResendClient.Topics.cs |
| Webhooks | Configuration of webhook endpoints and event replay. | ResendClient.Webhooks.cs |
| Contacts | Addition, retrieval, and segmenting of individual contacts. | ResendClient.Contacts.cs |
Sources: src/Resend/ResendClient.Templates.cs:1, src/Resend/ResendClient.Topics.cs:1, src/Resend/ResendClient.Webhooks.cs:1, src/Resend/ResendClient.Contacts.cs:1
Idempotency Support
The SDK supports idempotency for sensitive operations like email sending and draft creation. You provide an idempotencyKey to ensure that retries do not result in duplicate actions.
Task<ResendResponse<Guid>> EmailSendAsync(string idempotencyKey, EmailMessage email, CancellationToken cancellationToken = default);
Sources: src/Resend/IResend.cs:33-47
Data Modeling
The SDK defines strongly-typed classes for both request payloads and response objects. These models use System.Text.Json attributes to map C# properties to the Resend API's JSON format.
Template Model Structure
The Template class represents the detailed state of an email template, including its metadata and content.
This diagram illustrates the relationship between a full Template object and its summarized version used in lists.
Sources: src/Resend/Template.cs:6-65, src/Resend/TemplateSummary.cs:6-45
Dependency Injection & Configuration
The SDK integrates with IServiceCollection through the AddResend extension method. This allows the IResend interface to be injected into application services, controllers, or minimal APIs.
Setup Example
builder.Services.AddResend( o =>
{
o.ApiToken = Environment.GetEnvironmentVariable( "RESEND_APITOKEN" )!;
} );
Sources: README.md:45-53
Usage in Components
The sequence diagram demonstrates the interaction between an application, the SDK interface, and the remote Resend API.
Sources: README.md:60-85, src/Resend/IResend.cs:11-25
Summary
The SDK Architecture centers on the IResend interface and a modular ResendClient implementation. It provides a robust, type-safe environment for managing Resend resources. By utilizing partial classes and standardized response wrappers, the SDK maintains consistency across diverse API features while simplifying integration through standard .NET patterns.