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 API keys to authenticate communications with the Resend API. These keys control access levels and can be restricted to specific domains for enhanced security.
Sources: resend/api_keys/_api_keys.py:89-91, resend/init.py:228
System Architecture
The API Keys module consists of a main ApiKeys class that interfaces with the Resend REST API using both synchronous and asynchronous request handlers. The system uses specific TypedDict structures for parameter validation and BaseResponse subclasses for structured output.
The diagram above illustrates the relationship between the management class and the data model.
Sources: resend/api_keys/_api_keys.py:16-160, resend/api_keys/_api_key.py:5-24
API Key Data Model
API keys are represented by the ApiKey type, which contains metadata regarding the key's identity and usage history.
| Field | Type | Description |
|---|---|---|
id | str | Unique identifier for the API key. |
name | str | Display name for the API key. |
created_at | str | Timestamp indicating when the key was generated. |
last_used_at | Optional[str] | Timestamp of the most recent API call made with this key. |
Sources: resend/api_keys/_api_key.py:5-24
Core Operations
Creation
You create a new API key by providing a name and optional permissions. Permissions determine if the key has full_access or is restricted to sending_access.
The flow shows the creation process where the token is only returned once during initial creation.
Sources: resend/api_keys/_api_keys.py:91-105, tests/api_keys_test.py:11-23
Listing and Pagination
The list operation retrieves all API keys for the authenticated user. It supports pagination through limit, after, and before parameters.
| Parameter | Type | Description |
|---|---|---|
limit | int | Number of keys to retrieve (1 to 100). |
after | str | Retrieve keys created after this specific ID. |
before | str | Retrieve keys created before this specific ID. |
Sources: resend/api_keys/_api_keys.py:69-82, examples/api_keys.py:34-40
Management Methods
| Method | HTTP Verb | Path | Description |
|---|---|---|---|
create | POST | /api-keys | Generates a new key and token. |
update | PATCH | /api-keys/{id} | Updates the name of an existing key. |
list | GET | /api-keys | Returns a list of available keys. |
remove | DELETE | /api-keys/{id} | Permanently deletes a key. |
Sources: resend/api_keys/_api_keys.py:89-160
Asynchronous Execution
The SDK provides asynchronous variants for all management operations using httpx. These methods follow the naming convention [method]_async.
# Async creation example
params: resend.ApiKeys.CreateParams = {"name": "production-key"}
key = await resend.ApiKeys.create_async(params)
Sources: resend/api_keys/_api_keys.py:133-160, tests/api_keys_async_test.py:17-29
Authentication Flow
Internal requests utilize the Request class to inject authentication headers. Every request to the Resend API includes the Authorization header with the Bearer token configured at the module level via resend.api_key.
The sequence diagram demonstrates how the global API key is used to authorize management operations.
Sources: resend/request.py:65-72, resend/init.py:186, examples/api_keys.py:7-10
Summary
The API Keys Management module provides a structured interface for lifecycle management of authentication tokens. It supports both synchronous and asynchronous programming models and enforces type safety through detailed TypedDict definitions for all operation parameters.
Sources: resend/api_keys/_api_keys.py