Domain Extractor API: The Complete Guide to Domain Parsing

Need to extract the root domain or subdomain from URLs? This guide covers everything you need to know about domain extraction via API, including handling complex TLDs and implementation examples.

What is Domain Extraction?

Domain extraction parses a hostname to identify its subdomain, root domain, and top-level domain (TLD). This is trickier than splitting on dots because TLDs can be multi-part (like .co.uk or .com.au).

For example, from blog.shop.example.co.uk, the subdomain is "blog.shop", the domain is "example", and the TLD is "co.uk".

Domain Components

A fully qualified domain has several parts:

Top-Level Domain (TLD)

The suffix: .com, .org, .co.uk, .com.au. The Public Suffix List defines valid TLDs.

Root/Registered Domain

The domain registered with a registrar: example.com, company.co.uk.

Subdomain

Everything before the root domain: www, blog, api.v2, etc.

Challenge: Simple string splitting fails because .co.uk is one TLD, not two. Our API uses the Public Suffix List for accuracy.

Using the Domain Extractor API

TinyFn provides a simple endpoint to extract domain components:

API Request
GET https://api.tinyfn.io/v1/parse/domain?url=https://blog.shop.example.co.uk/page
Headers: X-API-Key: your-api-key
Response
{
  "input": "https://blog.shop.example.co.uk/page",
  "hostname": "blog.shop.example.co.uk",
  "subdomain": "blog.shop",
  "domain": "example",
  "tld": "co.uk",
  "registeredDomain": "example.co.uk",
  "isIp": false,
  "isValid": true
}

Parameters

Parameter Type Description
url string Full URL or hostname to parse

Code Examples

JavaScript / Node.js

// Extract domain from URL
const response = await fetch(
  'https://api.tinyfn.io/v1/parse/domain?url=https://api.shop.example.com/v1/products',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const { subdomain, domain, tld, registeredDomain } = await response.json();
console.log(subdomain);        // api.shop
console.log(domain);           // example
console.log(tld);              // com
console.log(registeredDomain); // example.com

// Group URLs by registered domain
async function groupByDomain(urls) {
  const groups = {};
  for (const url of urls) {
    const response = await fetch(
      `https://api.tinyfn.io/v1/parse/domain?url=${encodeURIComponent(url)}`,
      { headers: { 'X-API-Key': 'your-api-key' } }
    );
    const { registeredDomain } = await response.json();
    groups[registeredDomain] = groups[registeredDomain] || [];
    groups[registeredDomain].push(url);
  }
  return groups;
}

Python

import requests

# Extract domain from UK URL
response = requests.get(
    'https://api.tinyfn.io/v1/parse/domain',
    params={'url': 'https://www.bbc.co.uk/news'},
    headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['subdomain'])        # www
print(data['domain'])           # bbc
print(data['tld'])              # co.uk
print(data['registeredDomain']) # bbc.co.uk

# Extract from Australian domain
response = requests.get(
    'https://api.tinyfn.io/v1/parse/domain',
    params={'url': 'https://shop.example.com.au'},
    headers={'X-API-Key': 'your-api-key'}
)
au_data = response.json()
print(au_data['tld'])  # com.au

cURL

# Extract domain from URL
curl "https://api.tinyfn.io/v1/parse/domain?url=https://blog.example.co.uk/page" \
  -H "X-API-Key: your-api-key"

# Handle Japanese domain
curl "https://api.tinyfn.io/v1/parse/domain?url=https://www.example.co.jp" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Cookie Scope: Set cookies on the correct domain level
  • Link Analysis: Group links by root domain
  • Security: Verify links point to expected domains
  • Analytics: Aggregate traffic by registered domain
  • Email Parsing: Extract domain from email addresses

Best Practices

  1. Use Public Suffix List: Don't try to parse TLDs with simple logic
  2. Handle IP addresses: IPs don't have domains; check isIp flag
  3. Validate results: Check isValid before using extracted components
  4. Consider internationalized domains: Handle punycode/IDN properly

Try the Domain Extractor API

Get your free API key and start extracting domains in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key