AI Brief Practical fixes, setup guides and tool comparisons
Troubleshooting

Mailgun API 401 in n8n: "Invalid private key" vs "unauthorized" vs "Forbidden" vs "Need authorization header"

Mailgun answers a bad API key with four different 401 bodies depending on the endpoint and the header. Fake-key tests show which is which, plus a checklist.

Short answer: Mailgun returns HTTP 401 for every authentication failure, but the body changes: {"message":"Invalid private key"}, {"Error":"unauthorized"}, {"message":"Need authorization header"}, or the plain text Forbidden. Which one you get depends on the endpoint and on how the header is malformed, not only on whether the key is wrong. I sent requests with made-up keys and recorded each case. No real key was used, and no email was sent.

The four bodies

BodyContent typeWhen I saw it
{"message":"Invalid private key"}JSONFake key with username api on /v3/domains, /v4/domains and /v4/address/validate
{"Error":"unauthorized"}JSONNo credentials at all, a Bearer header, a malformed Basic header, and a fake key on the events endpoint
{"message":"Need authorization header"}JSONUsername api with an empty password, or an empty username with a password
Forbiddenplain textPOST /v3/{domain}/messages, with or without a fake key

All of them came back as HTTP 401, and the response header was WWW-Authenticate: Basic realm="MG API". Note the different capitalization: the first and third bodies use a lowercase message key, while the second uses Error with a capital E. A parser that reads message will find nothing in the "unauthorized" case.

What I tested

On November 18, 2026 I sent requests to api.mailgun.net and api.eu.mailgun.net with invented credentials. The key looked like key- followed by 32 characters, plus a second one in a newer-looking format. The domain was fake-domain.example.

RequestHTTPBody
GET /v3/domains, no credentials401{"Error":"unauthorized"}
GET /v3/domains, api + fake key401{"message":"Invalid private key"}
Same, newer-looking fake key format401{"message":"Invalid private key"}
Same, but username user instead of api401{"message":"Invalid private key"}
api + a 5-character password401{"message":"Invalid private key"}
api + empty password401{"message":"Need authorization header"}
Empty username + fake key401{"message":"Need authorization header"}
Authorization: Bearer ...401{"Error":"unauthorized"}
Malformed Basic value (not base64)401{"Error":"unauthorized"}
Basic value with no colon (api alone)401{"Error":"unauthorized"}
Key in the query string (?api_key=)401{"Error":"unauthorized"}
GET /v3/{domain}/events, api + fake key401{"Error":"unauthorized"}
POST /v3/{domain}/messages, no credentials, or api + fake key401Forbidden (plain text)
Fake key, path that does not exist404404 page not found (plain text)
  • The same fake key got "Invalid private key" on one endpoint and "unauthorized" on another. Domains and address validation said "Invalid private key". Events said "unauthorized", the same text as sending no key. So "unauthorized" does not prove that no key was sent.
  • The send endpoint answered in plain text. POST /messages returned the word Forbidden under HTTP 401, not 403. If your n8n node parses the response as JSON, this is the case that can show up as a parsing error instead of an auth error. I did not test this inside n8n.
  • The username was ignored for the "Invalid private key" check. Using user instead of api still gave "Invalid private key", so a wrong username is not reported separately.
  • "Need authorization header" appeared even when a header was sent. A Basic header with an empty password, or with an empty username, produced it. That wording suggests a missing header, but the header was present in both tests.
  • US and EU hosts behaved the same. The first ten requests I made (no credentials, fake keys, wrong username, empty password, events, messages, a path that does not exist, /v4/domains) gave the same result on api.mailgun.net and api.eu.mailgun.net. I ran the Bearer, malformed Basic, query-string and address-validation cases only on the US host. I could not test whether a real key from one region is rejected by the other.

What the docs say

The Mailgun authentication reference says the API uses HTTP Basic Auth with the username api and your API key as the password. It describes two key types: the primary account API key, which can do everything for all your sending domains, and domain sending keys, which can only send mail through the messages endpoints, and only for their own domain. It does not list the 401 bodies above, so the table is my observation, not documented behavior.

The n8n Mailgun credentials page says the credential needs three fields: the API domain (choose the EU endpoint if your account is in Europe), the email domain you send from, and the API key. If you have several sending domains, it says to make one credential per domain.

Checklist when you see a Mailgun 401

  1. Do not trust the wording of the body. "unauthorized" and "Forbidden" can both mean a wrong key, not only a missing one. Check the credential in n8n first.
  2. Re-copy the API key. A stray space, a line break, or a key that was replaced are plausible causes. I could only test fake keys, so I could not tell which cause produces which body.
  3. Pick the right API domain in n8n. The n8n page says to choose EU for European accounts. I could not test what a real key does on the wrong region.
  4. Key type versus endpoint. According to the Mailgun docs a domain sending key only works for sending on its own domain. If you use one for a listing or events call, a failure is plausible, but I did not test it.
  5. Header form. It must be Basic auth with the username api. Bearer and a query-string key both gave "unauthorized" in my tests.
  6. Never paste a real key in a public forum. The response bodies above are safe to share. The key is not.

Other email-service APIs show the same kind of split. I wrote up SendGrid API 401 and Twilio error 20003 from the same kind of fake-credential tests.

What I did not test

  • No real Mailgun key was used. I did not see a successful response, a domain sending key on the wrong endpoint, a key from the wrong region, a rotated key, a suspended account, or an unverified-domain error.
  • Only a few endpoints were tried. The messages call used a made-up domain, so it could not have sent anything.
  • I did not test inside n8n. All requests were made with curl, so I cannot say how n8n shows these bodies.
  • Webhook signing keys and sandbox domains are not covered.
  • This is one day of observation, and Mailgun can change its responses.

Sources: Mailgun API authentication and n8n Mailgun credentials. Tables and response bodies are from my own requests on November 18, 2026.

Updated: Nov 17, 2026

Comments

Loading comments…