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.
NextDNS API
Section titled “NextDNS API”Endpoints Used
Section titled “Endpoints Used”| Endpoint | Purpose | Method |
|---|---|---|
/profiles/{id}/denylist | Get denylist | GET |
/profiles/{id}/denylist | Add to denylist | POST |
/profiles/{id}/denylist/{domain} | Remove from denylist | DELETE |
/profiles/{id}/allowlist | Get allowlist | GET |
/profiles/{id}/allowlist | Add to allowlist | POST |
/profiles/{id}/allowlist/{domain} | Remove from allowlist | DELETE |
/profiles/{id}/parentalControl | Get Parental Control settings | GET |
/profiles/{id}/parentalControl | Update Parental Control settings | PATCH |
/profiles/{id}/parentalControl/categories/{id} | Update category | PATCH |
/profiles/{id}/parentalControl/services | Add service | POST |
/profiles/{id}/parentalControl/services/{id} | Update service | PATCH |
/profiles/{id}/parentalControl/services/{id} | Remove service | DELETE |
Authentication
Section titled “Authentication”All requests include the API key header:
X-Api-Key: your_api_keyBase URL
Section titled “Base URL”https://api.nextdns.ioRate Limiting
Section titled “Rate Limiting”Built-in Rate Limiter
Section titled “Built-in Rate Limiter”NextDNS Blocker includes a sliding window rate limiter to prevent API abuse.
Configuration
Section titled “Configuration”Set in .env:
RATE_LIMIT_REQUESTS=30 # Max requests per windowRATE_LIMIT_WINDOW=60 # Window in secondsDefault Settings
Section titled “Default Settings”| Setting | Default | Description |
|---|---|---|
RATE_LIMIT_REQUESTS | 30 | Max requests per 60 seconds |
RATE_LIMIT_WINDOW | 60 | Window duration in seconds |
Behavior
Section titled “Behavior”When limit is reached:
- Request is queued
- Thread waits for window to slide
- Request proceeds when under limit
- No error is raised
Algorithm
Section titled “Algorithm”Uses sliding window with time.monotonic():
- Unaffected by system clock changes
- Thread-safe using Condition variables
- O(1) time complexity
Caching
Section titled “Caching”Denylist Cache
Section titled “Denylist Cache”To reduce API calls, the current denylist is cached.
Configuration
Section titled “Configuration”CACHE_TTL=60 # Cache duration in secondsDefault Settings
Section titled “Default Settings”| Setting | Default | Range |
|---|---|---|
CACHE_TTL | 60 | 1-3600 seconds |
Behavior
Section titled “Behavior”- First sync: Fetch denylist from API
- Cache result with timestamp
- Subsequent syncs: Use cache if valid
- Cache expires: Fetch fresh data
Cache Invalidation
Section titled “Cache Invalidation”Cache is automatically invalidated:
- After TTL expires
- When domain is added/removed
- On explicit sync with verbose mode
Request Handling
Section titled “Request Handling”Timeout
Section titled “Timeout”API_TIMEOUT=10 # Seconds| Setting | Default | Recommended |
|---|---|---|
API_TIMEOUT | 10 | 10-30 seconds |
Retries
Section titled “Retries”API_RETRIES=3| Setting | Default | Max |
|---|---|---|
API_RETRIES | 3 | 5 |
Exponential Backoff
Section titled “Exponential Backoff”Failed requests use exponential backoff:
| Attempt | Wait Time |
|---|---|
| 1 | 1 second |
| 2 | 2 seconds |
| 3 | 4 seconds |
| 4 | 8 seconds |
| 5 | 16 seconds |
With random jitter (0-1 second) to prevent thundering herd.
Maximum Backoff
Section titled “Maximum Backoff”Capped at 30 seconds per attempt.
API Usage Patterns
Section titled “API Usage Patterns”Normal Sync
Section titled “Normal Sync”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 allowlistTypical API Calls
Section titled “Typical API Calls”| Scenario | API Calls |
|---|---|
| No changes needed | 1 (GET denylist, cached) |
| 1 domain block | 2 (GET + PUT) |
| 5 domain changes | 6 (GET + 5 PUT/DELETE) |
Watchdog Impact
Section titled “Watchdog Impact”With 2-minute sync interval:
- 30 syncs per hour
- ~720 syncs per day
- Most use cached GET (1 call)
Error Handling
Section titled “Error Handling”API Errors
Section titled “API Errors”| HTTP Code | Meaning | Action |
|---|---|---|
| 200 | Success | Continue |
| 401 | Unauthorized | Check API key |
| 404 | Not found | Check profile ID |
| 429 | Rate limited | Wait and retry |
| 500+ | Server error | Retry with backoff |
Handling Rate Limit (429)
Section titled “Handling Rate Limit (429)”If NextDNS returns 429:
- Wait for
Retry-Afterheader - Or wait 60 seconds
- Retry request
- Increment retry counter
Connection Errors
Section titled “Connection Errors”| Error | Handling |
|---|---|
| Timeout | Retry with backoff |
| DNS failure | Retry with backoff |
| Connection refused | Retry with backoff |
| SSL error | Fail (security issue) |
Monitoring API Usage
Section titled “Monitoring API Usage”Verbose Mode
Section titled “Verbose Mode”nextdns-blocker config push --verboseShows:
API call: GET /profiles/abc123/denylistResponse: 200 OK (3 domains)Cache: MISS (fetching fresh data)Log Analysis
Section titled “Log Analysis”# Count API calls todaygrep "API call" ~/.local/share/nextdns-blocker/logs/app.log | \ grep "$(date +%Y-%m-%d)" | wc -lOptimizing API Usage
Section titled “Optimizing API Usage”Reduce API Calls
Section titled “Reduce API Calls”-
Increase cache TTL for stable configs:
Terminal window CACHE_TTL=300 # 5 minutes -
Fewer domains = fewer potential changes
-
Stable schedules = fewer transitions
Reduce Errors
Section titled “Reduce Errors”-
Validate config before sync:
Terminal window nextdns-blocker config validate -
Test with dry run:
Terminal window nextdns-blocker config push --dry-run -
Check credentials:
Terminal window nextdns-blocker init
NextDNS API Limits
Section titled “NextDNS API Limits”Official Limits
Section titled “Official Limits”Check NextDNS documentation for current limits. As of 2024:
- Reasonable use expected
- No published hard limits
- Abuse may result in throttling
Best Practices
Section titled “Best Practices”- Don’t sync more than every 2 minutes
- Batch operations when possible
- Use caching
- Handle rate limits gracefully
Troubleshooting
Section titled “Troubleshooting””Rate limit exceeded”
Section titled “”Rate limit exceeded””Internal rate limiting triggered:
- Wait 60 seconds
- Reduce sync frequency
- Increase
RATE_LIMIT_WINDOW
”API timeout"
Section titled “”API timeout"”# Increase timeoutAPI_TIMEOUT=30"Connection refused”
Section titled “"Connection refused””- Check internet connection
- Check if NextDNS is accessible
- Try manual curl:
Terminal window curl -I https://api.nextdns.io
“Authentication failed”
Section titled ““Authentication failed””- Verify API key
- Check for extra whitespace
- Regenerate API key if needed