Skip to content

API & Rate Limiting

NextDNS Blocker interacts with the NextDNS API to manage your denylist and allowlist. This page covers API usage, caching, and rate limiting.

EndpointPurposeMethod
/profiles/{id}/denylistGet denylistGET
/profiles/{id}/denylistAdd to denylistPOST
/profiles/{id}/denylist/{domain}Remove from denylistDELETE
/profiles/{id}/allowlistGet allowlistGET
/profiles/{id}/allowlistAdd to allowlistPOST
/profiles/{id}/allowlist/{domain}Remove from allowlistDELETE
/profiles/{id}/parentalControlGet Parental Control settingsGET
/profiles/{id}/parentalControlUpdate Parental Control settingsPATCH
/profiles/{id}/parentalControl/categories/{id}Update categoryPATCH
/profiles/{id}/parentalControl/servicesAdd servicePOST
/profiles/{id}/parentalControl/services/{id}Update servicePATCH
/profiles/{id}/parentalControl/services/{id}Remove serviceDELETE

All requests include the API key header:

X-Api-Key: your_api_key
https://api.nextdns.io

NextDNS Blocker includes a sliding window rate limiter to prevent API abuse.

Set in .env:

Terminal window
RATE_LIMIT_REQUESTS=30 # Max requests per window
RATE_LIMIT_WINDOW=60 # Window in seconds
SettingDefaultDescription
RATE_LIMIT_REQUESTS30Max requests per 60 seconds
RATE_LIMIT_WINDOW60Window duration in seconds

When limit is reached:

  1. Request is queued
  2. Thread waits for window to slide
  3. Request proceeds when under limit
  4. No error is raised

Uses sliding window with time.monotonic():

  • Unaffected by system clock changes
  • Thread-safe using Condition variables
  • O(1) time complexity

To reduce API calls, the current denylist is cached.

Terminal window
CACHE_TTL=60 # Cache duration in seconds
SettingDefaultRange
CACHE_TTL601-3600 seconds
  1. First sync: Fetch denylist from API
  2. Cache result with timestamp
  3. Subsequent syncs: Use cache if valid
  4. Cache expires: Fetch fresh data

Cache is automatically invalidated:

  • After TTL expires
  • When domain is added/removed
  • On explicit sync with verbose mode
Terminal window
API_TIMEOUT=10 # Seconds
SettingDefaultRecommended
API_TIMEOUT1010-30 seconds
Terminal window
API_RETRIES=3
SettingDefaultMax
API_RETRIES35

Failed requests use exponential backoff:

AttemptWait Time
11 second
22 seconds
34 seconds
48 seconds
516 seconds

With random jitter (0-1 second) to prevent thundering herd.

Capped at 30 seconds per attempt.

1. GET /denylist (cached or fresh)
2. For each domain needing block:
PUT /denylist/{domain}
3. For each domain needing unblock:
DELETE /denylist/{domain}
4. Similar for allowlist
ScenarioAPI Calls
No changes needed1 (GET denylist, cached)
1 domain block2 (GET + PUT)
5 domain changes6 (GET + 5 PUT/DELETE)

With 2-minute sync interval:

  • 30 syncs per hour
  • ~720 syncs per day
  • Most use cached GET (1 call)
HTTP CodeMeaningAction
200SuccessContinue
401UnauthorizedCheck API key
404Not foundCheck profile ID
429Rate limitedWait and retry
500+Server errorRetry with backoff

If NextDNS returns 429:

  1. Wait for Retry-After header
  2. Or wait 60 seconds
  3. Retry request
  4. Increment retry counter
ErrorHandling
TimeoutRetry with backoff
DNS failureRetry with backoff
Connection refusedRetry with backoff
SSL errorFail (security issue)
Terminal window
nextdns-blocker config push --verbose

Shows:

API call: GET /profiles/abc123/denylist
Response: 200 OK (3 domains)
Cache: MISS (fetching fresh data)
Terminal window
# Count API calls today
grep "API call" ~/.local/share/nextdns-blocker/logs/app.log | \
grep "$(date +%Y-%m-%d)" | wc -l
  1. Increase cache TTL for stable configs:

    Terminal window
    CACHE_TTL=300 # 5 minutes
  2. Fewer domains = fewer potential changes

  3. Stable schedules = fewer transitions

  1. Validate config before sync:

    Terminal window
    nextdns-blocker config validate
  2. Test with dry run:

    Terminal window
    nextdns-blocker config push --dry-run
  3. Check credentials:

    Terminal window
    nextdns-blocker init

Check NextDNS documentation for current limits. As of 2024:

  • Reasonable use expected
  • No published hard limits
  • Abuse may result in throttling
  1. Don’t sync more than every 2 minutes
  2. Batch operations when possible
  3. Use caching
  4. Handle rate limits gracefully

Internal rate limiting triggered:

  • Wait 60 seconds
  • Reduce sync frequency
  • Increase RATE_LIMIT_WINDOW
Terminal window
# Increase timeout
API_TIMEOUT=30
  1. Check internet connection
  2. Check if NextDNS is accessible
  3. Try manual curl:
    Terminal window
    curl -I https://api.nextdns.io
  1. Verify API key
  2. Check for extra whitespace
  3. Regenerate API key if needed