Authentication overview
Mandrill, a transactional email API service, secures access primarily through API keys. These keys serve as the sole credential required to authenticate requests to the Mandrill API, enabling applications to send emails, manage templates, and retrieve analytics data. Each API key is associated with your Mandrill account and grants permissions based on its configuration. The use of API keys simplifies the authentication process for developers, allowing for direct integration into various applications and services without complex authorization flows.
As part of the Mailchimp ecosystem, Mandrill's authentication model is designed to be straightforward, leveraging a single token for identification and authorization. This model is suitable for server-to-server communication where the API key can be securely stored and managed. All communication with the Mandrill API occurs over HTTPS, ensuring that API keys and data are encrypted in transit. Understanding how to generate, manage, and securely implement these keys is fundamental for integrating with Mandrill effectively.
Supported authentication methods
Mandrill relies exclusively on API keys for authenticating requests to its API. This approach simplifies the authentication mechanism compared to multi-step protocols like OAuth 2.0, which are typically used for delegated authorization. For Mandrill, the API key acts as both the identifier and the secret, granting direct access to your account's transactional email functionalities.
The API key must be included in every API request, typically as a parameter in the request body or form data. Mandrill's SDKs and client libraries abstract this detail, allowing developers to configure the API key once, after which it is automatically appended to subsequent requests.
| Method | When to Use | Security Level |
|---|---|---|
| API Key | All API interactions with Mandrill, especially server-side applications and integrations. | High (when securely managed and transmitted over TLS) |
While API keys offer simplicity, their security heavily depends on proper handling. Unlike OAuth 2.0, which provides mechanisms for token expiry and refresh without exposing long-lived credentials, API keys generally do not expire unless explicitly revoked. This necessitates robust security practices to prevent unauthorized access.
Getting your credentials
To obtain your Mandrill API key, you need an active Mandrill account, which requires a paid Mailchimp account. Follow these steps to generate and retrieve your API key:
- Access your Mandrill Dashboard: Log in to your Mailchimp account, then navigate to your Mandrill dashboard.
- Go to Settings: In the Mandrill dashboard, locate and click on the 'Settings' menu option.
- API Keys: Within the Settings section, find and select 'API Keys'.
- Add a New API Key: Click the 'Add a New API Key' button. Mandrill will generate a unique alphanumeric string.
- Label Your Key: It is recommended to label your API key with a descriptive name (e.g., "Website Integration", "Mobile App Backend") to help you identify its purpose later. This is particularly useful if you generate multiple keys for different applications or environments.
- Copy the Key: Once generated, copy the API key immediately. For security reasons, Mandrill typically only displays the full key at the time of creation. If you lose it, you may need to generate a new one.
For more detailed instructions and visual guidance, consult the official Mandrill API Getting Started documentation.
Authenticated request example
Authenticating a request to the Mandrill API involves including your API key with each call. The following example demonstrates a basic /messages/send API call using Python, which is one of the supported Mandrill SDKs. This example assumes you have the Mandrill Python client library installed.
Python Example (using Mandrill Python Client)
import mandrill
try:
mandrill_client = mandrill.Mandrill('YOUR_MANDRILL_API_KEY')
message = {
'from_email': '[email protected]',
'to': [{
'email': '[email protected]',
'name': 'Recipient Name',
'type': 'to'
}],
'subject': 'Your Subject Here',
'html': '<p>Your HTML content goes here.</p>',
'text': 'Your plain text content goes here.',
'track_opens': True,
'track_clicks': True,
}
result = mandrill_client.messages.send(message=message, async=False, ip_pool='Main Pool')
print(result)
except mandrill.Error as e:
# Mandrill errors are thrown as mandrill.Error instances
print('A mandrill error occurred: %s - %s' % (e.__class__, e))
raise
In this Python example:
YOUR_MANDRILL_API_KEYshould be replaced with your actual API key.- The Mandrill client library handles the secure transmission of the API key over HTTPS and formats the request according to Mandrill's API specifications.
- The
mandrill.Mandrill()constructor initializes the client with your API key, making it available for subsequent API calls.
For direct HTTP requests without an SDK, the API key would typically be included in the JSON payload as part of the request body (e.g., {"key": "YOUR_MANDRILL_API_KEY", "message": {...}}). Always ensure that such requests are made over HTTPS to protect the API key in transit.
Security best practices
Securing your Mandrill API keys is crucial to prevent unauthorized email sending and access to your account data. Adhering to these best practices helps maintain the integrity and security of your transactional email operations:
- Keep API Keys Confidential: Treat your API keys like passwords. Never hardcode them directly into client-side code (e.g., JavaScript in a web browser) or commit them to public version control systems. Store them in secure environment variables, a secrets management service, or a configuration file that is not publicly accessible.
- Use Environment Variables: When deploying applications, use environment variables to inject API keys. This prevents keys from being directly present in your codebase and makes it easier to manage different keys for various environments (development, staging, production).
- Restrict IP Addresses: If your Mandrill account supports it, configure IP access restrictions for your API keys. This limits API calls using that key to only specific trusted IP addresses or ranges, adding a layer of protection against unauthorized use if the key is compromised.
- Generate Multiple Keys with Specific Permissions: For enhanced security, generate separate API keys for distinct applications or services. If one key is compromised, you can revoke it without affecting other integrations. Additionally, Mandrill allows you to assign specific permissions to each API key, limiting its capabilities (e.g., a key only for sending emails, not for managing templates).
- Regularly Rotate API Keys: Periodically rotate your API keys by generating a new key, updating your applications to use the new key, and then revoking the old one. This practice minimizes the window of vulnerability if a key is ever compromised without your knowledge.
- Monitor API Key Usage: Regularly review Mandrill's logs and analytics for unusual activity associated with your API keys. Anomalies, such as a sudden spike in email volume or calls from unexpected IP addresses, could indicate a compromised key.
- Use HTTPS/TLS: Mandrill's API always requires communication over HTTPS (Hypertext Transfer Protocol Secure). This encrypts all data, including your API key, during transit between your application and Mandrill's servers, protecting against interception. Ensure that your application configuration enforces TLS 1.2 or higher.
- Implement Error Handling: Implement robust error handling in your applications to gracefully manage authentication failures. This can help identify issues with compromised or revoked keys quickly and prevent service disruptions.
- Educate Your Team: Ensure anyone working with Mandrill API keys understands the importance of secure handling and follows established security protocols.
By following these best practices, developers can significantly reduce the risk of unauthorized access and maintain a secure transactional email environment with Mandrill.