Email, messaging and chat

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

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.

Mailgun key types

TypePrefixSecret?ScopeExpiry
Account API key
Number of keys depends on plan. Created via the Create key button on the API keys page.
—YesAll CRUD operations across the API and all sending domains—
Domain sending key
Created under Sending → Domains → Domain Settings → Sending API keys.
—YesPOST 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}).

The docs describe no in-place regenerate for private account or sending keys; only the public key can be regenerated (POST /v1/keys/public).

How to revoke a Mailgun key

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

If your Mailgun 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.

Things that catch people out

  • 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 your Mailgun key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a Mailgun key, these intents fit:

IntentWhat BotPassword does
run_withRuns 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_requestMakes 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.
rotatePartly 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_secretSaves a value the agent already holds as a new vault item.

Mailgun also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

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.

Stop pasting Mailgun keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.