> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lettr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Security Best Practices

> Protect your Lettr account, API keys, and email data by storing keys in environment variables, securing webhooks, and limiting key permissions.

Email infrastructure handles sensitive data and has direct access to your recipients' inboxes. A compromised API key or insecure webhook endpoint can lead to unauthorized emails sent on your behalf, leaked recipient data, or abuse of your sending reputation. This guide covers the practical steps to lock down your Lettr integration.

***

## API Key Security

Your API key is the single credential that authorizes requests to the Lettr API. Treat it like a database password.

### Store Keys in Environment Variables

Never hardcode API keys in your source code. Use environment variables instead:

```bash theme={null}
# .env file (never committed to version control)
LETTR_API_KEY=lttr_xxxxxxxxxxxx
```

```javascript theme={null}
// Access via environment variable
const apiKey = process.env.LETTR_API_KEY;
```

```bash theme={null}
# Use in curl requests
curl -X POST https://app.lettr.com/api/emails \
  -H "Authorization: Bearer $LETTR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"from": "you@example.com", "to": ["user@example.com"], "subject": "Hello", "html": "<p>Hello</p>"}'
```

### Keep Keys Out of Version Control

Add your environment files to `.gitignore` before your first commit:

```bash theme={null}
# .gitignore
.env
.env.local
.env.production
```

> [!WARNING]
>
> If you've already committed a file containing an API key, adding it to `.gitignore` won't remove it from your git history. Rotate the key immediately in the Lettr dashboard and consider using a tool like `git filter-branch` or BFG Repo-Cleaner to purge the key from your history.

### Use the Right Permission Level

Lettr API keys have two permission levels:

| Permission Level | Use Case |
| - | - |
| **Sending Only** | Application code that only needs to send emails — use this for production services |
| **Full Access** | Admin tasks like managing domains, templates, and account settings |

> [!TIP]
>
> Default to **Sending Only** keys for any service that just sends email. This limits the damage if a key is ever exposed — an attacker could send emails, but couldn't access your templates, domains, or account settings.

