API v2 Features: This guide covers the advanced API v2 create-message endpoint, which provides enhanced features like template substitutions, bulk sending, scheduled delivery, and comprehensive webhook support.
Quick Start
Here’s a simple example to send your first email using the API:Authentication
All API requests require authentication using your API v2 key:Authorization Header
Authorization Header
Account ID
Account ID
Replace
{account_id}
in the URL with your actual account ID. The API Key must be created within this account.Need API Keys? If you haven’t created API v2 credentials yet, check out our API Credentials guide for step-by-step instructions.
Basic Examples
Advanced Examples
Multiple Recipients: When you specify multiple recipients in the recipients array, AhaSend sends separate individual emails to each recipient. This is not one email with multiple addresses in the To/CC headers, but rather individual personalized emails where each recipient only sees their own email address and can receive personalized template substitutions.
Response Handling
The API returns a response with information about each message created:Successful Response (200)
Response Fields
Message Status
Message Status
queued
- Message accepted and queued for deliveryscheduled
- Message scheduled for future deliveryfailed
- Message failed validation or processing
Message ID
Message ID
Unique identifier for tracking the message through webhooks and logs
Schedule Information
Schedule Information
If you include the
schedule
parameter in your request, the response will include timing information including the scheduled first attempt and expiration.Error Handling
Handle different HTTP status codes appropriately:400 - Bad Request
400 - Bad Request
Common causes:
- Missing required fields (
from
,recipients
,subject
) - Invalid email addresses
- Missing
text_content
orhtml_content
- Invalid retention values (outside 1-30 days range)
401 - Unauthorized
401 - Unauthorized
403 - Forbidden
403 - Forbidden
Common causes:
- Sender domain not verified
- API key doesn’t have permission for the account
- Account suspended or over limits
409 - Conflict
409 - Conflict
Idempotency key conflict
- Request with same idempotency key already processed
- Returns original response for duplicate requests
Validation Requirements
Based on the API documentation, ensure your requests meet these requirements:Content Requirements
Content Requirements
- Either
text_content
orhtml_content
is required - Both can be provided for multipart emails
- Use proper HTML structure for
html_content
Sender Domain
Sender Domain
from.email
must be from a domain you own- Domain must have valid DNS records configured
- Domain must be verified in your AhaSend account
Retention Settings
Retention Settings
retention.metadata
must be between 1 and 30 daysretention.data
must be between 0 and 30 days- Use 0 for immediate content deletion after processing
Scheduling Format
Scheduling Format
- Schedule times must be in RFC3339 format
- Example:
"2024-12-25T10:30:00Z"
- Times should be in UTC timezone
Best Practices
Idempotency
Idempotency
Use idempotency keys for safe retries:Keys expire after 24 hours and ensure duplicate requests return the same response.
Template Variables
Template Variables
AhaSend supports MiniJinja templating language for email content.
- Use descriptive variable names
- Provide fallback values in templates
- Validate variables before sending
- Escape HTML content in variables
Bulk Operations
Bulk Operations
Efficient bulk sending:
- Send up to 100 requests per second
- Send up to 100 recipients per request
- Handle partial failures gracefully
- Implement retry logic for failed messages
Error Handling
Error Handling
Robust error handling:
- Implement exponential backoff for retries
- Log API responses for debugging
- Handle different status codes appropriately
- Use webhooks for delivery confirmation