Skip to main content
This guide will help you make your first DNS lookups using the library.

Installation

First, install the library:
npm install @layered/dns-records

Basic usage

The library provides two main functions:
  • getDnsRecords() - Get specific DNS record types for a hostname
  • getAllDnsRecords() - Discover all DNS records for a domain

Get specific DNS records

Use getDnsRecords() to retrieve records of a specific type:
1

Import the function

import { getDnsRecords } from '@layered/dns-records'
2

Query DNS records

Specify the hostname and record type:
const aRecords = await getDnsRecords('google.com', 'A')
console.log('DNS A records', aRecords)
3

Process the results

Each record returns an array of DnsRecord objects:
[
  {
    name: 'google.com.',
    ttl: 608,
    type: 'TXT',
    data: '"v=spf1 include:_spf.google.com ~all"'
  },
  // ... more records
]

Get all DNS records

Use getAllDnsRecords() to discover all DNS records for a domain, including subdomains:
1

Import the function

import { getAllDnsRecords } from '@layered/dns-records'
2

Discover all records

Call the function with a domain name:
const allRecords = await getAllDnsRecords('x.com')
console.log('All DNS records', allRecords)
3

Customize the discovery (optional)

Configure DNS resolver, subdomain checking, and extra subdomains:
const allRecords = await getAllDnsRecords('x.com', {
  resolver: 'cloudflare-dns',
  commonSubdomainsCheck: true,
  subdomains: ['extra-subdomain-to-check-for'],
})

Complete example

Here’s a complete example that demonstrates both functions:
import { getDnsRecords, getAllDnsRecords } from '@layered/dns-records'

// Get specific record types
const txtRecords = await getDnsRecords('google.com', 'TXT')
console.log(`Found ${txtRecords.length} TXT records`)

// Get MX records with specific resolver
const mxRecords = await getDnsRecords('android.com', 'MX', 'google-dns')
console.log(`Found ${mxRecords.length} MX records`)

// Discover all DNS records
const allRecords = await getAllDnsRecords('example.com')
console.log(`Discovered ${allRecords.length} total DNS records`)

// Advanced: Get all records with custom options
const customRecords = await getAllDnsRecords('shopify.com', {
  resolver: 'cloudflare-dns',
  commonSubdomainsCheck: true,
  subdomains: ['api', 'cdn', 'status'],
})

Expected output

When you query all DNS records, you’ll receive an array with various record types:
[
  { name: 'x.com.', ttl: 3600, type: 'NS', data: 'ns71.domaincontrol.com.' },
  { name: 'x.com.', ttl: 3600, type: 'NS', data: 'ns72.domaincontrol.com.' },
  { name: 'x.com.', ttl: 600, type: 'SOA', data: 'ns71.domaincontrol.com. dns.jomax.net. 2018071100 28800 7200 604800 600' },
  { name: 'x.com.', ttl: 600, type: 'A', data: '160.153.63.10' },
  { name: 'www.x.com.', ttl: 3600, type: 'CNAME', data: 'x.com.' },
  { name: 'x.com.', ttl: 3600, type: 'MX', data: '10 mx-van.mail.am0.yahoodns.net.' }
]

API reference

getDnsRecords()

getDnsRecords(
  name: string,
  type?: DnsRecordType,
  resolver?: string
): Promise<DnsRecord[]>
Parameters:
  • name (string) - Fully qualified domain name (e.g., 'google.com' or 'mail.apple.com')
  • type (string, optional) - Record type: 'A', 'AAAA', 'TXT', 'MX', 'CNAME', 'NS', 'SOA', etc. Defaults to 'A'
  • resolver (string, optional) - DNS resolver to use: 'cloudflare-dns', 'google-dns', 'node-dns', 'node-dig', or 'deno-dns'. Auto-selected if not specified

getAllDnsRecords()

getAllDnsRecords(
  domain: string,
  options?: GetAllDnsRecordsOptions
): Promise<DnsRecord[]>
Parameters:
  • domain (string) - Valid domain name (e.g., 'google.com')
  • options (object, optional) - Configuration options:
    • resolver (string) - Which DNS resolver to use
    • commonSubdomainsCheck (boolean) - Check for common subdomains from built-in list. Default: true
    • subdomains (string[]) - Array of extra subdomains to check for

DnsRecord type

interface DnsRecord {
  name: string      // Fully qualified domain name
  type: string      // Record type: A, AAAA, CNAME, MX, TXT, etc.
  ttl: number       // Time to live (in seconds)
  data: string      // Record data: IP for A/AAAA, fqdn for CNAME, etc.
}

Next steps

API Reference

Explore the complete API documentation

Examples

See more usage examples and patterns

Build docs developers (and LLMs) love