UniFi for libdns
This package implements the libdns interfaces for UniFi Network, allowing you to manage DNS records through the UniFi Network's DNS policy API.
This provider supports the following DNS record types:
- A - IPv4 address records
- AAAA - IPv6 address records
- CNAME - Canonical name (alias) records
- MX - Mail exchange records (Without TTL)
- TXT - Text records (Without TTL)
- SRV - Service records (Without TTL)
The provider requires three pieces of configuration:
- API Key - Your UniFi API authentication key
- Site ID - The UUID of the UniFi site containing the DNS policies
- Host URL - The base URL of your UniFi controller API
package main
import (
"context"
"log"
"net/netip"
"time"
"github.com/libdns/libdns"
"github.com/libdns/unifi"
)
func main() {
provider := unifi.Provider{
ApiKey: "your-api-key",
SiteId: "your-site-uuid",
BaseUrl: "https://192.168.1.1/proxy/network/integration/v1",
}
// List existing records
records, err := provider.GetRecords(context.Background(), "example.com")
if err != nil {
log.Fatal(err)
}
// Add a new A record
newRecords, err := provider.AppendRecords(context.Background(), "example.com", []libdns.Record{
libdns.Address{
Name: "www",
IP: netip.MustParseAddr("192.0.2.1"),
TTL: 3600 * time.Second,
},
})
if err != nil {
log.Fatal(err)
}
}- Log into your UniFi Network Console
- Navigate to Integrations
- Create a new API Key
- Set the key as your
UNIFI_API_KEYenvironment variable or use it asApiKeyparameter
Execute the following request to retrive the ID of your sites:
curl -k -X GET 'https://YOU_UNIFI_NETWORK_CONTROLLER/proxy/network/integration/v1/sites' -H 'X-API-KEY: YOUR_API_KEY' -H 'Accept: application/json'
The base URL is the base path of your UniFi Network API endpoint:
- Default:
https://YOU_UNIFI_NETWORK_CONTROLLER/proxy/network/integration/v1(replace IP with your device IP) - CloudKey/Controller:
https://YOU_UNIFI_NETWORK_CONTROLLER:8443/proxy/network/integration/v1