How to rotate a Mailgun API key
Mailgun lets you hold several API keys at once, so rotation is create, deploy, delete. The decisions that matter are which kind of key the new one should be, which role it gets (you can't change it afterwards), and whether you also need to reset the webhook signing key and SMTP passwords.
How to rotate Mailgun keys with Keysquid
Automatic Keysquid creates, verifies, distributes and revokes Mailgun keys on a schedule, with no one in the loop. Account or domain-scoped sending keys through the Keys API.
Setup, as the app shows it:
- Create an Admin-role key in Mailgun → API Security for Keysquid.
- Writes to: API key (default variable MAILGUN_API_KEY), in every platform and secret store you map it to.
- Interval: from every 1 hour to every 4 weeks, subject to your plan’s minimum.
- Limits Keysquid applies: Mailgun can expire each key on its own.
- Inventory: Keysquid lists every Mailgun key the connection can see, including keys it doesn’t manage, with names and last use.
Every rotation runs in the same order: create the new key, verify it, write it everywhere, redeploy where a platform needs it, and only then revoke the previous key after a grace period. If any write fails, everything written so far is restored and the new key is revoked. The value is encrypted to the agent you run, so Keysquid’s servers never see it.
Under the hood: Mailgun keys in detail
Everything below is what Keysquid handles for you on every rotation. It’s here so you know exactly what happens to your keys, and in case you ever need to do a step by hand.
- Where
- Profile menu (top right) → API Security. Add new key, give it a description and a role, then Create Key.
- Shown once
- Mailgun shows a new key once, in a pop-up after creation. Copy it before closing.
- Kinds
- Account API keys (account-wide, role-based) and domain sending keys (send mail for one domain only).
- Roles
- Admin, Developer, Support and Analyst on account keys. Free and Basic plans can only create Admin keys.
- Role is fixed
- A key's role can't be edited. To change permissions, create a new key and delete the old one.
- Overlap
- Yes: keys are independent, so the old and new key both work until you delete the old one.
- Expiry
- Optional, set at creation through the API:
expirationin seconds. - Auth
- HTTP Basic auth, username
api, password = the key. EU accounts useapi.eu.mailgun.net. - Keysquid
- Automatic: domain sending keys by default, with an optional expiry, and the old key deleted.
Which kind of key should the new one be?
Most leaked Mailgun keys are account keys that only ever needed to send mail. Rotation is the moment to downgrade them, because the replacement can be a different kind of key with no change to your sending code: both authenticate the same way.
| Key | What it can do | Use it for |
|---|---|---|
| Domain sending key | Send messages for the one domain it was created on (/messages and /messages.mime) | Apps that only send mail. One key per domain and service. |
| Account key, Developer | Read and write domains, messages, webhooks, logs and suppressions; read-only on keys and SMTP credentials | Deploy scripts and integrations that manage domains or webhooks |
| Account key, Support | Read most things, write to bounces, complaints and unsubscribes | Support tooling that clears suppressions |
| Account key, Analyst | Read-only data and metrics (basic in the API) | Dashboards and reporting |
| Account key, Admin | Everything, including creating and deleting other keys | Only the tool that manages keys |
Role permissions as listed in Mailgun's API key management docs. Role choice depends on your plan; on Free and Basic every account key is Admin.
Mailgun’s own advice matches this: if an application only sends email, give it a domain sending key rather than an account key. On a Free or Basic plan that’s also the only way to give an app less than full access.
Rotate a key in the Mailgun control panel
For a domain sending key:
- Go to Send → Sending → Domain settings, pick the domain and open the Sending API keys tab.
- Click Add sending key, enter a description that names the service and the date, and click Create sending key.
- Copy the key from the pop-up straight into your secret store. Mailgun won’t show it again.
- Deploy it wherever
MAILGUN_API_KEYlives and send a test message. - Delete the old key. If it was an account key, do that on API Security: click its trash icon, then Delete.
For an account key, the same flow runs from the API Security page under the profile menu: Add new key, choose the role, Create Key. Only users with the Admin role can create keys. The page lists keys by ID, description and created date, never by secret, so a good description is the only way to tell later which key a service uses.
Rotate a key with the Keys API
The Keys API needs an account key with write access to keys, which in practice means an Admin key. Requests are multipart/form-data.
MG=https://api.mailgun.net # EU accounts: https://api.eu.mailgun.net
# 1. Create a domain sending key that expires in 90 days (7,776,000 seconds)
curl -s -u "api:$MAILGUN_ADMIN_KEY" "$MG/v1/keys" \
-F role=sending -F kind=domain -F domain_name=mg.example.com \
-F description="web-app 2026-12" -F expiration=7776000 | jq -r '.key.id, .key.secret'
# 2. Deploy the new secret and send a test, then find and delete the old key
curl -s -u "api:$MAILGUN_ADMIN_KEY" "$MG/v1/keys?kind=domain&domain_name=mg.example.com" | jq '.items[] | {id, description, created_at}'
curl -s -u "api:$MAILGUN_ADMIN_KEY" -X DELETE "$MG/v1/keys/$OLD_KEY_ID"For an account key, use kind=user and one of admin, developer or basic (the API name for Analyst) as the role. A successful delete returns {"message": "key deleted"}; an unknown ID returns 404, so the call is safe to retry.
Use expiry as a backstop, not a schedule. A key with expiration stops working on its own, which is ideal for keys that should never outlive a project. But if nothing replaces it in time, mail stops too. Pair it with a rotation that runs well before the deadline.
The webhook signing key and SMTP passwords rotate separately
Mailgun signs every webhook it sends with the account’s HTTP webhook signing key, shown on the same API Security page. It isn’t an API key and isn’t touched when you rotate one. To reset it, click the refresh icon in its section, then Reset Key. There is one signing key per account and no overlap period, so update the value your webhook handler checks straight after the reset: webhooks signed with the new key fail verification until you do.
If your app sends over SMTP rather than the HTTP API, the credential is the domain’s SMTP password, not an API key. Reset it under Send → Sending → Domain settings → SMTP credentials with Reset password; the new password is also shown only once.
If a Mailgun key leaked
- Delete the key, then create the replacement. Mailgun requires compromised keys to be deleted, not just replaced.
- Reset the SMTP password for each domain and remove SMTP users you don’t recognise.
- Reset the password of every user on the account and turn on two-factor authentication.
- If Mailgun disabled the account, contact Mailgun support once the steps above are done so they can re-enable it.
- Find where it leaked (a repository, a hosting provider, a client-side bundle) using the first 10 minutes after a key leak, and scan your site with the website leak scanner.
By hand vs. with Keysquid
By hand
- Create a sending key per domain and copy it once.
- Update every service that sends mail.
- Send a test, then delete the old key.
- Remember which keys have an expiry coming up.
With Keysquid
- Keysquid creates the new key with the role and domain you set.
- It writes the key to every platform you mapped and redeploys.
- It deletes the old key after the grace period.
- It lists every key on the account with its role and domain.
What Keysquid does on every rotation
Keysquid holds the Admin key and hands your apps narrower keys: sending keys need a domain, and other roles become account keys. If you turn on expiry, it’s set on each new key at creation, so a key Keysquid made can never outlive its schedule by more than that window. Keysquid doesn’t reset the webhook signing key or SMTP passwords; do those by hand as above.
Frequently asked questions
Can I rotate a Mailgun API key without downtime?
Yes. An account can hold several keys, so create the new key, deploy it, send a test, and only then delete the old one. Both work in the meantime.
What is a Mailgun domain sending key?
A key created on one sending domain that can only send messages for that domain. It's the right key for an app that just sends mail, and Mailgun recommends it over an account key for that job.
Can I change the role of an existing Mailgun API key?
No. Roles are fixed at creation. Create a new key with the role you want, move your apps to it and delete the old key.
Can a Mailgun API key expire automatically?
Yes, if you set expiration (in seconds) when you create it through the Keys API. Expiry is set at creation; the Keys API documents no way to add one to an existing key.
Does rotating my API key change the webhook signing key?
No. The HTTP webhook signing key is separate. Reset it on the API Security page and update your webhook handler at the same moment.
Why can I only create Admin keys?
Accounts on Mailgun's Free or Basic plans can't choose a role other than Admin. Use domain sending keys to give apps less access.