NetBox API Extraction: Token Authentication and Offset Pagination
Use NetBox DRF token authentication with limit/offset pagination to extract bulk IPAM and DCIM data in Python automation scripts.
02 Jun 2026, 16:48 UTC

Decision and constraints
You need to read bulk IPAM or DCIM data from NetBox into an automation script—for example, exporting all assigned IP addresses or device inventory for a CMDB sync. The script must authenticate without interactive login and page through potentially thousands of records.
Assumed constraints: read-only access, scripted execution with no browser, a recent NetBox major release with the REST API enabled, and a user account that can hold an API token.
Supported options compared
| Option | Best for | Limitation |
|---|---|---|
| DRF Token authentication | Scripted API access | Long-lived secret; must be stored securely |
| Session authentication | Browser-based UI only | Not suitable for headless scripts |
| Basic authentication | Legacy or internal tools | Discouraged; sends credentials with every request |
| limit/offset pagination | Simple sequential reads | Performance degrades at high offsets |
| Cursor pagination | Large stable datasets | Not broadly exposed in NetBox API |
Trade-offs
DRF Token authentication (Django REST Framework token) is the standard for scripted access because it avoids sending passwords repeatedly and works outside a browser session. The trade-off is that a token is long-lived: if it leaks, an attacker can read whatever objects the associated user can see until the token is rotated.
limit/offset pagination is easy to implement: request a page, inspect the next field, and increment offset by limit until next is null. The risk is database performance. As offset grows, the database must scan and discard earlier rows before returning the requested page. Keep page sizes modest and filter by fields such as updated or created when you only need recent changes.
Implementation: paginated IPAM extraction in Python
Run this on a host that can reach the NetBox API, such as an automation runner or CI job. You need a NetBox API token created in the admin UI under Tokens, and the associated user must have view permission for the objects you request.
import os
import sys
import requests
NETBOX_URL = os.getenv("NETBOX_URL", "https://netbox.example.com/api")
API_TOKEN = os.getenv("NETBOX_TOKEN")
LIMIT = 100
if not API_TOKEN:
print("Set NETBOX_TOKEN before running", file=sys.stderr)
sys.exit(1)
headers = {
"Authorization": f"Token {API_TOKEN}",
"Accept": "application/json",
}
def fetch_ip_addresses():
offset = 0
all_results = []
while True:
url = f"{NETBOX_URL}/ipam/ip-addresses/"
params = {"limit": LIMIT, "offset": offset}
response = requests.get(url, headers=headers, params=params, timeout=30)
response.raise_for_status()
payload = response.json()
results = payload.get("results", [])
all_results.extend(results)
next_url = payload.get("next")
if not next_url:
break
offset += LIMIT
return all_results
if __name__ == "__main__":
addresses = fetch_ip_addresses()
print(f"Retrieved {len(addresses)} IP addresses")
Placeholders: replace NETBOX_URL with your deployment root, and set NETBOX_TOKEN in the environment rather than hardcoding it. The script sends Authorization: Token <key> on every request and follows the next link until it is null.
Risks: a token with broad object permissions exposes all corresponding data. Store it in a secrets manager or CI variable, not in version control. If you increase LIMIT beyond NetBox's MAX_PAGE_SIZE setting, the server may clamp the page or reject the request.
Validation
- Create a token in the NetBox admin UI under Tokens for a user with view rights to ipam.IPAddress.
- From a terminal with network access to NetBox, run:
curl -s -o /tmp/netbox-page.json -w "%{http_code}\n" \
-H "Authorization: Token $NETBOX_TOKEN" \
-H "Accept: application/json" \
"$NETBOX_URL/api/ipam/ip-addresses/?limit=5&offset=0"
Expected check: HTTP 200 and a JSON body containing count, next, previous, and results. Inspect the file:
python3 -c "import json; d=json.load(open('/tmp/netbox-page.json')); print(d.keys()); print(d['count'])"
- Page through results by incrementing
offsetand confirm the final response hasnextequal to null. - Check NetBox release notes for your target version to confirm authentication and pagination defaults, because behavior can differ between major releases.
Limitations
- Anonymous read access is disabled by default; do not rely on
EXEMPT_VIEW_PERMISSIONSfor automation. - Deep offset pagination slows as the database scans discarded rows. Prefer smaller pages and time-based filters for large extractions.
- Tokens are long-lived. Rotate immediately if exposed and never commit them to version control.
- This guide covers read-only extraction. Write operations require additional permission checks and are not covered here.
How to check the result
After running the script or curl command, verify that the number of objects in results matches the requested limit on intermediate pages, and that the final page returns fewer objects or an empty list with next: null. If you receive HTTP 401, the token is missing, expired, or invalid. If you receive HTTP 403, the token's user lacks view permission for the requested model.
This is a conservative draft for human review. Verify token lifecycle, page size limits, and pagination behavior against your NetBox version before deploying to production.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.