# Mailgun API keys: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/mailgun
Last checked: 2026-10-25

Mailgun authenticates API calls with HTTP Basic auth, username `api` and an API key as the password. Account API keys (Account Settings → API Keys) can do everything; domain sending keys can only send for one domain. A key cannot be shown again after creation.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Account API key | — | Yes | All CRUD operations across the API and all sending domains | — |
| Domain sending key | — | Yes | POST to `/messages` and `/messages.mime` for the one domain it was created for | — |

## How to create a Mailgun key

1. Sign in to Mailgun.
2. For an account key, open **Account Settings → API keys** and click **Create key**.
3. For a sending key, open **Sending → Domains**, pick the domain, then **Domain Settings → Sending API keys → Add Sending Key**.
4. Copy the key. Mailgun cannot show it again; a lost key means creating a new one.

## Limiting what the key can do

Prefer a domain sending key for apps that only send mail. It is limited to message-sending endpoints on a single domain. Account keys have complete access and should be kept to administration.

## How to rotate a Mailgun key

1. Create a new key of the same type.
2. Update your apps and secret stores.
3. Delete the old key (the Keys API supports `DELETE /v1/keys/{key_id}`).

## How to revoke a Mailgun key

1. Delete the key through the API keys page or `DELETE /v1/keys/{key_id}`.

## If the key leaked

1. Delete the exposed key and create a replacement.
2. Update every app and secret store.
3. Review sending logs for unfamiliar messages.

## Gotchas

- Basic auth uses the literal username `api`.
- Web keys have a maximum validity of 1 day.
- The MCP server runs locally; Mailgun does not offer a hosted one. EU accounts need the region setting `MAILGUN_API_REGION`.

## Using this key with AI agents (BotPassword)

- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `http_request`: Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.
- `rotate`: Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
- `store_secret`: Saves a value the agent already holds as a new vault item.

## FAQ

### Where do I find my Mailgun API key?

Account keys are under Account Settings → API keys. Mailgun cannot show a key again after it is created.

### What is a Mailgun domain sending key?

A key limited to sending messages for one domain, made under Sending → Domains → Domain Settings → Sending API keys.

### How do I authenticate to the Mailgun API?

Use HTTP Basic auth with username `api` and the API key as the password.

### Can an AI agent use my Mailgun key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `run_with`, `http_request`, `store_secret`, so the agent gets the result and never the value.

## Sources

- https://documentation.mailgun.com/docs/mailgun/api-reference/authentication/
- https://documentation.mailgun.com/docs/mailgun/mcp
