Getting Started with Resend Java
Relevant source files
The following files were used as context for generating this wiki page:
Resend Java SDK provides a structured way to integrate Resend API services into Java applications. The SDK manages authentication, HTTP communication, and data serialization for various modules like emails, contacts, and domains.
Sources: README.md:1-5, src/main/java/com/resend/Resend.java:31-36
Installation
Add the Resend Java dependency to your project build file to include the SDK.
Gradle
implementation 'com.resend:resend-java:+'
Maven
<dependency>
<groupId>com.resend</groupId>
<artifactId>resend-java</artifactId>
<version>LATEST</version>
</dependency>
Sources: README.md:9-25
Architecture and Core Components
The Resend class acts as the central entry point for all API interactions. It maintains a shared HTTP client across all sub-services to optimize connection pooling.
Component Relationship
The following diagram shows how the Resend class provides access to specific service modules.
The Resend instance orchestrates communication between the user code and the specific service implementations.
Sources: src/main/java/com/resend/Resend.java:38-164
Data Flow for API Requests
When you call a service method, the SDK serializes the options, attaches authentication headers, and executes the request via the HTTP client.
Configuration and Setup
You must obtain an API key from the Resend Dashboard before initializing the client. The SDK supports basic initialization and advanced configuration via a builder.
Initialization Options
| Configuration | Description | Default Value |
|---|---|---|
apiKey | The authentication token for Resend services. | Required |
baseUrl | The target API endpoint. | https://api.resend.com |
connectTimeout | Max time to establish a connection. | 10 Seconds |
readTimeout | Max time to wait for data. | 10 Seconds |
User-Agent | SDK identifier header. | resend-java/{version} |
Sources: src/main/java/com/resend/Resend.java:185-263, src/main/java/com/resend/core/net/impl/HttpClient.java:30-33, src/main/java/com/resend/core/SdkVersion.java:13-25
Customizing the Client
Use the Resend.builder() to configure proxies or custom timeouts.
Resend resend = Resend.builder()
.apiKey("re_123")
.connectTimeout(Duration.ofSeconds(5))
.readTimeout(Duration.ofSeconds(30))
.build();
Sources: README.md:65-74, src/main/java/com/resend/Resend.java:60-67
Service Modules
The SDK organizes functionality into logical modules accessible through the main Resend instance.
- Emails: Handles sending single and batch emails.
- Contacts: Manages subscribers, segments, and contact imports.
- Templates: Controls reusable email content and versions.
- Domains: Validates and manages sending domains.
- Automations: Orchestrates automated workflows and runs.
Sources: src/main/java/com/resend/Resend.java:75-183, src/main/java/com/resend/services/automations/Automations.java:15-20
Basic Usage Example
The following code demonstrates sending a basic email using the SDK.
Resend resend = new Resend("re_123");
CreateEmailOptions params = CreateEmailOptions.builder()
.from("Me <me@example.io>")
.to("you@example.com")
.subject("Hello from Java!")
.text("Hello, world!")
.build();
try {
CreateEmailResponse data = resend.emails().send(params);
System.out.println("Email ID: " + data.getId());
} catch (ResendException e) {
e.printStackTrace();
}
Sources: README.md:32-58
The Resend Java SDK streamlines API integration by providing a type-safe, builder-based interface for the Resend platform. It ensures efficient network usage through a shared HTTP client while allowing deep customization for enterprise requirements.