API Keys
Relevant source files
The following files were used as context for generating this wiki page:
API Keys manage authentication and authorization within the Resend PHP SDK. They allow developers to create, list, update, and remove keys used to interact with the Resend API. Each key is represented as both a resource object for management and a value object for secure transport in request headers.
Architecture and Data Models
The system uses three primary classes to handle API key logic:
Resend\ValueObjects\ApiKey: A value object that wraps the raw string token.Resend\ApiKey: A resource object representing a key's metadata (ID, name, creation date).Resend\Service\ApiKey: A service class providing methods to interact with the API endpoints.
Sources: src/ApiKey.php:11-15, src/ValueObjects/ApiKey.php:7-10, src/Service/ApiKey.php:9-11
API Key Data Structure
An API key resource contains metadata returned by the Resend API.
| Property | Type | Description |
|---|---|---|
id | string | Unique identifier for the API key. |
name | string | The display name of the API key. |
created_at | string | Timestamp indicating when the key was created. |
token | string | null | The secret token used for authentication (only present on creation). |
Sources: src/ApiKey.php:7-10, tests/Fixtures/ApiKey.php:5-9
Relationship Diagram
The following diagram shows the relationship between the Service, the Transporter, and the resulting Resource objects.
API Key Service Operations
The Resend\Service\ApiKey class manages the lifecycle of keys via HTTP requests. It uses a Transporter to send payloads to the api-keys endpoint.
Operation Flow
When a service method is called, it constructs a Payload and passes it to the transporter.
Service Methods
| Method | Parameters | Return Type | Description |
|---|---|---|---|
create | array $parameters | Resend\ApiKey | Creates a new API key. |
list | array $options | Resend\Collection | Lists keys with optional pagination (limit, before, after). |
update | string $id, array $params | Resend\ApiKey | Updates the name of an existing key. |
remove | string $id | Resend\ApiKey | Deletes a specific API key. |
Sources: src/Service/ApiKey.php:17-64
Authentication and Headers
The Resend\ValueObjects\ApiKey is specifically used to construct the Authorization header for all outgoing API requests. The SDK formats the key as a Bearer token.
$headers = Headers::withAuthorization(ApiKey::from('re_123'));
// Results in: ['Authorization' => 'Bearer re_123']
Sources: src/ValueObjects/ApiKey.php:25-28, tests/ValueObjects/Transporter/Headers.php:7-13
Header Generation Flow
Error Handling
If an operation is attempted on a non-existent key, the service throws a Resend\Exceptions\ErrorException. This typically occurs during update or remove operations if the ID is invalid.
Sources: tests/Service/ApiKey.php:33-46
Summary
The API Key module provides a structured way to manage access tokens within the Resend ecosystem. By separating the raw token into a ValueObject and the key metadata into a Resource, the SDK ensures that authentication logic is decoupled from resource management. Operations are centralized in the ApiKey service, which handles the communication with the Resend API endpoints.