Events, Logs, and Usage
Relevant source files
The following files were used as context for generating this wiki page:
The Resend PHP SDK provides modules to manage custom events, monitor account usage, and handle webhook interactions. These features allow you to trigger automations, track email and contact limits, and verify incoming data from Resend's infrastructure.
Events Management
The Event service manages the lifecycle of custom events used to trigger automations within the Resend platform. You can create events with specific schemas, update their definitions, and send instances of these events associated with a contact or email address.
Event Operations
The SDK supports standard CRUD operations for event definitions and a specialized send method for triggering those events.
| Operation | Method | Description |
|---|---|---|
| Get | get(string $identifier) | Retrieves an event by its ID or name. |
| Create | create(array $parameters) | Defines a new event with an optional schema (string, number, boolean, or date). |
| List | list(array $options) | Returns a collection of all defined events. |
| Update | update(string $id, array $params) | Modifies the schema of an existing event. |
| Remove | remove(string $identifier) | Deletes an event definition. |
| Send | send(array $parameters) | Triggers an event for a specific email or contact_id. |
Sources: src/Service/Event.php:14-114, tests/Service/Event.php:7-104
Event Data Flow
When you send an event, the SDK validates that either a contact_id or an email is provided, but not both. It then dispatches a POST request to the /events/send endpoint.
Account Usage Tracking
The Usage service provides a real-time overview of account consumption across various metrics. This includes email volume, contact counts, domain limits, and rate limits.
Usage Metrics Structure
The usage data structure organizes limits and current consumption into specific categories.
| Category | Fields | Description |
|---|---|---|
| Emails | used, limit, sent, received, resets_at | Tracks daily and monthly email quotas. |
| Contacts | used, limit | Tracks total stored contacts. |
| Automation | used, limit, resets_at | Tracks automation runs and reset cycles. |
| Rate Limit | limit, duration | Defines the API request threshold (e.g., 10 per 1000ms). |
| AI Credits | used, limit, next_increase_at | Tracks consumption of AI-related features. |
Sources: tests/Fixtures/Usage.php:5-52, tests/Service/Usage.php:7-32
Webhook Event Verification
While the Event service handles outbound triggers, the SDK also includes utilities for verifying inbound Webhook events sent by Resend. This ensures the authenticity of the payload using a signing secret.
Verification Logic
The WebhookSignature::verify method validates three critical headers: svix-id, svix-timestamp, and svix-signature. It compares the provided signature against a local HMAC SHA256 hash generated from the payload and the signing secret.
- Secret Normalization: If the secret starts with
whsec_, the prefix is removed. - Timestamp Validation: The
svix-timestampmust be within a tolerance window (default 300 seconds) to prevent replay attacks. - Signature Matching: The system iterates through provided signatures to find a
v1version that matches the expected hash.
Sources: src/WebhookSignature.php:9-52, tests/WebhookSignature.php:13-78
Summary
The Events, Logs, and Usage components provide the necessary tools for both proactive interaction and reactive monitoring. Developers use the Event service to drive business logic through automations, the Usage service to stay within platform quotas, and the WebhookSignature utility to secure incoming data streams from Resend.