← Back to docs

dns-api-guide

Language: SV | EN | SV

📘 DNS API Användarguide

Översikt

DNS API:et låter dig hantera DNS-poster programmatiskt via HTTP-anrop. Du kan använda det från scripts, automatiseringsverktyg eller egna applikationer.

Kom igång

Skaffa API-nyckel

Kontakta din administratör för att få en API-nyckel. Nyckeln är personlig och ska hållas hemlig.

Grundläggande användning

Alla API-anrop:

Base URL: https://tools.tornevall.net/api

Autentisering

Bearer Token (rekommenderat)

curl https://tools.tornevall.net/api/dns/zones \
  -H "Authorization: Bearer DIN_API_NYCKEL"

X-API-Key Header

curl https://tools.tornevall.net/api/dns/zones \
  -H "X-API-Key: DIN_API_NYCKEL"

Query Parameter (ej rekommenderat)

curl https://tools.tornevall.net/api/dns/zones?api_key=DIN_API_NYCKEL

Hämta zoner

Lista alla dina zoner

GET /api/dns/zones

Exempel:

curl https://tools.tornevall.net/api/dns/zones \
  -H "Authorization: Bearer DIN_API_NYCKEL"

Svar:

{
  "ok": true,
  "count": 5,
  "zones": [
    {
      "zone": "example.com",
      "file": "example.com/example.com",
      "key": "example.com"
    },
    {
      "zone": "test.se",
      "file": "test.se/test.se",
      "key": "test.se"
    }
  ],
  "is_admin": false,
  "access_type": "authenticated"
}

Hämta specifik zon

GET /api/dns/zones/{zone}

Exempel:

curl https://tools.tornevall.net/api/dns/zones/example.com \
  -H "Authorization: Bearer DIN_API_NYCKEL"

Svar:

{
  "ok": true,
  "zone": "example.com",
  "method": "AXFR",
  "record_count": 12,
  "zoneData": [
    {
      "name": "example.com",
      "type": "A",
      "rdata": "192.0.2.1",
      "ttl": 3600,
      "class": "IN"
    },
    {
      "name": "www.example.com",
      "type": "A",
      "rdata": "192.0.2.1",
      "ttl": 3600,
      "class": "IN"
    }
  ]
}

Hantera DNS-poster

Lägg till post

POST /api/dns/records/add
Content-Type: application/json

{
  "domain": "test.example.com",
  "type": "A",
  "target": "192.0.2.1",
  "ttl": 3600
}

Exempel:

curl -X POST https://tools.tornevall.net/api/dns/records/add \
  -H "Authorization: Bearer DIN_API_NYCKEL" \
  -H "Content-Type: application/json" \
  -d '{
    "domain": "test.example.com",
    "type": "A",
    "target": "192.0.2.1",
    "ttl": 3600
  }'

Svar (framgång):

{
  "ok": true,
  "message": "✓ Update successful (via DnsDirect)",
  "domain": "test.example.com",
  "type": "A",
  "target": "192.0.2.1",
  "ttl": 3600
}

Svar (fel):

{
  "ok": false,
  "reason": "validation_failed",
  "errors": {
    "target": ["The target field is required."]
  }
}

Ta bort post

POST /api/dns/records/delete
Content-Type: application/json

{
  "domain": "test.example.com",
  "type": "A"
}

Exempel:

curl -X POST https://tools.tornevall.net/api/dns/records/delete \
  -H "Authorization: Bearer DIN_API_NYCKEL" \
  -H "Content-Type: application/json" \
  -d '{
    "domain": "test.example.com",
    "type": "A"
  }'

Svar:

{
  "ok": true,
  "message": "✓ Update successful (via DnsDirect)",
  "domain": "test.example.com",
  "type": "A"
}

Uppdatera post

POST /api/dns/records/update
Content-Type: application/json

{
  "domain": "test.example.com",
  "type": "A",
  "old_target": "192.0.2.1",
  "new_target": "192.0.2.100",
  "ttl": 3600
}

Exempel:

curl -X POST https://tools.tornevall.net/api/dns/records/update \
  -H "Authorization: Bearer DIN_API_NYCKEL" \
  -H "Content-Type: application/json" \
  -d '{
    "domain": "test.example.com",
    "type": "A",
    "old_target": "192.0.2.1",
    "new_target": "192.0.2.100",
    "ttl": 3600
  }'

Bulk-operationer

Lägg till eller ta bort flera poster samtidigt:

POST /api/dns/records/bulk
Content-Type: application/json

{
  "operations": [
    {
      "action": "ADD",
      "domain": "test1.example.com",
      "type": "A",
      "target": "192.0.2.1",
      "ttl": 300
    },
    {
      "action": "DELETE",
      "domain": "test2.example.com",
      "type": "A"
    }
  ]
}

