DNS API Powershell example

Skip to main content
Du bist hier:
Drucken

DNS API Powershell example

Below you will find examples for using the DNS API with PowerShell.

Authentication

Using API Key (recommended)

You can find your API key in your customer profile under Account > Customer Profile > API Access. Enable API access there and copy the displayed API key.

$apiKey = "YOUR_API_KEY_FROM_WHMCS"
$headers = @{"X-API-Key"=$apiKey; "Content-Type"="application/json"}

Alternative: Basic Auth (not recommended with special characters in password)

$username = "your-email@example.ch"
$password = "your-password"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo); "Content-Type"="application/json"}

List all zones

$result = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$result.zones | Format-Table id, name

Get zone ID by domain name

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

if ($zoneId) {
    Write-Host "Zone ID for $domain is: $zoneId"
} else {
    Write-Host "Zone not found!"
}

List records of a zone

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$result = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/records"
$result.records | Format-Table name, type, data, ttl

Add record

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$body = @{
    name = "test.$domain"
    type = "A"
    data = "192.168.1.1"
    ttl = 3600
} | ConvertTo-Json

$result = Invoke-RestMethod -Method Post -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body
Write-Host "Record ID: $($result.record_id)"

Update record

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$body = @{
    name = "test.$domain"
    type = "A"
    old_data = "192.168.1.1"
    data = "192.168.1.2"
    ttl = 3600
} | ConvertTo-Json

Invoke-RestMethod -Method Put -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

Delete record

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$body = @{
    name = "test.$domain"
    type = "A"
    data = "192.168.1.2"
} | ConvertTo-Json

Invoke-RestMethod -Method Delete -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

ACME DNS-01 Challenge (Let’s Encrypt)

Automatic Propagation: For _acme-challenge.* records, the API automatically waits until all DNS servers have the record. The response contains propagation details.

# Configuration - copy API key from WHMCS customer profile
$apiKey = "YOUR_API_KEY_FROM_WHMCS"
$headers = @{"X-API-Key"=$apiKey; "Content-Type"="application/json"}
$domain = "example.ch"
$token = "your-acme-token-here"

# Get zone ID
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

# Add ACME challenge token
$body = @{
    name = "_acme-challenge.$domain"
    type = "TXT"
    data = "`"$token`""
    ttl = 60
} | ConvertTo-Json

$result = Invoke-RestMethod -Method Post -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

# Check if propagation succeeded
if ($result.propagation.propagated) {
    Write-Host "DNS propagated in $($result.propagation.time) seconds"
    Start-Sleep -Seconds 5
} else {
    Write-Host "Warning: Propagation not complete!"
}

# === Perform ACME validation here ===

# After validation: delete token
$body = @{
    name = "_acme-challenge.$domain"
    type = "TXT"
} | ConvertTo-Json

Invoke-RestMethod -Method Delete -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

Complete script as function

function Set-FirestormDnsRecord {
    param(
        [string]$ApiKey,
        [string]$Domain,
        [string]$RecordName,
        [string]$RecordType,
        [string]$RecordData,
        [int]$TTL = 3600
    )

    $headers = @{"X-API-Key"=$ApiKey; "Content-Type"="application/json"}

    # Get zone ID
    $zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
    $zoneId = ($zones.zones | Where-Object { $_.name -eq $Domain }).id

    if (-not $zoneId) {
        throw "Zone $Domain not found!"
    }

    # Add record
    $body = @{
        name = $RecordName
        type = $RecordType
        data = $RecordData
        ttl = $TTL
    } | ConvertTo-Json

    return Invoke-RestMethod -Method Post -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body
}

# Usage:
Set-FirestormDnsRecord -ApiKey "YOUR_API_KEY_FROM_WHMCS" -Domain "example.ch" -RecordName "test.example.ch" -RecordType "A" -RecordData "192.168.1.1"

Note

Before using the API, you must enable API access in the admin panel under Account > Customer Profile > API Access. After activation, your personal API key will be displayed.

Tags:
Related Post