Email API Integration Guide

February 14, 2024

Learn how to integrate Mail7's powerful email testing API into your applications. This comprehensive guide covers authentication, endpoints, and implementation examples in multiple programming languages.

Authentication

Mail7 uses API key authentication. You'll need to include your API key in the Authorization header of all requests.

Authorization: Bearer your_api_key_here

Getting Your API Credentials

  1. Log in to your Mail7 account
  2. Navigate to API Settings
  3. Generate a new API key if you haven't already
  4. Copy your API key and keep it secure

Core API Endpoints

1. Create Disposable Email

POST /api/email/create
Content-Type: application/json

{
    "prefix": "test",
    "domain": "mail7.io"
}

2. Check Inbox

GET /api/inbox/{email_address}
Authorization: Bearer your_api_key

3. Get Email Content

GET /api/email/{message_id}
Authorization: Bearer your_api_key

4. Delete Email

DELETE /api/email/{message_id}
Authorization: Bearer your_api_key

Implementation Examples

Python Implementation

import requests

class Mail7Client:
    def __init__(self, api_key):
        self.api_key = api_key
        self.base_url = 'https://api.mail7.io/api'
        self.headers = {
            'Authorization': f'Bearer {api_key}',
            'Content-Type': 'application/json'
        }
    
    def create_email(self, prefix='test'):
        response = requests.post(
            f'{self.base_url}/email/create',
            headers=self.headers,
            json={'prefix': prefix, 'domain': 'mail7.io'}
        )
        return response.json()
    
    def check_inbox(self, email_address):
        response = requests.get(
            f'{self.base_url}/inbox/{email_address}',
            headers=self.headers
        )
        return response.json()
    
    def get_email(self, message_id):
        response = requests.get(
            f'{self.base_url}/email/{message_id}',
            headers=self.headers
        )
        return response.json()

JavaScript/Node.js Implementation

class Mail7Client {
    constructor(apiKey) {
        this.apiKey = apiKey;
        this.baseUrl = 'https://api.mail7.io/api';
        this.headers = {
            'Authorization': `Bearer ${apiKey}`,
            'Content-Type': 'application/json'
        };
    }

    async createEmail(prefix = 'test') {
        const response = await fetch(`${this.baseUrl}/email/create`, {
            method: 'POST',
            headers: this.headers,
            body: JSON.stringify({
                prefix,
                domain: 'mail7.io'
            })
        });
        return response.json();
    }

    async checkInbox(emailAddress) {
        const response = await fetch(
            `${this.baseUrl}/inbox/${emailAddress}`,
            { headers: this.headers }
        );
        return response.json();
    }

    async getEmail(messageId) {
        const response = await fetch(
            `${this.baseUrl}/email/${messageId}`,
            { headers: this.headers }
        );
        return response.json();
    }
}

Error Handling

The API uses standard HTTP response codes:

  • 200 OK: Request successful
  • 400 Bad Request: Invalid parameters
  • 401 Unauthorized: Invalid API key
  • 404 Not Found: Resource not found
  • 429 Too Many Requests: Rate limit exceeded
  • 500 Server Error: Internal server error

Error Response Format

{
    "error": {
        "code": "invalid_request",
        "message": "Detailed error message"
    }
}

Best Practices

1. Rate Limiting

  • Implement exponential backoff for retries
  • Cache responses when possible
  • Monitor your API usage

2. Security

  • Never expose your API key in client-side code
  • Use environment variables for API keys
  • Implement proper error handling
  • Validate all input data

3. Performance

  • Implement connection pooling
  • Use appropriate timeout values
  • Implement proper error handling and retries

Webhooks

Mail7 supports webhooks for real-time email notifications:

// Example webhook handler
app.post('/webhook/mail7', (req, res) => {
    const { event, data } = req.body;
    
    switch(event) {
        case 'email.received':
            handleNewEmail(data);
            break;
        case 'email.read':
            handleEmailRead(data);
            break;
    }
    
    res.status(200).send('OK');
});

Start Testing Now

Ready to implement Email API Integration Guide in your application? Get started with Mail7 today: