Usage Statistics
Relevant source files
The following files were used as context for generating this wiki page:
- src/main/java/com/resend/services/usage/Usage.java
- src/main/java/com/resend/services/usage/model/UsageResponse.java
- src/main/java/com/resend/services/usage/model/UsageQuota.java
- src/main/java/com/resend/services/usage/model/UsageEmails.java
- src/main/java/com/resend/services/usage/model/UsageAiCredits.java
- src/main/java/com/resend/services/usage/model/UsageAutomationRuns.java
- src/test/java/com/resend/services/usage/UsageTest.java
The Usage Statistics module provides comprehensive data regarding a caller's account-level consumption and resource quotas. It enables developers to monitor real-time limits for various Resend services including emails, contacts, segments, and AI credits.
The system retrieves these statistics through a dedicated service class that interacts with the Resend API, returning a structured response that encapsulates limits and current usage across multiple categories.
Sources: src/main/java/com/resend/services/usage/Usage.java:11-13, src/main/java/com/resend/services/usage/model/UsageResponse.java:6-8
Service Architecture
The Usage class handles communication with the Resend API. It extends BaseService and utilizes an IHttpClient to perform requests. The primary operation is the get() method, which executes a GET request to the /usage endpoint.
The sequence above illustrates the flow from a service call to the final deserialized response.
Sources: src/main/java/com/resend/services/usage/Usage.java:38-40, src/test/java/com/resend/services/usage/UsageTest.java:49-56
Data Models
The usage response is highly structured, categorizing statistics into specific resource types. The root object is UsageResponse.
UsageResponse Fields
| Field | Type | Description |
|---|---|---|
object | String | The object type, always "usage". |
emails | UsageEmails | Email usage for daily and monthly periods. |
contacts | UsageQuota | Current count and limit for contacts. |
segments | UsageQuota | Current count and limit for segments. |
broadcasts | UsageQuota | Current count and limit for broadcasts. |
ai_credits | UsageAiCredits | AI credit consumption and schedule. |
automation_runs | UsageAutomationRuns | Automation execution statistics. |
domains | UsageQuota | Domain resource allocation. |
rate_limit | UsageRateLimit | Current API rate limit and duration. |
Sources: src/main/java/com/resend/services/usage/model/UsageResponse.java:11-40, src/test/java/com/resend/services/usage/UsageTest.java:63-100
Resource Quota Structure
The UsageQuota class is a generic container for resources that follow a simple "used vs limit" pattern. This is applied to contacts, segments, broadcasts, and domains.
Resource Specific Details
Email Usage
Email statistics are split into daily and monthly periods within the UsageEmails object. Each period tracks used counts, limits, sent amounts, and received amounts, along with a reset timestamp.
Sources: src/main/java/com/resend/services/usage/model/UsageEmails.java:8-14, src/test/java/com/resend/services/usage/UsageTest.java:23-26
AI Credits
The UsageAiCredits object tracks AI usage and replenishment schedules.
used: Amount of credits consumed.limit: Maximum allowed credits (can benullif unlimited).next_increase_at: Timestamp for the next scheduled credit increase.
Sources: src/main/java/com/resend/services/usage/model/UsageAiCredits.java:25-50
Automation Runs
The UsageAutomationRuns object monitors the execution of automated workflows.
used: Number of automation runs triggered.limit: Maximum allowed runs.resets_at: Timestamp when the run count resets.
Sources: src/main/java/com/resend/services/usage/model/UsageAutomationRuns.java:25-50
Implementation Example
You can retrieve account usage statistics by initializing the Usage service and calling the get method.
Usage usageService = new Usage("re_123456789");
UsageResponse stats = usageService.get();
System.out.println("Contacts Used: " + stats.getContacts().getUsed());
System.out.println("Monthly Email Limit: " + stats.getEmails().getMonthly().getLimit());
Sources: src/main/java/com/resend/services/usage/Usage.java:22-30, src/test/java/com/resend/services/usage/UsageTest.java:58-60
Conclusion
The Usage Statistics module serves as a critical monitoring component for applications using Resend. By providing granular data on email volume, contact limits, and credit consumption, it allows developers to implement proactive quota management and avoid service interruptions due to limit exhaustion.