Exempel:

curl -X POST https://tools.tornevall.net/api/dns/records/bulk \
  -H "Authorization: Bearer DIN_API_NYCKEL" \
  -H "Content-Type: application/json" \
  -d '{
    "operations": [
      {"action":"ADD","domain":"test1.example.com","type":"A","target":"192.0.2.1","ttl":300},
      {"action":"ADD","domain":"test2.example.com","type":"A","target":"192.0.2.2","ttl":300}
    ]
  }'

Record Types

A Record (IPv4)

{
  "domain": "www.example.com",
  "type": "A",
  "target": "192.0.2.1",
  "ttl": 3600
}

AAAA Record (IPv6)

{
  "domain": "www.example.com",
  "type": "AAAA",
  "target": "2001:db8::1",
  "ttl": 3600
}

CNAME Record

{
  "domain": "blog.example.com",
  "type": "CNAME",
  "target": "example.com.",
  "ttl": 3600
}

OBS: Target måste sluta med punkt!

MX Record

{
  "domain": "example.com",
  "type": "MX",
  "target": "10 mail.example.com.",
  "ttl": 3600
}

TXT Record

{
  "domain": "example.com",
  "type": "TXT",
  "target": "v=spf1 include:_spf.google.com ~all",
  "ttl": 3600
}

NS Record

{
  "domain": "subdomain.example.com",
  "type": "NS",
  "target": "ns1.provider.com.",
  "ttl": 3600
}

Användningsexempel

Bash Script: Uppdatera IP vid dynamisk IP

#!/bin/bash

API_KEY="din-api-nyckel"
DOMAIN="home.example.com"
CURRENT_IP=$(curl -s ifconfig.me)

# Uppdatera A-record
curl -X POST https://tools.tornevall.net/api/dns/records/add \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d "{
    \"domain\": \"$DOMAIN\",
    \"type\": \"A\",
    \"target\": \"$CURRENT_IP\",
    \"ttl\": 300
  }"

echo "Updated $DOMAIN to $CURRENT_IP"

Python: Automatisk DNS-hantering

import requests

API_KEY = "din-api-nyckel"
BASE_URL = "https://tools.tornevall.net/api"

headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json"
}

# Lägg till post
def add_record(domain, record_type, target, ttl=3600):
    data = {
        "domain": domain,
        "type": record_type,
        "target": target,
        "ttl": ttl
    }
    response = requests.post(
        f"{BASE_URL}/dns/records/add",
        headers=headers,
        json=data
    )
    return response.json()

# Ta bort post
def delete_record(domain, record_type):
    data = {
        "domain": domain,
        "type": record_type
    }
    response = requests.post(
        f"{BASE_URL}/dns/records/delete",
        headers=headers,
        json=data
    )
    return response.json()

# Användning
result = add_record("test.example.com", "A", "192.0.2.1")
print(result)

Node.js/JavaScript

const API_KEY = 'din-api-nyckel';
const BASE_URL = 'https://tools.tornevall.net/api';

async function addDnsRecord(domain, type, target, ttl = 3600) {
    const response = await fetch(`${BASE_URL}/dns/records/add`, {
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${API_KEY}`,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ domain, type, target, ttl })
    });
    
    return await response.json();
}

// Användning
addDnsRecord('test.example.com', 'A', '192.0.2.1')
    .then(result => console.log(result))
    .catch(error => console.error(error));

PHP

<?php

$apiKey = 'din-api-nyckel';
$baseUrl = 'https://tools.tornevall.net/api';

function addDnsRecord($domain, $type, $target, $ttl = 3600) {
    global $apiKey, $baseUrl;
    
    $data = [
        'domain' => $domain,
        'type' => $type,
        'target' => $target,
        'ttl' => $ttl
    ];
    
    $ch = curl_init("$baseUrl/dns/records/add");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $apiKey",
        'Content-Type: application/json'
    ]);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return json_decode($response, true);
}

// Användning
$result = addDnsRecord('test.example.com', 'A', '192.0.2.1');
var_dump($result);

Felhantering

HTTP Status Codes

Felmeddelanden

{
  "ok": false,
  "reason": "permission_denied",
  "message": "No permission for zone: example.com"
}

Vanliga fel:

Rate Limiting

Säkerhet

Best Practices

Gör:

Gör INTE:

Återkalla nyckel

Om API-nyckel har läckt:

  1. Kontakta administratör omedelbart
  2. Få nyckeln återkallad
  3. Skaffa ny nyckel
  4. Uppdatera alla system som använder den gamla

Support


Version: 1.0
Senast uppdaterad: 2026-02-16