Skip to content

How to Fix PowerDNS API Connection Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix PowerDNS API Connection Error. We cover key concepts, practical examples, and best practices.

PowerDNS API returns Connection refused or 401 Unauthorized when making API requests — the API endpoint is not enabled, the port is wrong, or the API key is missing.

The Problem

$ curl http://localhost:8081/api/v1/servers/localhost
{
  "error": "Unauthorized"
}

Step-by-Step Fix

Step 1: Enable the API in pdns.conf

# /etc/powerdns/pdns.conf
api=yes
api-key=YOUR_SECRET_API_KEY
webserver-address=0.0.0.0
webserver-port=8081
webserver-allow-from=127.0.0.1,10.0.0.0/8

Step 2: Test with API key

curl -H "X-API-Key: YOUR_SECRET_API_KEY" \
  http://localhost:8081/api/v1/servers/localhost

Expected:

{
  "type": "Server",
  "id": "localhost",
  "daemon_type": "authoritative",
  "version": "4.7.0",
  ...
}

Step 3: List zones via API

curl -s -H "X-API-Key: YOUR_SECRET_API_KEY" \
  http://localhost:8081/api/v1/servers/localhost/zones | jq .

Step 4: Create a zone via API

curl -X POST \
  -H "X-API-Key: YOUR_SECRET_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name":"example.com.","kind":"Native","nameservers":["ns1.example.com."]}' \
  http://localhost:8081/api/v1/servers/localhost/zones

Step 5: Check API access logs

sudo tail -f /var/log/powerdns/api.log

Prevention Tips

  • Restrict API access with webserver-allow-from
  • Use a strong random API key
  • Enable HTTPS for the API in production
  • Monitor API access logs for unauthorized attempts

Common Mistakes with api error

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world POWERDNS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does PowerDNS API return Connection refused?

The webserver is not enabled or is bound to the wrong interface. Check that api=yes is set in pdns.conf and the webserver port is correct. Restart PowerDNS after changes: sudo systemctl restart pdns.

How do I secure the PowerDNS API?

Restrict access with webserver-allow-from to specific IPs or networks. Use a strong API key (generated with openssl rand -base64 32). For production, proxy the API through Nginx with HTTPS and authentication.

What API version does PowerDNS support?

PowerDNS Authoritative Server supports API v1. The endpoint is /api/v1/servers/localhost. The recursor has a separate API on port 8082. Check your version with pdns_server --version and refer to the matching API documentation.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro