Skip to main content

Introduction

This library provides an API as well as a CLI tool to manage DNS Zones and Records hosted at Hetzner.

This library includes both a library and a small CLI application.

I have created this module as I couldn't find an existing one, and I needed one for my ansible playbooks.

Installation

The library is available on PyPi, and can be installed with pip install hetzner-dns-api

CLI usage

$ hetzner-dns --help
Usage: hetzner-dns [OPTIONS] COMMAND [ARGS]...

  Hetzner DNS API CLI client.

  Manage your hetzner DNS zones and entries.

  You must specify the API key either with --api-key or using the environment
  variable HETZNER_API_KEY.

Options:
  --api-key TEXT
  --help          Show this message and exit.

Commands:
  record
  zone

Zones

List or export zones.

Zone import is not implemented yet.

$ hetzner-dns zone list --help

Usage: hetzner-dns zone list [OPTIONS]

  List DNS zones.

  You may narrow the zones returned with the options.

Options:
  --name TEXT
  --search
  --plain      Plain table without fancy text formatting
  --help       Show this message and exit.

The export subcommand takes either a zone-id or the domain name.

$ hetzner-api zone export --help
Usage: hetzner-dns zone export [OPTIONS] ZONE_ID_OR_NAME OUTPUT

  Export a zone.

Options:
  --help  Show this message and exit.

Output can be a filename or - for stdout.

Records

List, create, modify, or delete records

Usage: hetzner-dns record [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  create  Create a record in the given zone.
  delete  Delete a record.
  list    List records in a zone.
  update  Update a record.

Create

Usage: hetzner-dns record create [OPTIONS] ZONE_ID_OR_NAME NAME {A|AAAA|NS|MX|
                                 CNAME|RP|TXT|SOA|HINFO|SRV|DANE|TLSA|DS|CAA}
                                 VALUE

  Create a record in the given zone.

Options:
  --ttl INTEGER
  --help         Show this message and exit.


Example:

hetzner-dns record create example.com www A 192.0.2.1

If TTL is not specified, it will default to the default value for the Zone.

List

Usage: hetzner-dns record list [OPTIONS] ZONE_ID_OR_NAME

  List records in a zone.

Options:
  --plain  Plain table without fancy text formatting
  --help   Show this message and exit.

This will return a table view of all your records in a given zone. The zone may be given as either the ID or the domain itself.

Update

Usage: hetzner-dns record update [OPTIONS] RECORD_ID

  Update a record.

Options:
  --name TEXT
  --type [A|AAAA|NS|MX|CNAME|RP|TXT|SOA|HINFO|SRV|DANE|TLSA|DS|CAA]
  --value TEXT
  --ttl INTEGER
  --help                          Show this message and exit.

You may specify any number of fields to update by providing the corresponding option, however at least one option must be specified.

The record ID can be retrieved with the list command.

Example:

hetzner-dns record update example.com MYRECORDID --ttl 600

Delete

Usage: hetzner-dns record delete [OPTIONS] RECORD_ID

  Delete a record.

Options:
  --help  Show this message and exit.

Usage as a python library

For an example of how to use the API, check out the CLI module hetzner_dns_api.cli.

Initialize the API

from hetzner_dns_api import HetznerDNS

api = HetznerDNS("my-api-key")

Zones

The HetznerDNS.zones.list method returns an iterator that will iterate through all zones. This automatically takes in to account any paging done by the API server.

The iterator yields a hetzner_dns_api.types.DnsZoneResponse object. This object is a msgspec.Struct object, containing all the properties provided by Hetzner's API.

from hetzner_dns_api import HetznerDNS

api = HetznerDNS("my-api-key")

for zone in api.zones.all():
    # do something with the zone object

See hetzner_dns_api.DnsZone for more info on what you can do with Zones.

Records

Records is implemented in the hetzner_dns_api.types.DnsRecord class, which is conveniently available as .records in the HetznerDNS class instance.

from hetzner_dns_api import HetznerDNS

api = HetznerDNS("my-api-key")

# Get the zone_id of a domain
zone_id = api.zones.get_id("example.com")

# Create www.example.com A-record with 192.0.2.1 as value, and the zone default ttl
new_record = api.records.create(zone_id, name="www", record_type="A", value="192.0.2.1")

# Print the new record ID
print(new_record.id)

# Update the TTL of the record (or any other field)
updated_record = api.records.update(new_record.id, zone_id=zone_id, ttl=600)

# delete the newly created record
api.records.delete(new_record.id)

There's also methods for bulk creation, implemented as context managers:

api = HetznerDNS("my-api-key")

with api.records.bulk_create() as bulk:
    for num in range(200):
        bulk.add(zone_id="my-zone-id", name=f"host{num}", record_type="AAAA", value=f"2001:db8:f00::{num}")
    changeset = bulk.submit()
    
# Get all my record-ids
records = [record.id for record in api.records.all(zone_id="my-zone-id")]

with api.records.bulk_update() as bulk:
    for record_id in records:
        bulk.add(record_id, "my-zone-id", ttl=600)
    changeset = bulk.submit()

For bulk-creation, the .submit() function returns a hetzner_dns_api.types.DnsBulkRecordCreateResponse while the update one, returns a hetzner_dns_api.types.DnsBulkRecordUpdateResponse.

Metadata

Release files for hetzner-dns-api 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hetzner-dns-api 1.0.3
File Size Uploaded Provenance Actions
hetzner_dns_api-1.0.3.tar.gz 10.6 kB Without provenance Details for hetzner_dns_api-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hetzner-dns-api 1.0.3
File Interpreter ABI Platform Actions
hetzner_dns_api-1.0.3-py3-none-any.whl Python 3 none any Details for hetzner_dns_api-1.0.3-py3-none-any.whl

Total release size: 24.9 kB

Release files / hetzner_dns_api-1.0.3.tar.gz

Download URL hetzner_dns_api-1.0.3.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6bdff148e2fbad8645f5998999e2771c990ba40ad10d568ea103183b6ef6823c
BLAKE2b-256 checksum
How to use checksums
dcd646c0b1b862a6782be29e965483144f528b485d43bff52c0d5b116dd0eaf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.8.23

Release files / hetzner_dns_api-1.0.3-py3-none-any.whl

Download URL hetzner_dns_api-1.0.3-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
487c5a028ba9ca9404e04b5145381f2efdbbf88810b5894fceb91ff50cb8187d
BLAKE2b-256 checksum
How to use checksums
6bd3cdadfaff115799f64437b17c1d54e9cbd684317c217f6844f076d3aa7eb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.8.23

Security for hetzner-dns-api 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page