Configuration & Environment Variables
Relevant source files
The following files were used as context for generating this wiki page:
Configuration and environment variables manage how the Resend Rust SDK interacts with the Resend API. The system uses a centralized Config struct to store authentication keys, base URLs, HTTP clients, and rate-limiting states. You can initialize these settings through environment variables for zero-config defaults or use the ConfigBuilder for granular control.
The SDK prioritizes security by ensuring sensitive credentials like API keys are not accidentally exposed in debug logs or error messages. It also implements automated rate limiting to prevent 429 Too Many Requests errors from the server.
Sources: src/config.rs:101-140, README.md:31-40, src/lib.rs:7-25
Environment Variables
The SDK checks specific environment variables during initialization to configure the default client behavior.
| Variable | Requirement | Description | Default |
|---|---|---|---|
RESEND_API_KEY | Required | The API key used for Bearer authentication. | N/A |
RESEND_BASE_URL | Optional | Overrides the default Resend API endpoint. | https://api.resend.com |
RESEND_RATE_LIMIT | Optional | Sets maximum requests per second (Async only). | 9 |
Sources: README.md:31-40, src/config.rs:113-125
Base URL Resolution
If you provide a base_url through the ConfigBuilder, it takes precedence over the RESEND_BASE_URL environment variable. The SDK ensures that any path prefix in the base URL (such as a proxy path) is preserved when constructing final request URLs.
Sources: src/config.rs:106-118, src/config.rs:205-212
Configuration Architecture
The architecture centers on the Config struct, which is wrapped in an Arc and shared across all service modules (Emails, Contacts, etc.).
The diagram shows the flow from environment variables to the shared configuration used by SDK services.
Sources: src/config.rs:43-62, src/lib.rs:95-115
Config Struct Components
The Config struct holds the following internal state:
- user_agent: A string formatted as
crate_name/crate_version(e.g.,resend-rs/0.35.0). - api_key: The Bearer token for authentication.
- base_url: The target
reqwest::Url. - client: The underlying
reqwest::Client. - limiter: A
governorrate limiter used in asynchronous contexts.
Sources: src/config.rs:82-99, src/config.rs:119
Client Customization
You can use ConfigBuilder to inject custom HTTP configurations, which is useful for setting timeouts or using proxies.
let http_client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()
.unwrap();
let config = ConfigBuilder::new("re_123456789")
.base_url("https://my-proxy.com".parse().unwrap())
.client(http_client)
.build();
let resend = Resend::with_config(config);
Sources: src/config.rs:24-41, src/config.rs:65-79
Rate Limiting and Quotas
The SDK manages request flow to stay within Resend's API limits. By default, it allows 9 requests per 1.1 seconds. This provides a buffer against the server-side limit of 10 requests per second.
Rate Limiting Logic Flow
The sequence illustrates the internal rate-limiting check before an HTTP request is dispatched.
Sources: src/config.rs:125-132, src/config.rs:145-150, src/lib.rs:7-15
Handling 429 Errors
If the server returns a 429 Too Many Requests status, the SDK parses the following headers into an Error::RateLimit variant:
ratelimit-limitratelimit-remainingratelimit-reset
Sources: src/config.rs:154-173, src/lib.rs:328-334
Feature Flags
Configuration behavior changes based on the feature flags enabled in Cargo.toml.
| Feature | Description |
|---|---|
blocking | Switches reqwest to the blocking client. Rate limiting is disabled in this mode. |
native-tls | Uses system-native TLS (Default). |
rustls-tls | Uses the rustls library for TLS. |
Sources: Cargo.toml:23-27, README.md:24-29, CONTRIBUTING.md:12-14
Summary
Configuration in the Resend Rust SDK is primarily managed through the Config struct, which can be initialized via environment variables (RESEND_API_KEY, RESEND_BASE_URL) or programmatically via ConfigBuilder. The SDK provides built-in protections including automated rate limiting for async clients and redacted debug formatting for API keys to ensure secure and reliable operation.