See [API Key Permissions](https://docs.lettr.com/learn/api-keys/permissions.md) for details on what each level can access.

### Use Separate Keys Per Environment

Create distinct API keys for each environment:

* **Development**: a [sandbox key](https://docs.lettr.com/learn/api-keys/sandbox.md) (`lttr_sandbox_` prefix), which delivers every email to your own inbox
* **Staging**: a Sending Only key for pre-production testing
* **Production**: a Sending Only key for live email sending
* **Admin**: a Full Access key used only for dashboard-level operations

If one environment is compromised, you only need to rotate that key without affecting other environments.

### Rotate Keys Regularly

Rotate your API keys periodically and immediately if you suspect exposure. You can create new keys and revoke old ones from the Lettr dashboard without downtime — create the new key first, update your environment variables, then revoke the old key.

***

## Webhook Security

Webhooks push event data to your server. Without verification, anyone who discovers your webhook URL could send fake events to your application.

### Authenticate Webhook Requests

When you create a webhook, choose **Basic** or **OAuth 2.0** authentication rather than none. Lettr then sends credentials with every webhook request, and your handler should reject any request without them:

```javascript theme={null}
const crypto = require('crypto');
const express = require('express');
const app = express();

function hasValidCredentials(req) {
  const header = req.headers.authorization || '';
  const expected = 'Basic ' + Buffer.from(
    `${process.env.WEBHOOK_USERNAME}:${process.env.WEBHOOK_PASSWORD}`
  ).toString('base64');

  // Constant-time comparison to avoid timing attacks
  return header.length === expected.length &&
    crypto.timingSafeEqual(Buffer.from(header), Buffer.from(expected));
}

app.post('/webhooks/lettr', express.json(), (req, res) => {
  if (!hasValidCredentials(req)) {
    console.error('Invalid webhook credentials');
    return res.sendStatus(401);
  }

  // Credentials are valid; safe to process
  handleEvents(req.body);

  res.sendStatus(200);
});
```

> [!WARNING]
>
> A webhook with no authentication accepts events from anyone who discovers its URL. Use a long, random password for Basic auth and store it in environment variables, not in your source code.

### Use HTTPS for Webhook Endpoints

Always use an `https://` URL for your webhook endpoint. HTTP endpoints transmit event data — including recipient email addresses — in plain text across the network.

See [Webhook Authorization](https://docs.lettr.com/learn/webhooks/authorization.md) for Basic and OAuth 2.0 setup and verification examples.

***

## Account Security

### Enable Two-Factor Authentication

Enable 2FA for every team member in the Lettr dashboard under **Settings** → **Two-Factor Auth**. This protects against password compromise: even if an attacker obtains a password, they can't access the dashboard without the second factor.

> [!NOTE]
>
> 2FA is configured per user in the dashboard. If you own the team, turn on **Require Two-Factor Authentication** under **Settings** → **Team** so every member must enable it before accessing team resources.

### Review Team Access

Periodically review who has access to your Lettr account through the dashboard:

* Remove team members who no longer need access
* Verify that each member's role matches their current responsibilities
* Audit when new members were added and by whom

### Configure IP Restrictions

If your API requests originate from known IP addresses (such as production servers), set **Allowed IPs** on the API key in the Lettr dashboard to reject requests from unrecognized sources. This adds a layer of defense even if an API key is leaked.

***

## Data Protection

### Minimize Sensitive Data in Emails

Email is not a secure transport channel. Avoid including sensitive information directly in email content or metadata:

| Don't Include | Do Instead |
| - | - |
| Full credit card numbers | Last four digits only |
| Passwords or secrets | A link to securely reset or retrieve |
| Social security numbers | Reference an account number |
| Full API keys or tokens | A masked version with a link to the dashboard |
| Medical or financial details | A notification to log in and view securely |

### Be Careful with Metadata and Custom Headers

Any data you attach to an email — custom headers, metadata fields, tags — is stored by Lettr and may appear in logs, webhooks, and dashboard views. Don't use metadata fields to store passwords, tokens, or personally identifiable information that isn't necessary for email delivery.

> [!TIP]
>
> If you need to correlate emails with internal records, use an opaque identifier (like a UUID) rather than embedding sensitive data directly.

***

## Monitoring and Incident Response

### Use Webhooks to Detect Unusual Activity

Set up webhook handlers to flag abnormal patterns:

```javascript theme={null}
// Uses hasValidCredentials() from the webhook authentication example above
app.post('/webhooks/lettr', express.json(), (req, res) => {
  if (!hasValidCredentials(req)) {
    return res.sendStatus(401);
  }

  // Lettr delivers webhook events as a JSON array
  for (const event of req.body) {
    const data = event.msys?.message_event;

    // Hard bounce classes: 10 (invalid recipient), 30 (no RCPT), 90 (unsubscribe)
    if (data?.type === 'bounce' && [10, 30, 90].includes(parseInt(data.bounce_class))) {
      trackBounce(data.rcpt_to);

      // Alert if hard bounces spike; could indicate a compromised key
      // sending to purchased or invalid lists
      if (getRecentBounceCount() > BOUNCE_THRESHOLD) {
        alertSecurityTeam('Unusual hard bounce spike detected');
      }
    }
  }

  res.sendStatus(200);
});
```

### Review Dashboard Logs Regularly

Check the Lettr dashboard periodically for:

* **Unexpected sending volume** — a spike could mean a compromised key
* **Emails to unfamiliar recipients** — could indicate unauthorized use
* **Failed authentication attempts** — someone may be trying stolen credentials
* **New API keys you didn't create** — another team member may need to verify, or an account may be compromised

> [!NOTE]
>
> If you discover any sign of unauthorized access, rotate all API keys immediately and contact [support@lettr.com](mailto:support@lettr.com).

***

## Security Checklist

Use this checklist when setting up a new Lettr integration or auditing an existing one:

**1. Store API keys in environment variables**

Move all API keys out of source code and into environment variables. Verify your `.env` files are in `.gitignore`.

**2. Use Sending Only keys for application code**

Check that production services use Sending Only keys, not Full Access keys. Reserve Full Access for admin operations only.

**3. Use separate keys per environment**

Create distinct keys for development, staging, and production so a compromise in one doesn't affect the others.

**4. Authenticate webhook requests**

Confirm your webhooks use Basic or OAuth 2.0 authentication and that your handler rejects requests without valid credentials before processing any event.

**5. Use HTTPS for all endpoints**

Ensure your webhook URLs and any client-side requests use HTTPS, not HTTP.

**6. Enable 2FA for all team members**

Verify every user on your Lettr account has two-factor authentication enabled in the dashboard.

**7. Audit team access**

Review the team members list in the dashboard and remove anyone who no longer needs access.

**8. Remove sensitive data from email content**

Audit your email templates to ensure they don't contain passwords, full card numbers, or other sensitive data.

**9. Set up monitoring alerts**

Configure webhook handlers to detect unusual bounce rates, sending volume, or other anomalies.

***

## Related Topics

> [card] [API Key Permissions](https://docs.lettr.com/learn/api-keys/permissions.md)
>
> Understand Full Access vs Sending Only permission levels

> [card] [Webhook Authorization](https://docs.lettr.com/learn/webhooks/authorization.md)
>
> Authenticate webhook requests with Basic or OAuth 2.0 credentials

> [card] [Authentication Troubleshooting](https://docs.lettr.com/knowledge-base/troubleshooting/authentication.md)
>
> Diagnose and fix API authentication errors

> [card] [Letting AI Agents Send Email Safely](https://docs.lettr.com/knowledge-base/use-cases/ai-agent-email.md)
>
> Give an AI agent restricted access to Lettr over MCP.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.