### Install Async Support Source: https://github.com/resend/resend-python/blob/main/README.md Install the necessary extra dependencies to enable asynchronous operations. ```bash pip install resend[async] ``` -------------------------------- ### Install Async Support for Resend SDK Source: https://github.com/resend/resend-python/blob/main/_autodocs/configuration.md Install the necessary packages to enable asynchronous HTTP client support in the Resend Python SDK. ```bash pip install resend[async] # or pip install resend httpx ``` -------------------------------- ### Complete Configuration Example Source: https://github.com/resend/resend-python/blob/main/_autodocs/configuration.md A comprehensive example demonstrating how to configure the Resend Python SDK with API key, API URL, and custom timeouts for both sync and async HTTP clients. ```python import resend import os # 1. Set API key from environment or explicit assignment resend.api_key = os.environ.get("RESEND_API_KEY", "re_yourkey") # 2. Configure API URL (optional, defaults to production) resend.api_url = os.environ.get( "RESEND_API_URL", "https://api.resend.com" ) # 3. Configure sync HTTP client with custom timeout resend.default_http_client = resend.RequestsClient(timeout=60) # 4. Configure async HTTP client with custom timeout try: resend.default_async_http_client = resend.HTTPXClient(timeout=60) except ImportError: # httpx not installed resend.default_async_http_client = None ``` -------------------------------- ### Install Resend SDK Source: https://github.com/resend/resend-python/blob/main/README.md Command to install the Resend Python SDK via pip. ```bash pip install resend ``` -------------------------------- ### Install Resend SDK with Async Support Source: https://github.com/resend/resend-python/blob/main/_autodocs/README.md Install the Resend Python SDK, including optional asynchronous HTTP client support. This command ensures all necessary dependencies are met. ```bash pip install resend[async] ``` -------------------------------- ### Example Event Schema Source: https://github.com/resend/resend-python/blob/main/_autodocs/types.md An example of how to define an event schema for tracking user-specific data. This schema includes user ID, purchase amount, VIP status, and purchase date. ```python schema: resend.EventSchema = { "user_id": "string", "purchase_amount": "number", "is_vip": "boolean", "purchase_date": "date" } ``` -------------------------------- ### Example of Using EmailTemplate Source: https://github.com/resend/resend-python/blob/main/_autodocs/emails.md Illustrates how to construct the `params` dictionary to send an email using a predefined template with custom variables. ```python params: resend.Emails.SendParams = { "from": "onboarding@resend.dev", "to": "customer@example.com", "template": { "id": "template_abc123", "variables": { "username": "John", "order_id": 12345 } } } ``` -------------------------------- ### Create and Send a Broadcast Immediately Source: https://github.com/resend/resend-python/blob/main/_autodocs/broadcasts.md This example demonstrates creating and sending a broadcast in one step. Set the 'send' parameter to True to initiate immediate sending. ```python import resend resend.api_key = "re_yourkey" # Create and send immediately response = resend.Broadcasts.create({ "from": "hello@example.com", "segment_id": "segment_abc123", "subject": "Urgent Update", "html": "
Your order #{{order_id}} is confirmed.
Total: {{amount}}
``` -------------------------------- ### Async List and Get Logs Source: https://github.com/resend/resend-python/blob/main/_autodocs/logs.md Provides asynchronous equivalents for listing and retrieving log entries, suitable for non-blocking operations in async applications. Requires an active asyncio event loop. ```python import asyncio import resend resend.api_key = "re_yourkey" async def main(): # List logs async logs = await resend.Logs.list_async({"limit": 10}) print(f"Total logs: {len(logs['data'])}") # Get specific log async if logs["data"]: log = await resend.Logs.get_async(logs["data"][0]["id"]) print(f"Status: {log['response_status']}") asyncio.run(main()) ``` -------------------------------- ### Asynchronous Contact Management Source: https://github.com/resend/resend-python/blob/main/_autodocs/contacts.md Demonstrates asynchronous operations for creating, listing, updating, getting, and removing contacts using the Resend Python SDK. Ensure you have set your API key. ```python import asyncio import resend resend.api_key = "re_yourkey" async def main(): # Create async response = await resend.Contacts.create_async({ "email": "user@example.com", "first_name": "User" }) print(f"Created: {response['id']}") # List async contacts = await resend.Contacts.list_async({"limit": 10}) print(f"Total contacts: {len(contacts['data'])}") # Update async await resend.Contacts.update_async({ "id": response["id"], "last_name": "Test" }) # Get async contact = await resend.Contacts.get_async(response["id"]) print(f"Email: {contact['email']}") # Remove async await resend.Contacts.remove_async(response["id"]) asyncio.run(main()) ``` -------------------------------- ### Real Estate Custom Properties Source: https://github.com/resend/resend-python/blob/main/_autodocs/contact-properties.md Provides examples of creating custom properties for real estate use cases, such as agent ID, budget, preferred locations, last showing date, and buyer seriousness. ```python # Store property-specific information resend.ContactProperties.create({"name": "agent_id", "type": "string"}) resend.ContactProperties.create({"name": "budget", "type": "number"}) resend.ContactProperties.create({"name": "preferred_locations", "type": "string"}) resend.ContactProperties.create({"name": "last_showing_date", "type": "date"}) resend.ContactProperties.create({"name": "is_serious_buyer", "type": "boolean"}) ``` -------------------------------- ### Webhook Event Payload Example Source: https://github.com/resend/resend-python/blob/main/_autodocs/webhooks.md An example of the JSON payload for an 'email.sent' webhook event. This structure includes event type, timestamp, and specific data related to the sent email. ```json { "type": "email.sent", "created_at": "2024-01-01T00:00:00.000Z", "data": { "email_id": "email_abc123", "from": "hello@example.com", "to": ["user@example.com"], "created_at": "2024-01-01T00:00:00.000Z" } } ``` -------------------------------- ### Get Contact Source: https://github.com/resend/resend-python/blob/main/_autodocs/contacts.md Retrieves a specific contact by its ID. ```APIDOC ## Get Contact ### Description Retrieves a specific contact by its ID. ### Method GET ### Endpoint /contacts/{id} ### Parameters #### Path Parameters - **id** (str) - Required - The ID of the contact to retrieve. ### Response #### Success Response (200) - **id** (str) - Contact ID - **email** (str) - Email address - **first_name** (Optional[str]) - First name - **last_name** (Optional[str]) - Last name - **created_at** (str) - ISO 8601 creation timestamp - **unsubscribed** (bool) - Whether contact is unsubscribed #### Response Example ```json { "id": "con_xxxxxxxxxxxxxxxx", "email": "user@example.com", "first_name": "User", "last_name": null, "created_at": "2023-10-27T10:00:00Z", "unsubscribed": false } ``` ``` -------------------------------- ### get() Source: https://github.com/resend/resend-python/blob/main/_autodocs/broadcasts.md Retrieve the details of a single broadcast by its unique ID. ```APIDOC ## GET /v1/broadcasts/{broadcast_id} ### Description Retrieve a single broadcast by ID. ### Method GET ### Endpoint /v1/broadcasts/{broadcast_id} ### Parameters #### Path Parameters - **broadcast_id** (str) - Required - The broadcast ID ### Response #### Success Response (200) - All broadcast fields ### Response Example ```json { "id": "broadcast_abc123", "name": "Summer Promo", "status": "draft", "subject": "Summer Sale", "from": "hello@example.com", "created_at": "2024-01-01T12:00:00Z", "updated_at": "2024-01-01T12:00:00Z" } ``` ``` -------------------------------- ### get() Source: https://github.com/resend/resend-python/blob/main/_autodocs/contact-properties.md Retrieves a specific contact property definition by its unique ID. ```APIDOC ## get() ### Description Retrieve a single property by ID. ### Method `GET` (Assumed based on get operation) ### Endpoint `/v1/contact-properties/{property_id}` (Assumed based on typical REST patterns) ### Parameters #### Path Parameters - **property_id** (str) - Required - The property ID ### Response #### Success Response (200) - **id** (str) - The property ID - **name** (str) - Property name - **type** (str) - Data type - **created_at** (str) - Timestamp of creation #### Response Example ```json { "id": "prop_abc123", "name": "phone_number", "type": "string", "created_at": "2023-01-01T12:00:00Z" } ``` ``` -------------------------------- ### Create User Lifecycle Event Schemas Source: https://github.com/resend/resend-python/blob/main/_autodocs/events.md Defines schemas for 'user_signup' and 'user_trial_ending' events. These help standardize user-related event data. ```python resend.Events.create({ "name": "user_signup", "schema": { "user_id": "string", "plan_tier": "string", "signup_date": "date" } }) resend.Events.create({ "name": "user_trial_ending", "schema": { "user_id": "string", "days_remaining": "number" } }) ``` -------------------------------- ### Get Webhook Source: https://github.com/resend/resend-python/blob/main/_autodocs/webhooks.md Retrieves the details of a specific webhook endpoint using its ID. ```APIDOC ## GET /webhooks/{webhook_id} ### Description Retrieve a single webhook by its ID. ### Method GET ### Endpoint /webhooks/{webhook_id} ### Parameters #### Path Parameters - **webhook_id** (str) - Required - The ID of the webhook to retrieve. ### Response #### Success Response (200) - **id** (str): The webhook ID. - **object** (str): Always "webhook". - **endpoint** (str): The webhook endpoint URL. - **events** (List[str]): The subscribed event types. - **status** (str): The status of the webhook ('enabled' or 'disabled'). - **signing_secret** (str): The webhook signing secret. - **created_at** (str): Timestamp of when the webhook was created. #### Response Example ```json { "object": "webhook", "id": "whk_abc123", "endpoint": "https://example.com/webhooks/resend", "events": ["email.sent", "email.delivered"], "status": "enabled", "signing_secret": "your_signing_secret_here", "created_at": "2023-10-27T10:00:00Z" } ``` ``` -------------------------------- ### Create Marketing Topics Source: https://github.com/resend/resend-python/blob/main/_autodocs/topics.md Creates three distinct 'opt-in' topics suitable for marketing communications, each with a specific focus and description. ```python # Create opt-in topics for different email types resend.Topics.create({ "name": "Marketing Newsletter", "default_subscription": "opt_in", "description": "Promotional offers and new product launches" }) resend.Topics.create({ "name": "Product Updates", "default_subscription": "opt_in", "description": "New features and improvements" }) resend.Topics.create({ "name": "Event Invitations", "default_subscription": "opt_in", "description": "Invitations to webinars and events" }) ``` -------------------------------- ### Get a Single Audience Source: https://github.com/resend/resend-python/blob/main/_autodocs/audiences.md Retrieve details for a specific audience using its ID. ```python audience = resend.Audiences.get("audience_abc123") print(f"Audience: {audience['name']}") print(f"Created: {audience['created_at']}") ``` -------------------------------- ### get() Source: https://github.com/resend/resend-python/blob/main/_autodocs/audiences.md Retrieve a single audience by its unique ID. This method returns the details of a specific audience. ```APIDOC ## GET /audiences/{audience_id} ### Description Retrieve a single audience by its ID. ### Method GET ### Endpoint /audiences/{audience_id} ### Parameters #### Path Parameters - **audience_id** (str) - Required - The ID of the audience to retrieve ### Response #### Success Response (200 OK) - **id** (str) - Audience ID - **name** (str) - Audience name - **created_at** (str) - ISO 8601 creation timestamp #### Response Example ```json { "id": "aud_12345", "name": "Premium Customers", "created_at": "2023-10-26T10:00:00Z" } ``` ``` -------------------------------- ### Async Methods for Contact Properties Source: https://github.com/resend/resend-python/blob/main/_autodocs/contact-properties.md Shows how to asynchronously create and list contact properties using `create_async()` and `list_async()`. Ensure Resend API key is set. ```python import asyncio import resend resend.api_key = "re_yourkey" async def main(): # Create property async response = await resend.ContactProperties.create_async({ "name": "preferred_language", "type": "string" }) print(f"Created: {response['id']}") # List properties async props = await resend.ContactProperties.list_async({"limit": 10}) print(f"Total properties: {len(props['data'])}") asyncio.run(main()) ``` -------------------------------- ### create() Source: https://github.com/resend/resend-python/blob/main/_autodocs/api-keys.md Adds a new API key for authentication. It requires a name and can optionally include permission levels and a domain ID for restricted access. ```APIDOC ## POST /v1/keys ### Description Creates a new API key for authentication. You can specify the key's name, permission level, and optionally restrict it to a specific domain. ### Method POST ### Endpoint /v1/keys ### Parameters #### Request Body - **name** (str) - Required - Name for the API key - **permission** (str) - Optional - Permission level: "full_access" (all operations) or "sending_access" (send emails only). Defaults to "full_access". - **domain_id** (str) - Optional - Restrict sending to a specific domain (only with sending_access permission) ### Response #### Success Response (200) - **id** (str) - The ID of the created API key - **token** (str) - The API key token (only visible on creation) ### Response Example ```json { "id": "key_abc123", "token": "re_xxxxxxxxxxxxxxxxxxxxxxxx" } ``` ### Throws - **ValidationError**: Missing required fields - **InvalidApiKeyError**: Invalid permission or domain_id ``` -------------------------------- ### Paginate Through Logs Source: https://github.com/resend/resend-python/blob/main/_autodocs/logs.md Demonstrates how to retrieve logs page by page using cursor-based pagination. Essential for handling large log volumes efficiently. ```python import resend # Get first page response = resend.Logs.list({"limit": 20}) print(f"Page 1: {len(response['data'])} logs") # Get next page if available if response["has_more"]: next_response = resend.Logs.list({ "limit": 20, "after": response["data"][-1]["id"] }) print(f"Page 2: {len(next_response['data'])} logs") ``` -------------------------------- ### get() Source: https://github.com/resend/resend-python/blob/main/_autodocs/templates.md Retrieves a single email template by its unique ID, returning all its details including HTML content and variables. ```APIDOC ## get() ### Description Retrieve a single template by ID. ### Method GET (inferred from SDK method) ### Endpoint /templates/{template_id} (inferred from SDK context and parameters) ### Parameters #### Path Parameters - **template_id** (str) - Required - The template ID ### Response #### Success Response (200) - All template fields including HTML, subject, variables, etc. ### Request Example ```python template = resend.Templates.get("template_abc123") print(f"Subject: {template['subject']}") print(f"Variables: {template.get('variables', [])}") ``` ``` -------------------------------- ### Configure Custom Async Client Source: https://github.com/resend/resend-python/blob/main/README.md Override the default HTTP client to customize settings like timeouts. ```python import resend resend.api_key = "re_yourkey" resend.default_async_http_client = resend.HTTPXClient(timeout=60) ``` -------------------------------- ### Create Transactional and Marketing Topics Source: https://github.com/resend/resend-python/blob/main/_autodocs/topics.md Creates both an 'opt-out' transactional topic for essential business operations and an 'opt-in' marketing topic for promotional content. ```python # Transactional (opt-out) - important for business operations transactional = resend.Topics.create({ "name": "Transactional", "default_subscription": "opt_out", "description": "Order confirmations, receipts, password resets" }) # Marketing (opt-in) - promotional content marketing = resend.Topics.create({ "name": "Marketing", "default_subscription": "opt_in", "description": "Promotions, offers, and marketing content" }) ``` -------------------------------- ### get() Source: https://github.com/resend/resend-python/blob/main/_autodocs/emails.md Retrieves a single email by its unique identifier. This method allows fetching details of a previously sent or scheduled email. ```APIDOC ## get() ### Description Retrieve a single email by ID. ### Method GET (inferred from SDK method) ### Endpoint /emails/{email_id} (inferred from SDK context) ### Parameters #### Path Parameters - **email_id** (str) - Required - The ID of the email to retrieve ### Response #### Success Response (200) - **id** (str) - The Email ID - **from** (str) - The sender email address - **to** (Union[List[str], str]) - Recipients - **created_at** (str) - When the email was created - **subject** (str) - Email subject - **html** (str) - HTML content - **text** (str) - Plain text content - **bcc** (Union[List[str], str]) - Blind carbon copy addresses - **cc** (Union[List[str], str]) - Carbon copy addresses - **reply_to** (Union[List[str], str]) - Reply-to addresses - **last_event** (str) - The last event of the email ### Response Example ```json { "id": "email_123456", "from": "sender@example.com", "to": ["recipient@example.com"], "created_at": "2024-01-01T12:00:00Z", "subject": "Test Email", "html": "This is a test email.
", "text": "This is a test email.", "bcc": [], "cc": [], "reply_to": [], "last_event": "sent" } ``` ``` -------------------------------- ### create() Source: https://github.com/resend/resend-python/blob/main/_autodocs/automations.md Create a new automation workflow with specified name, steps, and connections. An optional status can be provided to enable or disable the automation upon creation. ```APIDOC ## POST /automations ### Description Creates a new automation workflow. ### Method POST ### Endpoint /automations ### Parameters #### Request Body - **name** (str) - Required - Automation workflow name - **steps** (List[AutomationStep]) - Required - Workflow steps (must include at least one trigger) - **connections** (List[AutomationConnection]) - Required - Connections between steps - **status** (AutomationStatus) - Optional - Initial status: "enabled" or "disabled" (default) ### Response #### Success Response (200) - **object** (str) - Always "automation" - **id** (str) - The automation ID ``` -------------------------------- ### Create and Update Contact with Custom Properties Source: https://github.com/resend/resend-python/blob/main/_autodocs/contact-properties.md Demonstrates how to add custom fields like phone number, lifetime value, last purchase date, and VIP status when creating or updating a contact. ```python # Create contact with custom properties contact = resend.Contacts.create({ "email": "john@example.com", "first_name": "John", "last_name": "Doe", # Custom properties stored as additional fields "phone_number": "555-1234", "lifetime_value": 5000, "last_purchase_date": "2024-01-15", "is_vip": True }) # Update contact with custom properties resend.Contacts.update({ "id": contact["id"], "phone_number": "555-5678", "lifetime_value": 7500, "is_vip": False }) ``` -------------------------------- ### Configure Staging Environment Source: https://github.com/resend/resend-python/blob/main/_autodocs/configuration.md Set API key and URL for the staging environment. This allows testing against a staging API endpoint. ```python import resend # Use staging credentials resend.api_key = "re_staging_xxxxx" resend.api_url = "https://staging-api.resend.com" # if available ``` -------------------------------- ### Create Broadcast to Segment Source: https://github.com/resend/resend-python/blob/main/_autodocs/segments.md Example of creating a broadcast that targets a specific segment. The `segment_id` parameter is crucial for directing the broadcast to the intended audience. ```python response = resend.Broadcasts.create({ "from": "hello@example.com", "segment_id": "segment_abc123", # Send to this segment "subject": "Premium Offer", "html": "Thanks for joining!
", "variables": [ { "name": "name", "description": "User's name", "type": "string", "required": True } ] }) print(f"Template created: {response['id']}") ``` ``` -------------------------------- ### Get a Single Webhook Source: https://github.com/resend/resend-python/blob/main/_autodocs/webhooks.md Retrieve the details of a specific webhook using its unique ID. This includes the endpoint, status, events, and signing secret. ```python webhook = resend.Webhooks.get("webhook_abc123") print(f"Endpoint: {webhook['endpoint']}") print(f"Status: {webhook['status']}") print(f"Events: {webhook['events']}") ``` -------------------------------- ### Asynchronous Audience Operations Source: https://github.com/resend/resend-python/blob/main/_autodocs/audiences.md Perform audience operations asynchronously using async/await syntax. Includes create, list, get, and remove operations. ```python import asyncio import resend resend.api_key = "re_yourkey" async def main(): # Create async response = await resend.Audiences.create_async({ "name": "Newsletter Subscribers" }) print(f"Created: {response['id']}") # List async audiences = await resend.Audiences.list_async({"limit": 10}) print(f"Total audiences: {len(audiences['data'])}") # Get async audience = await resend.Audiences.get_async(response["id"]) print(f"Name: {audience['name']}") # Remove async await resend.Audiences.remove_async(response["id"]) asyncio.run(main()) ``` -------------------------------- ### Async Methods Source: https://github.com/resend/resend-python/blob/main/_autodocs/api-keys.md Provides asynchronous equivalents for creating, listing, and removing API keys, allowing for non-blocking operations. ```APIDOC ## Async API Keys Operations All methods available in the synchronous API Keys API have asynchronous counterparts, suffixed with `_async`. ### create_async() - Asynchronously creates a new API key. ### list_async() - Asynchronously retrieves a paginated list of API keys. ### remove_async() - Asynchronously deletes an API key by its ID. **Example:** ```python import asyncio import resend resend.api_key = "re_yourkey" async def main(): # Create async response = await resend.ApiKeys.create_async({ "name": "Async Key", "permission": "full_access" }) print(f"Created: {response['id']}") # List async keys = await resend.ApiKeys.list_async({"limit": 10}) print(f"Total keys: {len(keys['data'])}") # Remove async await resend.ApiKeys.remove_async(response["id"]) print("Key deleted") asyncio.run(main()) ``` ``` -------------------------------- ### Async Methods Source: https://github.com/resend/resend-python/blob/main/_autodocs/logs.md Asynchronous equivalents for listing and retrieving log entries are available as `list_async()` and `get_async()`. ```APIDOC ## Async Methods ### Description Asynchronous equivalents for listing and retrieving log entries are available as `list_async()` and `get_async()`. ### Method - `list_async(params: Optional[ListParams] = None)` - `get_async(log_id: str)` ### Parameters - **params** (`Optional[ListParams]`) - Optional - Pagination parameters for `list_async()` - **limit** (`Optional[int]`) - Number of logs (1-100, default 10) - **after** (`Optional[str]`) - Cursor for pagination - **before** (`Optional[str]`) - Cursor for pagination - **log_id** (`str`) - Required - The log ID for `get_async()` ### Request Example ```python import asyncio import resend resend.api_key = "re_yourkey" async def main(): # List logs async logs = await resend.Logs.list_async({"limit": 10}) print(f"Total logs: {len(logs['data'])}") # Get specific log async if logs["data"]: log = await resend.Logs.get_async(logs["data"][0]["id"]) print(f"Status: {log['response_status']}") asyncio.run(main()) ``` ### Response - `list_async()` returns a `ListResponse` object similar to `list()`. - `get_async()` returns a `Log` object similar to `get()`. ``` -------------------------------- ### create() Source: https://github.com/resend/resend-python/blob/main/_autodocs/audiences.md Create a new audience. This method takes audience creation parameters and returns the details of the newly created audience. ```APIDOC ## POST /audiences ### Description Create a new audience group for organizing contacts. ### Method POST ### Endpoint /audiences ### Parameters #### Request Body - **name** (str) - Required - The audience name ### Request Example ```json { "name": "Premium Customers" } ``` ### Response #### Success Response (200 OK) - **object** (str) - Always "audience" - **id** (str) - The created audience ID - **name** (str) - The audience name #### Response Example ```json { "object": "audience", "id": "aud_12345", "name": "Premium Customers" } ``` ``` -------------------------------- ### Get Single Log Entry by ID Source: https://github.com/resend/resend-python/blob/main/_autodocs/logs.md Retrieves a specific log entry using its unique ID. This is useful for detailed inspection of a single request/response. ```python log = resend.Logs.get("log_abc123") print(f"Endpoint: {log['endpoint']}") print(f"Status: {log['response_status']}") print(f"Request body: {log['request_body']}") print(f"Response body: {log['response_body']}") ``` -------------------------------- ### Send Email with Remote Attachment Source: https://github.com/resend/resend-python/blob/main/_autodocs/types.md Example of sending an email with a remote file attachment. The attachment is specified using its HTTPS URL and an optional display filename. ```python attachment = { "path": "https://cdn.example.com/invoice.pdf", "filename": "invoice.pdf" } email = resend.Emails.send({ "from": "hello@example.com", "to": "user@example.com", "subject": "Your invoice", "html": "