How-To

Mailgun API error: 401 Forbidden and invalid parameters, fixed

Mailgun's 401 Forbidden usually means your API key, sending domain and regional base URL (US or EU) don't agree. "Need at least one of 'text' or 'html'" means no body field reached Mailgun, often because the request was JSON instead of form data. Here are the causes and fixes.

Sohail HussainSohail Hussain9 min read

Short answer

  • 401 Forbidden means authentication failed: your API key, sending domain and regional base URL (api.mailgun.net or api.eu.mailgun.net) do not match. Use the username api and the key as the password.
  • "Need at least one of 'text' or 'html'" is a 400: the request got through, but no body field was read. Send form data, not JSON, with a text or html field.

You probably pasted this into a search box:

The parameters passed to the API were invalid. Check your inputs! Need at least one of 'text' or 'html' parameters specified

Or you got a bare Forbidden with a 401 status. These are different problems that tend to appear in the first hour of a Mailgun integration. This page covers both, plus the sandbox 403 and the invalid address error.

Which error you have

Match on the status code first, because Mailgun's wording can change.

What you seeStatusMost likely cause
Forbidden401Wrong key, key from another account, wrong region, or a malformed auth header
Domain not found404The domain in the URL is not on the account or region you are calling
Sandbox subdomains are for test purposes only...403Sandbox domain sending to a recipient who is not an authorized recipient
Need at least one of 'text' or 'html' parameters specified400Body missing, empty, or sent as JSON
'from' parameter is missing or 'to' parameter is missing400Same encoding problem, or a misspelled field name
to parameter is not a valid address. please check documentation400Malformed address, or a character damaged by encoding

Why it happens

Most of these come from setup mismatches, not from a Mailgun outage.

  1. Region mismatch. Mailgun's base URL is https://api.mailgun.net/ for the US region and https://api.eu.mailgun.net/ for the EU region; AU is contract only. A key and domain from one region will not work against another.
  2. Wrong key or domain. The key belongs to another account, or it is a domain-specific sending key used on a different domain. In the Control Panel you can create an API key from API Security, or a domain-specific Sending API Key from your sending domain settings.
  3. A hand-built auth header. Mailgun uses HTTP Basic authentication; the username is always api and the password is your API key. If you build the header yourself, the value after Basic must be the Base64 encoding of api:YOUR_KEY, not the raw key.
  4. JSON instead of form data. Mailgun's own Supabase guide says in a code comment that Mailgun requires FormData, not JSON. A JSON body arrives with no form fields, so you see missing text, from or to errors.
  5. The sandbox restriction. On the Free plan without credit card information, or when using the sandbox domain, you can only send to Authorized Recipients.
  6. Encoding damage to addresses. A + in a hand-built form string becomes a space, and stray whitespace from a spreadsheet breaks the address.

Fix 401 Forbidden: key, domain and region must match

Start with a request that is known to be correct, using real values:

curl -s --user 'api:YOUR_API_KEY' \
  https://api.mailgun.net/v3/mg.example.com/messages \
  -F from='Sender <sender@mg.example.com>' \
  -F to='recipient@example.org' \
  -F subject='Test' \
  -F text='Hello'

If it returns 401, work through these in order:

  1. Confirm your region. US accounts use app.mailgun.com and EU accounts use app.eu.mailgun.com. If you sign in on the EU address, change your code to https://api.eu.mailgun.net/v3/mg.example.com/messages.
  2. Copy the key again. Create or copy a key in the Control Panel and paste it straight into the curl command. Do not reuse the value from your app config for this test.
  3. Check the domain in the URL. Use the domain exactly as the dashboard lists it, such as mg.example.com, not your root domain unless that is what you added. A 404 Domain not found points here.
  4. Look for invisible characters. A trailing newline from an .env file, or quotes that survived parsing, will break the key. Print the key length in your app and compare it with the dashboard value.
  5. Set the username explicitly. In your HTTP client, the username must be api. Your account email as the username fails.
  6. Check for rotation. If someone regenerated or deleted the key, the old value in your config no longer works.

Never paste the key into a ticket or thread

Share the status code, response body and domain, but never the API key itself. If a key has been posted anywhere public, rotate it in the Control Panel.

Fix the sandbox 403

Every new account gets a sandbox domain that looks like sandboxXXXX.mailgun.org. You have two ways out.

Option 1: add authorized recipients

Mailgun's Authorized Recipients help page describes the steps:

  1. Log in to the Mailgun Control Panel.
  2. Click Domain settings in the left menu (expand Send first if needed; it sits in the Sending section).
  3. In the Domain dropdown near the top right, select the sandbox domain, the long name starting with sandbox.
  4. Open the Setup tab, enter the recipient's address and click Add.
  5. Ask the recipient to click the activation link Mailgun emails them.

The same page says up to 5 Authorized Recipients can be added, and a recipient becomes "Verified" after clicking the activation link; until then they are "Unverified." Only users with the Administrator or Developer role can set these up.

Option 2: send from your own domain

For regular, production-level sending, you need a domain from a DNS provider, set up with Mailgun. Add it in the Control Panel, publish the DNS records Mailgun shows you at your registrar, and wait for verification. Then change the domain in your API URL from the sandbox to the real one.

The URL decides the sending domain

Changing the from address does nothing if the URL path still contains the sandbox domain. Check the path first.

Fix "Need at least one of 'text' or 'html'"

The request reached Mailgun, but no text or html field came through. Check these in order.

  1. Switch to form encoding. The messages endpoint reads multipart/form-data or application/x-www-form-urlencoded. In Node's fetch, send a FormData or URLSearchParams object. In Python requests, pass data= and not json=.
