Rotate › Mailgun

# 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.

Keysquid team · Updated December 1, 2026 · Checked against Mailgun's documentation and help centre on December 1, 2026

**Do this in two minutes, then never again.** Keysquid replaces your Mailgun key on a schedule, everywhere it's used, and kills a leaked one in seconds. From $9.99 a month. We never see the value.

[Try it free for 7 days](https://keysquid.com/signup)

## 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:**

1. 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.

What $9.99 a month saves you

One stolen key costs more than a lifetime of Keysquid.

- **$82,314 in 48 hours.** What a stolen Google API key ran up for a 3-person startup that normally spent about $180 a month ([The Register](https://www.theregister.com/2026/03/03/gemini_api_key_82314_dollar_charge/)).
- **$15,400 before the alert landed.** A solo developer revoked a leaked key within minutes of a billing alert, and the charges had already piled up ([TechRadar](https://techradar.com/pro/security/usd15k-bill-destroyed-a-solo-developers-startup-how-hackers-are-using-leaked-google-api-keys-to-go-wild-with-gemini-ai-for-free)).
- **$9.99 a month** for scheduled rotation and a one-click kill switch. **$19.99** adds the Security suite: spend guard, leak scanning, breach alerts, blast radius and compliance packs. Cancel any time.

[Start your 7-day free trial](https://keysquid.com/signup)No card needed to look around. Your keys stay encrypted to an agent you run.

## 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: `expiration` in seconds.
- **Auth**: HTTP Basic auth, username `api`, password = the key. EU accounts use `api.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:

1. Go to **Send → Sending → Domain settings**, pick the domain and open the **Sending API keys** tab.
2. Click **Add sending key**, enter a description that names the service and the date, and click **Create sending key**.
3. Copy the key from the pop-up straight into your secret store. Mailgun won’t show it again.
4. Deploy it wherever `MAILGUN_API_KEY` lives and send a test message.
5. 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`.

```bash
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

1. **Delete the key**, then create the replacement. Mailgun requires compromised keys to be deleted, not just replaced.
2. Reset the SMTP password for each domain and remove SMTP users you don’t recognise.
3. Reset the password of every user on the account and turn on two-factor authentication.
4. If Mailgun disabled the account, contact Mailgun support once the steps above are done so they can re-enable it.
5. Find where it leaked (a repository, a hosting provider, a client-side bundle) using [the first 10 minutes after a key leak](https://keysquid.com/blog/leaked-api-key-first-10-minutes), and scan your site with the [website leak scanner](https://keysquid.com/tools/website-leak-scanner).

## By hand vs. with Keysquid

### By hand

1. Create a sending key per domain and copy it once.
2. Update every service that sends mail.
3. Send a test, then delete the old key.
4. Remember which keys have an expiry coming up.

### With Keysquid

1. Keysquid creates the new key with the role and domain you set.
2. It writes the key to every platform you mapped and redeploys.
3. It deletes the old key after the grace period.
4. 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.

What $9.99 a month saves you

One stolen key costs more than a lifetime of Keysquid.

- **$82,314 in 48 hours.** What a stolen Google API key ran up for a 3-person startup that normally spent about $180 a month ([The Register](https://www.theregister.com/2026/03/03/gemini_api_key_82314_dollar_charge/)).
- **$15,400 before the alert landed.** A solo developer revoked a leaked key within minutes of a billing alert, and the charges had already piled up ([TechRadar](https://techradar.com/pro/security/usd15k-bill-destroyed-a-solo-developers-startup-how-hackers-are-using-leaked-google-api-keys-to-go-wild-with-gemini-ai-for-free)).
- **About 20 hours a year** of engineering time to rotate 10 keys by hand every 90 days, at roughly 30 minutes a key. That's around $2,000 at $100 an hour (our estimate).
- **$9.99 a month** for scheduled rotation and a one-click kill switch. **$19.99** adds the Security suite: spend guard, leak scanning, breach alerts, blast radius and compliance packs. Cancel any time.

[Start your 7-day free trial](https://keysquid.com/signup)No card needed to look around. Your keys stay encrypted to an agent you run.

## 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.

Rotate keys without ever handing them over

Keysquid rotates and revokes API keys on a schedule and encrypts every value to an agent you run, so we never see a readable key. Flat pricing from $9.99 a month, unlimited team members, 7-day free trial.

[Start 7-day free trial](https://keysquid.com/signup)

[Security model](https://keysquid.com/security)[Docs](https://keysquid.com/docs)

## Related

- [Rotate a SendGrid API key](https://keysquid.com/rotate/sendgrid)
- [Rotate a Twilio Auth Token and API keys](https://keysquid.com/rotate/twilio)
- [Rotation support for every provider](https://keysquid.com/rotate)
- [Check SPF, DKIM and DMARC for your domain](https://keysquid.com/tools/email-security)
- [OpenAI key rotation](https://keysquid.com/rotate/openai)
- [Anthropic key rotation](https://keysquid.com/rotate/anthropic)

## Sources

1. [Mailgun docs: API key management and roles](https://documentation.mailgun.com/docs/mailgun/user-manual/api-key-mgmt/rbac-mgmt)
2. [Mailgun API: Create Mailgun API key (POST /v1/keys)](https://documentation.mailgun.com/docs/mailgun/api-reference/send/mailgun/keys/post-v1-keys)
3. [Mailgun API: Delete key (DELETE /v1/keys/{key_id})](https://documentation.mailgun.com/docs/mailgun/api-reference/send/mailgun/keys/delete-v1-keys--key-id-)
4. [Mailgun help: Where can I find my API keys and SMTP credentials?](https://help.mailgun.com/hc/en-us/articles/203380100-Where-can-I-find-my-API-keys-and-SMTP-credentials)
5. [Mailgun help: API Key Roles](https://help.mailgun.com/hc/en-us/articles/26016288026907-API-Key-Roles)
6. [Mailgun help: What can I do if my account has been compromised?](https://help.mailgun.com/hc/en-us/articles/360012008114-What-can-I-do-if-my-account-has-been-compromised)
