This project provides a comprehensive example demonstrating how Email Service Providers (ESPs) can use the SendPost Go 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
- Go 1.18 or higher
- SendPost account with:
- Account API Key (for account-level operations)
- Sub-Account API Key (for sub-account-level operations)
cd go-esp-exampleYou 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 main.go and update the constants in the NewESPExample function:
accountAPIKey := "your_account_api_key_here"
subAccountAPIKey := "your_sub_account_api_key_here"Edit main.go and update the constants at the top of the file:
testFromEmail- Your verified sender email addresstestToEmail- Recipient email addresstestDomainName- Your sending domainwebhookURL- Your webhook endpoint URL
go mod tidyThis will download required dependencies and update the go.mod file. Note that go.sum is not required to be committed.
go run main.goThis will execute the complete ESP workflow demonstrating all features.
example-sdk-go/
├── go.mod # Go module definition
├── README.md # This file
├── .gitignore # Git ignore file
└── main.go # Main example program
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 Go 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 details
- Error message 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 example is organized into a single ESPExample struct with methods for each workflow step:
NewESPExample()- Initializes the example with API clientListSubAccounts()- Lists all sub-accountsCreateSubAccount()- Creates a new sub-accountCreateWebhook()- Creates a webhookListWebhooks()- Lists all webhooksAddDomain()- Adds a sending domainListDomains()- Lists all domainsSendTransactionalEmail()- Sends a transactional emailSendMarketingEmail()- Sends a marketing emailGetMessageDetails()- Retrieves message detailsGetSubAccountStats()- Gets sub-account statisticsGetAggregateStats()- Gets aggregate statisticsListIPs()- Lists all IPsCreateIPPool()- Creates an IP poolListIPPools()- Lists all IP poolsGetAccountStats()- Gets account-level statisticsRunCompleteWorkflow()- Runs the complete workflow
The example uses Go's context.Context to pass authentication credentials:
// Account-level operations
ctx := context.WithValue(
context.Background(),
sendpost.ContextAPIKeys,
map[string]sendpost.APIKey{
"accountAuth": {Key: accountAPIKey},
},
)
// Sub-account-level operations
ctx := context.WithValue(
context.Background(),
sendpost.ContextAPIKeys,
map[string]sendpost.APIKey{
"subAccountAuth": {Key: subAccountAPIKey},
},
)To build a binary:
go build -o esp-example main.goThen run:
./esp-exampleYou can modify the main() function to test individual steps:
func main() {
example := NewESPExample()
// Test only email sending
example.SendTransactionalEmail()
example.SendMarketingEmail()
// Or test only statistics
example.GetSubAccountStats()
example.GetAggregateStats()
}After 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 Go 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
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.