API Keys Management
Relevant source files
The following files were used as context for generating this wiki page:
The API Keys Management system enables you to create, list, update, and remove authentication tokens for the Resend API. This system manages the lifecycle of credentials that grant programmatic access to project resources.
The ApiKeys service provides a structured interface within the Go SDK to interact with the /api-keys REST endpoints. It supports both standard execution and context-aware requests to handle timeouts or cancellations.
Sources: api_keys.go:37-49, examples/api_keys.go:14-20
Architecture and Service Interface
The ApiKeysSvc interface defines the contract for key management operations. The implementation, ApiKeysSvcImpl, uses an internal client to perform HTTP requests.
Service Interface Structure
The diagram above shows the relationship between the service interface and its implementation.
Sources: api_keys.go:37-51
Key Operations
Creating API Keys
You create a new API key by providing a name and optional permissions or domain restrictions. The response includes a token which is only visible at the time of creation.
| Parameter | Type | Description |
|---|---|---|
Name | string | The display name of the API key |
Permission | string | Optional access level (e.g., restricted) |
DomainId | string | Optional restriction to a specific domain |
Sources: api_keys.go:8-12, examples/api_keys.go:21-25
Listing and Pagination
The system retrieves all API keys associated with the project. It supports cursor-based pagination using the ListOptions structure.
The flow shows how pagination parameters are applied to the request path.
| Field | Type | Description |
|---|---|---|
Data | []ApiKey | Array of API key objects |
HasMore | bool | Indicates if more records exist |
Object | string | The type of object returned (list) |
Sources: api_keys.go:19-23, api_keys.go:80-84, examples/with_pagination.go:42-46
Updating and Deleting
You update an API key's metadata or revoke it permanently. Updating is restricted to changing the display name.
- Update: Performs a
PATCHrequest to/api-keys/{id}. - Remove: Performs a
DELETErequest to/api-keys/{id}and returns a boolean status.
Sources: api_keys.go:25-31, api_keys.go:127-133, api_keys.go:150-155
Data Models
The system uses specific structures for requests and responses to ensure type safety.
The ER diagram represents the data structures used in API Key operations.
Sources: api_keys.go:14-17, api_keys.go:33-35, api_keys_test.go:58-65
Error Handling
The service returns specific sentinel errors if a request cannot be initialized.
| Error Variable | Description |
|---|---|
ErrFailedToCreateApiKeysCreateRequest | Failed to initialize the POST request |
ErrFailedToCreateApiKeysListRequest | Failed to initialize the GET request |
ErrFailedToCreateApiKeysRemoveRequest | Failed to initialize the DELETE request |
ErrFailedToCreateApiKeysUpdateRequest | Failed to initialize the PATCH request |
Sources: errors.go:62-67
Implementation Example
The following code demonstrates the standard lifecycle of an API key within the SDK.
// Create key
params := &resend.CreateApiKeyRequest{Name: "production-key"}
resp, _ := client.ApiKeys.Create(params)
// List keys
keys, _ := client.ApiKeys.List()
// Remove key
success, _ := client.ApiKeys.Remove(resp.Id)
Sources: examples/api_keys.go:21-50
Summary
API Keys Management provides the foundational security layer for the SDK. It allows developers to programmatically rotate credentials, restrict key scope to specific domains, and monitor key usage through the last_used_at timestamp. All operations support Go's context package for robust request lifecycle management.
Sources: api_keys.go:33-51, api_keys_test.go:121-125