This project provides a comprehensive example demonstrating how Email Service Providers (ESPs) can use the SendPost Ruby SDK to manage email sending operations.
The example demonstrates a complete ESP workflow including:
- Sub-Account Management - Create and manage sub-accounts for different clients or use cases
- Webhook Setup - Configure webhooks to receive real-time email event notifications
- Domain Management - Add and verify sending domains
- Email Sending - Send transactional and marketing emails
- Message Tracking - Retrieve message details for tracking and debugging
- Statistics & Analytics - Monitor email performance via sub-account stats, IP stats, and IP pool stats
- IP Pool Management - Create and manage IP pools for better deliverability control
- Ruby 2.7 or higher
- Bundler (Ruby gem manager)
- SendPost account with:
- Account API Key (for account-level operations)
- Sub-Account API Key (for sub-account-level operations)
cd example-sdk-rubyInstall the required dependencies using Bundler:
bundle installThis will install the SendPost Ruby SDK version 2.0.0 from RubyGems.org.
Or install the SendPost Ruby SDK directly:
gem install sendpost_ruby_sdkYou can set API keys in two ways:
export SENDPOST_ACCOUNT_API_KEY="your_account_api_key_here"
export SENDPOST_SUB_ACCOUNT_API_KEY="your_sub_account_api_key_here"Edit ESPExample.rb and update the constants:
SUB_ACCOUNT_API_KEY = "your_sub_account_api_key_here"
ACCOUNT_API_KEY = "your_account_api_key_here"Edit ESPExample.rb and update:
TEST_FROM_EMAIL- Your verified sender email addressTEST_TO_EMAIL- Recipient email addressTEST_DOMAIN_NAME- Your sending domainWEBHOOK_URL- Your webhook endpoint URL
ruby ESPExample.rbOr if using Bundler:
bundle exec ruby ESPExample.rbThis will execute the complete ESP workflow demonstrating all features.
example-sdk-ruby/
├── ESPExample.rb # Main example class
├── README.md # This file
├── Gemfile # Ruby dependencies
└── .gitignore # Git ignore file (optional)
The example demonstrates the following workflow:
- List all sub-accounts
- Create new sub-accounts for different clients or use cases
- Create webhooks to receive email event notifications
- Configure which events to receive (delivered, opened, clicked, bounced, etc.)
- Add sending domains
- View DNS records needed for domain verification
- List all domains
- Send transactional emails (order confirmations, receipts, etc.)
- Send marketing emails (newsletters, promotions, etc.)
- Configure tracking (opens, clicks)
- Add custom headers and fields
- Retrieve message details by message ID
- View delivery information, IP used, submission time, etc.
- Get sub-account statistics (processed, delivered, opens, clicks, bounces, etc.)
- Get aggregate statistics
- Get account-level statistics across all sub-accounts
- List all dedicated IPs
- Create IP pools for better deliverability control
- View IP pool configurations
- Transactional Emails: Order confirmations, receipts, notifications
- Marketing Emails: Newsletters, promotions, campaigns
- Tracking: Open tracking, click tracking
- Customization: Custom headers, custom fields, groups
- Sub-Account Stats: Daily statistics for a specific sub-account
- Aggregate Stats: Overall performance metrics
- Account Stats: Statistics across all sub-accounts
- Performance Metrics: Open rates, click rates, delivery rates
- Sub-Accounts: Organize sending by client, product, or use case
- Domains: Add and verify sending domains
- IPs: Monitor dedicated IP addresses
- IP Pools: Group IPs for better deliverability control
- Webhooks: Receive real-time notifications for email events
- Event Types: Processed, delivered, dropped, bounced, opened, clicked, unsubscribed, spam
Used for account-level operations:
- Creating and managing sub-accounts
- Managing IPs and IP pools
- Creating webhooks
- Getting account-level statistics
- Retrieving messages
Used for sub-account-level operations:
- Sending emails
- Managing domains
- Managing suppressions
- Getting sub-account statistics
When you run the example, you'll see output like:
╔═══════════════════════════════════════════════════════════════╗
║ SendPost Ruby SDK - ESP Example Workflow ║
╚═══════════════════════════════════════════════════════════════╝
=== Step 1: Listing All Sub-Accounts ===
Retrieving all sub-accounts...
✓ Retrieved 3 sub-account(s)
- ID: 50441
Name: API
API Key: pR0YIuxYSbVwmQi2Y8Qs
...
=== Step 2: Creating Webhook ===
Creating webhook...
URL: https://your-webhook-endpoint.com/webhook
✓ Webhook created successfully!
ID: 12345
...
...
The example includes comprehensive error handling. If an operation fails, you'll see:
- HTTP status code
- Error response body
- Stack trace for debugging
Common issues:
- 401 Unauthorized: Invalid or missing API key
- 403 Forbidden: Resource already exists or insufficient permissions
- 404 Not Found: Resource ID doesn't exist
- 422 Unprocessable Entity: Invalid request body or parameters
The SendPost Ruby SDK is available on RubyGems.org. Install the latest version (2.0.0) with:
gem install sendpost_ruby_sdkOr add to your Gemfile:
gem 'sendpost_ruby_sdk', '~> 2.0.0'If you're using the SDK from the local repository:
cd ../sendpost-ruby-sdk
gem build sendpost_ruby_sdk.gemspec
gem install ./sendpost_ruby_sdk-2.0.0.gemOr add to your Gemfile:
gem 'sendpost_ruby_sdk', path: '../sendpost-ruby-sdk'require 'sendpost_ruby_sdk'
puts Sendpost::VERSIONrequire 'sendpost_ruby_sdk'
# Configure API key
config = Sendpost::Configuration.new
config.host = 'https://api.sendpost.io/api/v1'
config.api_key['X-SubAccount-ApiKey'] = 'your_sub_account_api_key'
# Create API client
api_client = Sendpost::ApiClient.new(config)
email_api = Sendpost::EmailApi.new(api_client)
# Create email message
email_message = Sendpost::EmailMessageObject.new
# Set sender
from_addr = Sendpost::EmailMessageFrom.new
from_addr.email = 'sender@example.com'
from_addr.name = 'Sender'
email_message.from = from_addr
# Set recipient
recipient = Sendpost::EmailMessageToInner.new
recipient.email = 'recipient@example.com'
recipient.name = 'Recipient'
email_message.to = [recipient]
# Set email content
email_message.subject = 'Test Email'
email_message.html_body = '<h1>Hello!</h1>'
email_message.text_body = 'Hello!'
# Send email
response = email_api.send_email(email_message)
puts "Message ID: #{response[0].message_id}"require 'sendpost_ruby_sdk'
require 'date'
# Configure API key
config = Sendpost::Configuration.new
config.host = 'https://api.sendpost.io/api/v1'
config.api_key['X-Account-ApiKey'] = 'your_account_api_key'
# Create API client
api_client = Sendpost::ApiClient.new(config)
stats_api = Sendpost::StatsApi.new(api_client)
# Get stats for last 7 days
to_date = Date.today
from_date = to_date - 7
stats = stats_api.account_subaccount_stat_subaccount_id_get(
from_date, to_date, sub_account_id
)
stats.each do |stat|
puts "Date: #{stat.date}, Processed: #{stat.stats.processed}"
endThe Ruby SDK uses a configuration object pattern:
config = Sendpost::Configuration.new
config.host = 'https://api.sendpost.io/api/v1'
config.api_key['X-SubAccount-ApiKey'] = 'your_key'Each API class requires an ApiClient instance:
api_client = Sendpost::ApiClient.new(config)
email_api = Sendpost::EmailApi.new(api_client)The SDK raises Sendpost::ApiError for API errors:
begin
response = email_api.send_email(email_message)
rescue Sendpost::ApiError => e
puts "Error: #{e.code} - #{e.response_body}"
rescue StandardError => e
puts "Unexpected error: #{e.message}"
endAfter running the example:
- Customize for Your Use Case: Modify the example to match your specific requirements
- Integrate with Your Application: Use the SDK in your own Ruby application
- Set Up Webhooks: Configure your webhook endpoint to receive email events
- Monitor Statistics: Set up regular monitoring of your email performance
- Optimize Deliverability: Use IP pools and domain verification to improve deliverability
- SendPost Ruby SDK on RubyGems - Install the latest version (2.0.0)
- SendPost API Documentation
- SendPost Ruby SDK GitHub
- SendPost Developer Portal
For questions or issues:
- Email: hello@sendpost.io
- Website: https://sendpost.io
- Documentation: https://docs.sendpost.io
This example is provided as-is for demonstration purposes.