DEV Community

giveitatry
giveitatry

Posted on

How to Add a Subdomain with Hetzner DNS API

Hetzner’s DNS Public API allows you to programmatically manage your domains, including creating subdomains. A subdomain is simply a DNS record under your main zone.

1. Get Your Zone ID

Each domain in Hetzner DNS has a unique zone_id. To find it:

curl -X GET "https://dns.hetzner.com/api/v1/zones" \
  -H "Auth-API-Token: $API_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Look for your domain in the response and note its id. For example:

{
  "id": "ABC...",
  "name": "domain.com"
}
Enter fullscreen mode Exit fullscreen mode

2. Add the Subdomain Record

Once you have the zone_id, you can create a subdomain by adding a DNS record. For example, to create xxx.domain.com pointing to 1.2.3.4:

curl -X POST "https://dns.hetzner.com/api/v1/records" \
  -H "Auth-API-Token: $API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "zone_id": "ABC...",
    "type": "A",
    "name": "xxx",
    "value": "1.2.3.4",
    "ttl": 300
  }'
Enter fullscreen mode Exit fullscreen mode

Parameters:

  • zone_id → The zone where the subdomain will be added.
  • type → DNS record type (A, CNAME, MX, etc.).
  • name → Subdomain prefix (xxx).
  • value → IP address or target domain (1.2.3.4 here).
  • ttl → Time-to-live in seconds (optional, default 300).

3. Verify the Subdomain

To confirm the subdomain was created:

curl -X GET "https://dns.hetzner.com/api/v1/records?zone_id=ABC..." \
  -H "Auth-API-Token: $API_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Check for your new record in the response.


✅ Summary

  • Subdomains are just DNS records in Hetzner.
  • Use GET /zones to find your zone_id.
  • Use POST /records to add subdomains.
  • Use GET /records to list or verify existing subdomains.

This approach lets you automate DNS management for your domains using Hetzner’s DNS API.

Top comments (1)

Collapse
 
giveitatry profile image
giveitatry

Hetzner depreciated this method see: docs.hetzner.com/networking/dns/mi...