Hetzner Cloud API: Update DNS record with curl

Before migrating my DNS zones from the deprecated dns.hetzner.com console to the Hetzner Cloud Console, I updated one dynamic (DynDNS) record with curl like this:

curl -X "PUT" "https://dns.hetzner.com/api/v1/records/12345" \
  -H 'Content-Type: application/json' \
  -H 'Auth-API-Token: 12345' \
  -d '{"value": "'$IP'", "ttl": 60, "type": "A", "name": "dns", "zone_id": "12345"}'

Once a DNS zone is migrated, this API doesn't work anymore and the new Hetzner Cloud API has to be used.

The easiest way I found to achieve the same using the new API is:

ZONE_ID="example.com"
RECORD="dns"
TYPE="A"
API_KEY="12345"
IP="1.2.3.4"

curl "https://api.hetzner.cloud/v1/zones/$ZONE_ID/rrsets/$RECORD/$TYPE" \
  --request DELETE \
  --header "Authorization: Bearer $API_KEY"

curl "https://api.hetzner.cloud/v1/zones/$ZONE_ID/rrsets" \
  --request POST \
  --header 'Content-Type: application/json' \
  --header "Authorization: Bearer $API_KEY" \
  --data '{
        "name": "'"$RECORD"'",
        "type": "'"$TYPE"'",
        "ttl": 60,
        "records": [
            {
            "value": "'"$IP"'",
            "comment": ""
            }
        ],
        "labels": {}
    }'

This will delete and recreate the A record dns.example.com with IP address 1.2.3.4.

Correct me if I'm wrong: At this point in time it doesn't seem to be possible to just update a single record.

Since lego for Let's Encrypt is already supporting the new API, everything else was easy to achieve.

Schreibe einen Kommentar