DEV Community

Ozor
Ozor

Posted on

How to Build a DNS Lookup Tool with JavaScript (Free API, No dig Required)

Need to resolve a domain name from your app? You could shell out to dig or nslookup, but there's a much cleaner way.

The Problem

Most DNS resolution options for web apps are either:

  • Server-side only (Node's dns.resolve won't work in the browser)
  • Require installing system tools
  • Limited to A records

What if you need MX records, TXT records, or CNAME lookups from a simple HTTP call?

The Solution: One API Call

curl https://agent-gateway-kappa.vercel.app/v1/agent-dns/api/resolve/google.com
Enter fullscreen mode Exit fullscreen mode

Response:

{
  "domain": "google.com",
  "records": {
    "A": ["142.250.80.46"],
    "AAAA": ["2607:f8b0:4004:800::200e"],
    "MX": [
      { "priority": 10, "exchange": "smtp.google.com" }
    ],
    "NS": ["ns1.google.com", "ns2.google.com", "ns3.google.com", "ns4.google.com"],
    "TXT": ["v=spf1 include:_spf.google.com ~all"]
  }
}
Enter fullscreen mode Exit fullscreen mode

All record types in a single call. No API key needed for testing.

JavaScript Implementation

Browser (Fetch)

async function dnsLookup(domain) {
  const res = await fetch(
    `https://agent-gateway-kappa.vercel.app/v1/agent-dns/api/resolve/${domain}`
  );
  const data = await res.json();
  return data.records;
}

// Usage
const records = await dnsLookup('github.com');
console.log('A records:', records.A);
console.log('MX records:', records.MX);
Enter fullscreen mode Exit fullscreen mode

Node.js

const dns = await fetch(
  'https://agent-gateway-kappa.vercel.app/v1/agent-dns/api/resolve/stripe.com'
).then(r => r.json());

// Check if a domain has email configured
if (dns.records.MX?.length > 0) {
  console.log(`${dns.domain} accepts email via ${dns.records.MX[0].exchange}`);
}
Enter fullscreen mode Exit fullscreen mode

Real Use Cases

1. Email Validation (Check MX Records)

Before sending an email, verify the domain actually accepts mail:

async function canReceiveEmail(email) {
  const domain = email.split('@')[1];
  const dns = await fetch(
    `https://agent-gateway-kappa.vercel.app/v1/agent-dns/api/resolve/${domain}`
  ).then(r => r.json());

  return dns.records.MX?.length > 0;
}

console.log(await canReceiveEmail('user@gmail.com'));    // true
console.log(await canReceiveEmail('user@fake-domain.xyz')); // false
Enter fullscreen mode Exit fullscreen mode

2. SPF Record Checker

Verify a domain's email authentication setup:

async function checkSPF(domain) {
  const dns = await fetch(
    `https://agent-gateway-kappa.vercel.app/v1/agent-dns/api/resolve/${domain}`
  ).then(r => r.json());

  const spf = dns.records.TXT?.find(r => r.startsWith('v=spf1'));
  return spf ? { valid: true, record: spf } : { valid: false };
}
Enter fullscreen mode Exit fullscreen mode

3. Domain Health Monitor

const domains = ['myapp.com', 'api.myapp.com', 'cdn.myapp.com'];

for (const domain of domains) {
  const dns = await fetch(
    `https://agent-gateway-kappa.vercel.app/v1/agent-dns/api/resolve/${domain}`
  ).then(r => r.json());

  const hasA = dns.records.A?.length > 0;
  const hasAAAA = dns.records.AAAA?.length > 0;

  console.log(`${domain}: IPv4=${hasA}, IPv6=${hasAAAA}`);
}
Enter fullscreen mode Exit fullscreen mode

Get an API Key (Free)

The examples above work without a key, but for production use you'll want one:

curl -X POST https://agent-gateway-kappa.vercel.app/api/keys/create \
  -H "Content-Type: application/json" \
  -d '{"name": "my-app"}'
Enter fullscreen mode Exit fullscreen mode

You get 200 free credits. Each DNS lookup costs 1 credit.

Then pass your key:

curl -H "X-Api-Key: YOUR_KEY" \
  https://agent-gateway-kappa.vercel.app/v1/agent-dns/api/resolve/example.com
Enter fullscreen mode Exit fullscreen mode

Why Not Just Use dns.resolve in Node?

You can! But this API adds:

  • All record types in one call (A, AAAA, MX, NS, TXT, CNAME, SOA)
  • Works from browser JavaScript (no server needed)
  • Consistent JSON format across all record types
  • No system dependencies (dig, nslookup not required)

Full docs: frostbyte-api.vercel.app

Top comments (0)