How to create Sending API for domain in Mailgun

Create Sending API key for a domain in Mailgun

Post Date: January 9, 2021
Last Updated: November 24, 2026

Introduction

This guide explains how to create Sending API keys for a domain in Mailgun. A Sending API key lets your application send email through one specific domain, without using your main account key. If the key leaks, an attacker can send mail only for that domain, and you can delete the key without touching anything else.

The original guide covered the dashboard clicks only. This version adds the difference between a Sending key and the account key, a domain check, a curl test to prove the key works, safe storage advice and troubleshooting.

Warning: When you create Sending API keys, Mailgun shows the key only once. Copy it immediately and store it in a password manager or secrets store. Never paste a key into public code, a Git repository or a support chat.

Prerequisites

Before you create Sending API keys, make sure you have:

  • A Mailgun account, with a user role that is allowed to manage domain settings
  • A domain added to Mailgun and verified (the DNS records are in place)
  • Your account region: US or EU, because the API address differs
  • A secure place to store the key, such as a password manager or a secrets manager
  • curl on your computer or server, for the test in Step 6

Architecture

  • A Mailgun account holds one or more sending domains
  • A Sending API key belongs to one domain and can only send mail for that domain. It cannot read account settings or manage other domains
  • Your application sends a request to the Mailgun API and authenticates with the user name api and the Sending key as the password
  • The Mailgun API checks that the key matches the domain in the URL, then queues the email for delivery
  • DNS records (SPF and DKIM) on the domain let receiving servers trust the message

The image below shows the workflow. For the official reference, see the Mailgun documentation.

Steps to Create Sending API Keys for a Domain in Mailgun

1. Log In to the Mailgun Portal

Sign in to your Mailgun account at the Mailgun website. If you have more than one account or region, check that you are in the right one before you continue.

2. Open Your Domain

In the left menu, open Domains under the Sending section. Select the domain for which you want a sending key. Check that its status shows as verified, because an unverified domain cannot send mail.

3. Open the Sending API Keys Tab

Click Domain settings, open the Sending API keys tab, and click the button to add a new sending key. Depending on your dashboard version, the button may read Add sending key.

4. Name the Key and Create It

This is the step where you create Sending API keys. Enter a description that tells you who uses the key, such as the application name (for example billing-app-production). Then click Create sending key.

Note: Use one key per application or environment. If one key leaks, you can delete it without breaking the others.

5. Copy and Store the Key

Mailgun now shows the key. Copy it and save it in your password manager or secrets store. If you lose it, you cannot view it again, so you must create a new key and delete the old one.

Keep the key out of your code. Use an environment variable instead:

export MAILGUN_API_KEY="your-sending-key-here"

6. Test the Key with curl

Send a test message to check that the key works. Replace example.com with your domain and the recipient with your own address:

curl -s --user "api:$MAILGUN_API_KEY" \
  https://api.mailgun.net/v3/example.com/messages \
  -F from="Test <postmaster@example.com>" \
  -F to="you@example.net" \
  -F subject="Mailgun test" \
  -F text="The Sending API key works."

A working key returns a response like this:

{"id": "<20260101000000.abc123@example.com>", "message": "Queued. Thank you."}

If your account is in the EU region, use https://api.eu.mailgun.net instead of https://api.mailgun.net. Then check the Logs page in the dashboard to confirm that the message was delivered.

7. Delete or Rotate a Key When Needed

Return to the Sending API keys tab to remove a key you no longer use. To rotate a key, create the new one first, update your application, confirm the test passes, then delete the old key.

Troubleshooting

If you cannot create Sending API keys or the key does not work, check these:

  • “401 Unauthorized” or “Forbidden”: The key is wrong, has a stray space, or belongs to a different domain. Also confirm the user name is exactly api
  • “Domain not found”: The domain name in the URL has a typo, or you used the US address for an EU account. Switch to api.eu.mailgun.net if your account is in the EU
  • A message that sandbox domains are for testing only: You are sending from the sandbox domain. Add and verify your own domain, or add the recipient as an authorized recipient in the sandbox settings
  • You cannot create Sending API keys because the tab or button is missing: Your user role may not allow it. Ask the account owner or an admin to create the key or change your role
  • You lost the key: Mailgun does not show it again. Create a new Sending API key, update your application, then delete the old one
  • The key works for one domain but not another: A Sending key is limited to one domain. Create a separate key for each domain
  • The API says “Queued” but the email never arrives: The domain’s SPF or DKIM records may be wrong. Check the domain’s verification status and the Logs page

Conclusion

To create Sending API keys in Mailgun, open your verified domain, go to Domain settings, add a key on the Sending API keys tab, and copy it at once. Store the key in a secrets manager, test it with curl, and use one key per application. A domain-scoped key limits the damage if it ever leaks, which makes it safer than the main account key.

Frequently Asked Questions (FAQs)

What is the difference between a Sending API key and the account API key?
A Sending API key works for one domain and can only send email. The account API key has wider access to the account, so use a Sending key in applications.

Who can create Sending API keys in Mailgun?
Users whose role allows domain management, usually account owners and admins. If you don’t see the option, ask an admin.

Can I see a Sending API key again after I create it?
No. Mailgun shows it only once. If you lose it, create a new key and delete the old one.

Can I use the same Sending API key for several domains?
No. Each key belongs to a single domain, so create one key per domain.

Should I use the API or SMTP?
Both work. Use the HTTP API for new applications, and SMTP credentials for software that only supports SMTP. SMTP credentials are a separate setting under the same domain.

Exim Mail Server – Commands

How to Send an Email Using Telnet from a Remote Server

Talk to Our Technology Experts

Having trouble with email delivery, DNS records, or mail integrations on your servers? Our team can configure your domain, SPF and DKIM records, and cloud mail setup through our DevOps and Cloud Services.

Connect with our technology experts

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top