Check MX Records via API for Email Deliverability

Before sending an email to a new address, how do you know the recipient's domain can actually receive it? The answer lives in MX records -- the DNS entries that tell the world which mail servers handle incoming email for a domain. By querying MX records through the Apixies DNS Lookup API, you can verify email deliverability programmatically, reduce bounce rates, and protect your sender reputation.

Why MX Records Matter for Email Deliverability

Every email you send to an invalid or non-existent domain bounces. High bounce rates damage your sender reputation with services like Gmail, Outlook, and Yahoo, which can lead to your emails being flagged as spam or rejected entirely. Checking MX records before sending is a lightweight, effective way to catch bad domains early.

An MX (Mail Exchange) record specifies the mail server responsible for accepting email on behalf of a domain. If a domain has no MX records, it cannot receive email through standard SMTP delivery. By verifying that MX records exist -- and that they point to legitimate mail infrastructure -- you can filter out domains that will inevitably bounce.

This check does not replace full email verification (which involves SMTP handshakes and mailbox-level validation), but it is the fastest and cheapest first pass. It catches typos in domain names, entirely fake domains, and domains that have been decommissioned.

Querying MX Records with the API

The Apixies DNS Lookup API makes this straightforward. Pass the domain and set type=MX:

curl -H "X-API-Key: YOUR_API_KEY" \
  "https://apixies.io/api/v1/dns-lookup?domain=gmail.com&type=MX"

A domain with properly configured email will return one or more MX records:

{
  "records": [
    {
      "type": "MX",
      "value": "alt1.gmail-smtp-in.l.google.com",
      "priority": 10,
      "ttl": 3600
    },
    {
      "type": "MX",
      "value": "gmail-smtp-in.l.google.com",
      "priority": 5,
      "ttl": 3600
    }
  ]
}

The priority field determines the order in which mail servers are tried. Lower values mean higher priority -- the sending server contacts the lowest-priority server first and falls back to others if it is unavailable.

A domain with no MX records returns an empty records array, which is a strong signal that the domain cannot receive email.

Building an Email Domain Validator

Here is a practical implementation. Given an email address, extract the domain portion and check whether it has valid MX records.

JavaScript (Node.js)

async function canReceiveEmail(email) {
  const domain = email.split("@")[1];
  if (!domain) return false;

  const url = new URL("https://apixies.io/api/v1/dns-lookup");
  url.searchParams.set("domain", domain);
  url.searchParams.set("type", "MX");

  const response = await fetch(url, {
    headers: { "X-API-Key": process.env.APIXIES_KEY },
  });

  if (!response.ok) return false;

  const data = await response.json();
  return data.records && data.records.length > 0;
}

// Usage
const valid = await canReceiveEmail("user@gmail.com");
console.log(valid ? "Domain can receive email" : "No MX records found");

Python

import requests
import os

api_key = os.environ["APIXIES_KEY"]

def can_receive_email(email: str) -> bool:
    domain = email.split("@")[-1]
    if not domain:
        return False

    response = requests.get(
        "https://apixies.io/api/v1/dns-lookup",
        params={"domain": domain, "type": "MX"},
        headers={"X-API-Key": api_key},
    )

    if response.status_code != 200:
        return False

    records = response.json().get("records", [])
    return len(records) > 0

# Usage
if can_receive_email("contact@example.com"):
    print("Domain has MX records -- email delivery is possible")
else:
    print("No MX records -- domain cannot receive email")

PHP

$apiKey = getenv('APIXIES_KEY');

function canReceiveEmail(string $email): bool
{
    global $apiKey;

    $parts = explode('@', $email);
    $domain = $parts[1] ?? '';
    if (empty($domain)) return false;

    $query = http_build_query(['domain' => $domain, 'type' => 'MX']);
    $ch = curl_init("https://apixies.io/api/v1/dns-lookup?{$query}");
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-API-Key: {$apiKey}"]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $body = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($status !== 200) return false;

    $data = json_decode($body, true);
    return !empty($data['records']);
}

// Usage
if (canReceiveEmail('user@company.com')) {
    echo "MX records found -- domain can receive email\n";
} else {
    echo "No MX records -- do not send\n";
}

Going Deeper: Validating Email Security Records

Checking that MX records exist confirms the domain can receive email. But you can go further by also querying TXT records to verify that the domain has proper email authentication policies in place. This is useful if you are evaluating a domain's trustworthiness before exchanging sensitive information.

Check for SPF Records

SPF (Sender Policy Framework) records are stored as TXT records and specify which servers are authorized to send email on behalf of a domain:

curl -H "X-API-Key: YOUR_API_KEY" \
  "https://apixies.io/api/v1/dns-lookup?domain=gmail.com&type=TXT"

Look for a record whose value starts with v=spf1. If present, the domain has published an SPF policy -- a sign of legitimate email infrastructure.

Check for DMARC Records

DMARC records are published at the _dmarc subdomain. Query them by looking up the TXT records for _dmarc.example.com:

curl -H "X-API-Key: YOUR_API_KEY" \
  "https://apixies.io/api/v1/dns-lookup?domain=_dmarc.gmail.com&type=TXT"

A DMARC record starting with v=DMARC1 indicates the domain has a published DMARC policy, which helps prevent email spoofing.

When to Run MX Checks

Integrating MX validation into your workflow depends on where bad email addresses enter your system:

  • Registration forms -- Validate the email domain when a user signs up. If no MX records exist, prompt them to double-check the address before submitting.
  • Contact forms and lead capture -- Before adding a lead to your CRM, verify the domain. This keeps your mailing list clean from the start.
  • Bulk imports -- When importing a CSV of email addresses, run each domain through the MX check to flag problematic entries before your first campaign.
  • Transactional email pipelines -- Add an MX check before enqueuing messages. This prevents your email queue from filling with undeliverable messages.

With 75 requests/day on a free account, you can validate domains during development, test the integration thoroughly, and handle a reasonable volume of real-time checks in production. For bulk validation scenarios, paid plans provide higher limits.

Handling Edge Cases

Not every situation is black and white. Keep these scenarios in mind:

  • Domains with no MX but an A record -- According to RFC 5321, if no MX records exist, the sending server may fall back to the domain's A record. This is an uncommon configuration but technically valid. For maximum accuracy, you could query both MX and A records.
  • Wildcard MX records -- Some providers configure wildcard DNS, which means every subdomain returns records. The presence of MX records does not guarantee a valid mailbox -- only that the domain has mail infrastructure.
  • Temporary DNS failures -- If the API returns an error, it may be a transient issue with upstream DNS. Retry once before concluding that the domain has no records.

Next Steps

MX record validation is one of the simplest, highest-value checks you can add to any email-related workflow. It requires a single API call, runs in milliseconds, and prevents costly bounces.

Get your free API key and start validating email domains today.

Try the DNS Lookup API

Free tier is for development & small projects. 75 requests/day with a registered account.

Getting Started

Explore

Resources

Get Started Free

Free for development and small projects.

Get Free API Key