Security

How to rotate a leaked API key

Short answer

If a key leaks, create a new key with the same permission and domain lock, put it in your app's secrets and deploy, then revoke the old key under API keys. Revoking takes effect at once, and anything still using the old key gets 401. Then check Emails for sends you did not make and the audit log for keys or webhooks you did not create. If you think the key is being abused right now, revoke it first.

Last checked 16 November 2026

Keys leak in ordinary ways: a commit to a public repository, a screenshot, a log file, a support ticket. Rotating takes a few minutes.

1. Decide how urgent it is

  • Signs of abuse (emails you did not send, bounce spikes): revoke the old key first (step 4), then create and deploy a new one. Your app cannot send for a few minutes, but nobody else can either.
  • No signs of abuse: follow the steps in order, so your app never stops.

2. Create the replacement

Open API keys and press Create API key. Give it the same permission and the same Limit to one domain as the leaked key, and a name that shows it is new, for example “Production web 2026-11”. Copy the key (it starts with mc_live_ and is shown once).

With the API (full-access key that is not the leaked one):

curl https://api.mailcandor.com/v1/api-keys \
  -H "Authorization: Bearer $MAILCANDOR_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "Production web 2026-11", "permission": "sending"}'

3. Deploy the new key

Update every place the old key lived:

  • environment variables or the secrets store (MAILCANDOR_KEY),
  • SMTP settings, where the password is the key,
  • CI secrets, staging, background workers.

Check the new key works:

curl https://api.mailcandor.com/v1/whoami -H "Authorization: Bearer $NEW_KEY"

On the API keys page, the new key’s Last used should change from Never, and the old key’s should stop moving.

4. Revoke the leaked key

In the dashboard: API keys → Revoke on the old key → Revoke key. “Anything still using this key stops working at once. This cannot be undone.”

With the API:

curl -X DELETE https://api.mailcandor.com/v1/api-keys/$OLD_KEY_ID \
  -H "Authorization: Bearer $MAILCANDOR_ADMIN_KEY"

The answer is 204. Find the id with GET /v1/api-keys; the prefix (for example mc_live_a1b2c3) helps you match it.

5. Look for misuse

  • Emails: filter by date and look for subjects or recipients you do not recognise.
  • Deliverability: a sudden rise in bounces or complaints.
  • Audit log (admins): filter API keys for api_key.created you did not make, and Webhooks for new endpoints. A full-access key could have done either. Revoke and delete anything unknown.
  • Webhooks: check every endpoint URL is yours.

If a leaked key sent mail you did not authorise, write to hello@mailcandor.com with the time window, so we can help protect your sending reputation.

6. Remove the key from where it leaked

Revoked keys cannot be used again, but clean up anyway: delete the file or message, and if it was committed, remove it from the repository history so it does not trigger alerts later.

Prevent the next one

  • Use sending-only keys, locked to a domain, in apps. A leaked one can only send as that domain.
  • One key per place, named after it.
  • Keep keys in a secrets store and add secret scanning to your repository.

See API key best practices.

If it does not work

  • “This is the key making the request.” You tried to revoke the leaked key with itself. Use another full-access key or the dashboard.
  • 403 forbidden “The person who created this API key cannot manage keys on this team any more.” Use a key made by a current admin or developer, or the dashboard.
  • App gets 401 after revoking. Some place still has the old key. Search your config for its prefix.

Key endpoints are in the API reference.

Questions

Is there a rotate button that keeps the same key working for a while?

No. Rotation is create a new key, switch to it, revoke the old one. Revoking is immediate.

Should I revoke first or deploy first?

If the key is being abused, revoke first and accept a short outage. Otherwise create and deploy the new key first, then revoke, so nothing breaks.

Can I revoke a key using that same key?

No. The API refuses to revoke the key making the request. Use another full-access key or the dashboard.

Does rotating the key change my SMTP settings?

Yes. The SMTP password is the API key, so update it wherever SMTP is configured.

Related

Send with someone on your side.

Unlimited domains on every plan, a person before any block, and 3,000 emails a month free. Tell us what you send and we set you up within one business day.