import requests

r = requests.post(
    "https://api.mailgun.net/v3/mg.example.com/messages",
    auth=("api", "YOUR_API_KEY"),
    data={
        "from": "Sender <sender@mg.example.com>",
        "to": ["recipient@example.org"],
        "subject": "Test",
        "text": "Hello",
    },
)
print(r.status_code, r.text)
  1. Use the right field names. The fields are text and html. Names like body, message, content or htmlBody are ignored.
  2. Log the final payload. A template variable may have resolved to an empty string. Print the payload right before the HTTP call.
  3. Check template sends. If you use the template parameter, confirm the template exists on that domain and the name is spelled exactly.
  4. Update the SDK. A Mailgun PHP library issue on GitHub has reported this exact message. Update the library, then compare what it sends with the curl request above.

Fix the invalid address error

The message refers to a to, cc or bcc value. Check these:

  • Plus signs. In form encoding, an unescaped + reads as a space, so name+tag@example.com arrives as name tag@example.com. Let your HTTP client encode values instead of building the body by hand.
  • Display names. Name <address@example.com> is fine. An unquoted comma or an unbalanced angle bracket in the name is not.
  • Multiple recipients. Use a comma-separated string or repeat the to field. Do not send a JSON-style array as one string.
  • Whitespace and line breaks copied from a spreadsheet or CSV.
  • Empty values. An empty array element or null becomes a blank to.

A syntax check does not tell you whether the mailbox exists. That only shows up later as a bounce.

No HTTP status at all

Mailgun announced a change to the TLS cipher suites on api.mailgun.net and api.eu.mailgun.net for early April 2026. A client limited to the retired ciphers fails before any 401 or 400 is returned. See Mailgun's TLS Cipher Suites help article if you get connection errors.

Checklist before you try again

  • The base URL matches your region (api.mailgun.net or api.eu.mailgun.net).
  • The domain in the URL path is the one you want to send from.
  • The auth username is api and the password is a current key with no trailing space or newline.
  • The body is form data with from, to, subject and text or html.
  • If you use the sandbox, every recipient shows as Verified.

If nothing works

Run the curl command with a freshly created key, the correct regional URL and a verified domain. If it still returns 401, contact Mailgun support from the Control Panel. For a new account, mention whether a credit card is on file, because Mailgun's help pages tie sandbox-only limits to the Free plan without one. Mailgun's start sending guide lists the regional base URLs if you need to double-check.

Include:

  • the sending domain and region
  • the UTC timestamp of the failed request
  • the HTTP status code and full response body
  • the exact curl command with the key removed
  • what you already tried
Subject: API returns 401 Forbidden on /v3/{domain}/messages

Hello,

Sending through the Mailgun API fails with 401 Forbidden.

Domain: mg.example.com
Region: US (api.mailgun.net)
Timestamp (UTC): 2026-11-30 14:05
Response body: Forbidden

I tested with curl using a newly created API key and the username "api".
The domain is verified and the base URL matches my region.
Please check whether the key is valid for this domain and region.

Thanks

Common questions

Why does it work in curl but not in my app?

Curl sends form data with correct Basic auth, so your app is doing something different. Compare the Content-Type header, the Authorization header and the field names against the curl request.

Can I send JSON to the messages endpoint?

Not for sending a message; the endpoint expects form fields. Use FormData, URLSearchParams, or your SDK's send method.

Do I need a different key for the EU region?

Use the key and domain from the EU dashboard, together with the EU base URL. Mailgun's region guide notes that EU has its own API, SMTP, MX and tracking endpoints.

Is a 401 the same as a 403?

No. A 401 means authentication failed, as with a bad key, region or header. A 403 generally means the request was understood but not allowed, as with a sandbox domain sending to an unverified recipient.

designmailgunerror-codes
Share this article
Sohail Hussain

Sohail Hussain

Founder & CEO at Mailneo

Building Mailneo — AI-powered email marketing for growing businesses.

Related Articles

How-To

FLBuilderServiceConvertKit::subscribe too few arguments fix

The fatal error "Too few arguments to function FLBuilderServiceConvertKit::subscribe(), 3 passed and exactly 4 expected" means one piece of code calls a method with 3 arguments while its definition requires 4. After a Beaver Builder and PowerPack update, roll back to the previous versions, or make the 4th argument optional as a stopgap.

Sohail Hussain|8 min read
How-To

Gmail "This message could be dangerous" on beehiiv: fixes

Gmail's "This message could be dangerous" banner is a per-message content and reputation warning, not an SMTP error. Some beehiiv publishers blamed the promotional image in the footer; beehiiv staff say it isn't the root cause. Check the headers, links and images first, then move to a custom domain.

Sohail Hussain|9 min read
How-To

Outlook "click here to view it in a web browser" banner fix

Outlook shows "If there are problems with how this message is displayed, click here to view it in a web browser" when it blocks remote images. As a recipient, click the banner and choose Download Pictures, or add the sender to Safe Senders. As a sender, design the email to read well with images off.

Sohail Hussain|9 min read
How-To

Mailchimp Free plan can't send or import an HTML newsletter

Mailchimp's Free plan now shows "Importing custom HTML templates isn't currently included in our Free plan" when you import HTML. Staff say the fix is upgrading to Standard. Otherwise, rebuild the email in Mailchimp's builder, use Canva images, or move to a tool that accepts HTML.

Sohail Hussain|7 min read

Ready to supercharge your email marketing?

Start sending smarter emails with AI-powered campaigns. No credit card required.

Get Started Free