Dansk

DNS API

Change the DNS of your domain from your own scripts - e.g. for domain verification with other services, a change of IP address or automatic setup.

1. What can the API do?

With the DNS API a program can add, change and delete these records on your domain:

Name servers, DKIM, the standard mail SRV records and free text cannot be changed through the API - they are changed in the DNS manager in the control panel as before.

Changes are saved exactly as when you save in the DNS manager and are sent to the name servers automatically. It usually takes a few minutes before a change can be seen on the internet.

2. Switch the API on and get a key

  1. Log in to the control panel for the domain and press DNS information.
  2. Find the line DNS API (below Toggle admin lock) and press Off.
  3. Press Switch the DNS API on. You now get a key, which you can copy with Copy.

In the same window you can:

The DNS API line is only shown when the domain uses our DNS manager (our name servers) and the DNS API has been opened for the domain. Contact support if you would like it opened.

3. Security

4. Address, key and format

All addresses start with:

https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1

The key is sent in the header (either of them):

Authorization: Bearer YOUR-KEY
X-Api-Key: YOUR-KEY

Data is sent and received as JSON. The answer always has "ok": true or "ok": false; on errors also "error" (a code) and "message" (an explanation). The explanations are in English; send the header Accept-Language: da to get them in Danish.

Names

The name of a record can be written in several ways - they all mean the same:

You writeMeans (for example.dk)
@ or emptyexample.dk (the domain itself)
wwwwww.example.dk
www.example.dk or www.example.dk.www.example.dk
_verify.shop_verify.shop.example.dk

Overview

MethodAddressWhat
GET/recordsAll records
POST/recordsAdd or change one record
DELETE/recordsDelete one record
POST/spfAdd or remove a part of SPF
POST/batchUp to 20 changes - all or none
POST/undoUndo the latest API change
GET/changesThe latest 20 API changes
GET/statusSerial number, and whether the change has been sent to the name server
GET/check?name=…&type=…What the name server answers right now

5. List records

curl -H "Authorization: Bearer YOUR-KEY" \
     https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/records

Answer (shortened):

{
  "ok": true,
  "domain": "example.dk",
  "version": "5dfb3e05d2e3c3c9d38298565fcefc41",
  "records": [
    {"name": "@",   "fqdn": "example.dk",     "type": "A",  "value": "192.0.2.10"},
    {"name": "www", "fqdn": "www.example.dk", "type": "A",  "value": "192.0.2.10"},
    {"name": "@",   "fqdn": "example.dk",     "type": "MX", "value": "mail1.webhosting.dk.", "priority": 10},
    {"name": "@",   "fqdn": "example.dk",     "type": "TXT","value": "v=spf1 a mx ~all"}
  ],
  "limits": {"txt_used": 1, "txt_max": 6, "mx_used": 1, "mx_max": 10},
  "not_in_api": {"dkim": 1, "srv": 0, "free_text": 0}
}

version changes every time the DNS is changed - see section 11. not_in_api shows how many records exist that can only be changed in the DNS manager.

6. Add or change a record

curl -X POST -H "Authorization: Bearer YOUR-KEY" \
     -d '{"type":"A","name":"test","value":"192.0.2.20"}' \
     https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/records
FieldDescription
typeA, AAAA, CNAME, MX, TXT or SRV
nameThe name, see section 4. MX only on the domain itself (@). SRV: _service._protocol, e.g. _sip._tls.
valueIP address, host name or text. SRV: "priority weight port target", e.g. "100 1 443 sipdir.online.lync.com"
priorityMX, 0-32767 (default 10) - or SRV, if value is only "weight port target"

If an A, AAAA or CNAME already exists on the name, it is changed. TXT and MX are added next to the existing ones. An SRV with the same name and target gets its numbers changed; otherwise it is added.

If exactly the same record already exists, the API answers "ok": true, "changed": false - so it is safe to send the same change again.

The answer shows what was changed, and any warnings:

{
  "ok": true, "changed": true, "change_id": 12,
  "changes": [{"op": "add", "record": "test.example.dk A 192.0.2.20"}],
  "warnings": [],
  "version": "81178d68cf6a8c10ba113bf0f85972d7"
}

More examples

# IPv6 on www (www must already have an A record)
{"type":"AAAA","name":"www","value":"2001:db8::10"}

# CNAME
{"type":"CNAME","name":"shop","value":"shops.myshopify.com"}

# MX with priority
{"type":"MX","name":"@","value":"mx2.example.net","priority":20}

# TXT - domain verification
{"type":"TXT","name":"@","value":"google-site-verification=abc123"}

# DMARC (replaces an existing DMARC record)
{"type":"TXT","name":"_dmarc","value":"v=DMARC1; p=quarantine; rua=mailto:dmarc@example.dk"}

# SRV - Microsoft 365 / Teams
{"type":"SRV","name":"_sip._tls","value":"100 1 443 sipdir.online.lync.com"}

# SRV - Minecraft on port 25565
{"type":"SRV","name":"_minecraft._tcp","value":"0 5 25565 mc.example.dk"}

7. Delete a record

curl -X DELETE -H "Authorization: Bearer YOUR-KEY" \
     -d '{"type":"TXT","name":"_verify","value":"abc123"}' \
     https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/records

8. SPF

A domain may only have one SPF record. So you add and remove parts of it instead of creating a new one:

# add Google Workspace
curl -X POST -H "Authorization: Bearer YOUR-KEY" \
     -d '{"action":"add","value":"include:_spf.google.com"}' \
     https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/spf

# remove it again
-d '{"action":"remove","value":"include:_spf.google.com"}'

You can also give an IP address (192.0.2.10 becomes ip4:192.0.2.10) or a domain (mailgun.org becomes include:mailgun.org). If the domain has no SPF, one is created with v=spf1 a mx … ~all.

The SPF record may make at most 10 DNS lookups and be at most 255 characters - otherwise the change is refused.

9. Several changes at once

Up to 20 changes in one call. Either all are saved, or none - if one fails, nothing is changed, and the answer tells you which one ("index", counted from 0).

curl -X POST -H "Authorization: Bearer YOUR-KEY" \
     -d '{"changes":[
           {"op":"add",    "type":"A",   "name":"test", "value":"192.0.2.20"},
           {"op":"delete", "type":"TXT", "name":"@",    "value":"old-verification"},
           {"op":"spf_add","value":"include:_spf.google.com"}
         ]}' \
     https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/batch

op can be add, delete, spf_add and spf_remove.

10. Try first (dry_run)

Add ?dry_run=1 to the address (or "dry_run": true in the JSON), and the API shows what would be changed, without saving anything:

curl -X POST -H "Authorization: Bearer YOUR-KEY" \
     -d '{"type":"CNAME","name":"www","value":"example.netlify.app"}' \
     "https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/records?dry_run=1"

The answer has "dry_run": true and the list of changes - e.g. that the A record on www would be deleted and replaced by the CNAME.

11. Avoid overwriting other changes

Fetch the records, and send the version you got with your change. If the DNS has been changed in the meantime (in the control panel or by another script), the change is refused with version_conflict (HTTP 409), and you can fetch the records again.

curl -X POST -H "Authorization: Bearer YOUR-KEY" \
     -H 'If-Match: 5dfb3e05d2e3c3c9d38298565fcefc41' \
     -d '{"type":"A","name":"@","value":"192.0.2.30"}' \
     https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/records

Instead of the header you can send "if_version": "…" in the JSON.

Note: If someone has the DNS manager open in the control panel while the API changes something, the DNS manager will save the version that was opened when it is saved - and so overwrite the API change. Close the DNS manager before you run scripts.

12. Undo a change

curl -X POST -H "Authorization: Bearer YOUR-KEY" \
     https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/undo

Undoes the latest change made through the API. Call again to go one more change back. To be sure you undo exactly your own change, send its number: {"change_id": 12} (change_id from the answer to the change) - if a newer change has been made since, it is refused.

It only works if the DNS has not been changed in another way since - otherwise the answer is changed_since. Use ?dry_run=1 to see what would be undone.

GET /changes shows the latest 20 changes with time and IP address.

13. Status and checks

GET /status

{"ok": true, "domain": "example.dk", "serial": "2026112305",
 "pending": true, "zone_ok": true, "primary_ns": "ns1pri.webhosting.dk", "version": "…"}

pending: true means the change is waiting to be picked up by the name server.

GET /check

Asks our primary name server what it answers right now - handy for waiting until a record is active:

curl -H "Authorization: Bearer YOUR-KEY" \
     "https://www.webhosting.dk/cgi-bin/dnsapi.pl/v1/check?name=_verify&type=TXT"

{"ok": true, "name": "_verify.example.dk", "type": "TXT",
 "server": "ns1pri.webhosting.dk", "answers": ["abc123"]}

14. Rules and limits

15. Error codes

HTTPerrorMeans
400rejectedThe change is not allowed - see message
400bad_jsonThe content is not valid JSON
400too_manyMore than 20 changes in one batch
401no_keyThe key is missing in the header
401bad_keyWrong key, or the API is switched off
403ip_not_allowedThe call comes from an IP address that is not allowed
403adminlockAdmin lock is switched on for the domain
403inactiveThe domain is not active
403https_onlyUse https
404not_foundThe record to be deleted does not exist
404no_dnsThe domain does not use our DNS manager
404nothing_to_undoNo API change to undo
409version_conflictThe DNS has changed since the version you sent
409ambiguousSeveral records match - give value
409changed_sinceCannot be undone, the DNS has changed since
409not_newestThe change you gave with change_id is not the newest - undo the newer ones first
409busyAnother change is in progress - try again
429rate_limitToo many calls - wait a minute