This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
pynetbox is a Python API client library for NetBox. It provides a Pythonic interface to interact with NetBox's REST API, supporting NetBox 3.3 and above (pynetbox 6.7+).
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
pip install -r requirements-dev.txtThe test suite requires Docker to be installed and running for integration tests.
# Run all tests
pytest
# Run only unit tests (fast, no Docker required)
pytest tests/unit
# Run only integration tests (requires Docker)
pytest tests/integration
# Run specific test file
pytest tests/test_api.py
# Run specific test function
pytest tests/test_api.py::ApiStatusTestCase::test_api_status
# Run tests matching a pattern
pytest -k "test_api"
# Run with coverage
pytest --cov=pynetbox tests/Integration tests can be run against specific NetBox versions:
# Test against specific NetBox versions (default: 4.4)
pytest tests/integration --netbox-versions 4.4
# Skip cleanup to leave Docker containers running
pytest --no-cleanup
# Use existing NetBox instance (skip Docker)
pytest -p no:docker --url-override http://localhost:8000# Run Ruff linter
ruff check pynetbox/ tests/
# Fix auto-fixable issues
ruff check --fix pynetbox/ tests/# Build and serve docs locally
mkdocs serve
# Deploy docs to GitHub Pages
mkdocs gh-deployThe codebase follows a layered architecture:
-
API Layer (
pynetbox/core/api.py):Apiclass is the main entry point- Initializes NetBox app endpoints (dcim, ipam, circuits, virtualization, extras, users, wireless, core, vpn, tenancy)
- Supports
pluginsviaPluginsAppfor accessing NetBox plugin APIs - Manages HTTP session, authentication tokens, threading, and strict filter validation
-
App Layer (
pynetbox/core/app.py):Appclass represents NetBox applications (dcim, ipam, etc.)- Dynamic attribute access returns
Endpointobjects PluginsApphandles plugin namespacing with dashes converted to underscores
-
Endpoint Layer (
pynetbox/core/endpoint.py):Endpointclass provides CRUD operations for API endpoints- Methods:
.all(),.filter(),.get(),.count(),.create(),.update(),.delete(),.choices() - Handles parameter validation against OpenAPI spec when
strict_filters=True - Converts underscores to dashes in endpoint names (e.g.,
ip_addresses→ip-addresses)
-
Query Layer (
pynetbox/core/query.py):Requestclass handles HTTP communication- Supports threading for
.all()and.filter()operations - Custom exceptions:
RequestError,AllocationError,ContentError,ParameterValidationError
-
Response Layer (
pynetbox/core/response.py):Recordclass represents individual API objectsRecordSetclass for collections (iterable, returned by.all()and.filter())- Records support dict-like access, attribute access, and serialization
.save()method for updating objects,.delete()for deletion
Custom Record classes in pynetbox/models/ provide specialized behavior for specific endpoints:
dcim.py: DCIM-specific models (Devices, Interfaces, Cables with tracing, etc.)ipam.py: IP address management (IpAddresses, Prefixes, VLANs, etc.)circuits.py: Circuit modelsvirtualization.py: Virtual machine modelsextras.py: Custom fields, tags, webhooksusers.py: User and permission modelswireless.py: Wireless endpoint modelscore.py: DataSources, Jobs, ObjectChanges modelsmapper.py:CONTENT_TYPE_MAPPERmaps NetBox content-type strings (e.g."dcim.device") to their custom Record classes; used when resolving polymorphic nested objects
- Lazy Loading:
Endpointobjects are created lazily viaApp.__getattr__; top-level app objects (dcim, ipam, etc.) are created eagerly inApi.__init__ - Threading Support: Enable with
threading=Truein API initialization for parallel pagination - Custom Sessions: Override
api.http_sessionfor custom SSL, timeouts, retries - Branch Support: Context manager
api.activate_branch()for NetBox branching plugin - Filter Validation:
strict_filters=Truevalidates parameters against OpenAPI spec before requests
When preparing objects for POST/PUT/PATCH operations, pynetbox automatically handles:
- Nested objects are reduced to IDs or simple values
- Custom fields are flattened
- Tags and certain list fields are treated as sets, not ordered lists
- Use
.serialize()method to see how an object will be sent to the API
Some NetBox objects like Cables have complex nested structures. The dcim.py models contain specialized handling:
TraceableRecord: Base class for objects that support cable tracingCables: Handles A/B terminations with proper serialization- DetailEndpoints vs regular Endpoints for objects without list views
tests/test_*.py: Older-style unit tests (circuits, tenancy, users, virtualization, wireless, api, app)tests/unit/: Newer unit tests that mock HTTP responsestests/integration/: Integration tests against real NetBox instances in Dockertests/conftest.py: Shared pytest configuration and fixtures (defines--netbox-versions,--no-cleanup,--url-overrideoptions)tests/integration/conftest.py: Docker setup for spinning up NetBox containers
The integration test framework uses pytest-docker to:
- Pull and launch netbox-docker containers
- Wait for NetBox to be ready
- Run tests against the live instance
- Clean up containers after tests (unless
--no-cleanupspecified)
- Main branch:
master - Current working branch visible in git status
- Commit messages should prefix with "Fixes" or "Closes" and issue number (e.g., "Closes #1234: Add IPv5 support")
- Pull requests require approval for accepted issues only (trivial documentation changes excepted)
Current version: 7.6.1
NetBox version compatibility is strict:
- pynetbox 7.6.1 supports NetBox 4.5.0
- pynetbox 7.5.0 supports NetBox 4.1, 4.2, 4.3
- pynetbox 7.4.1 supports NetBox 4.0.6
- pynetbox 7.0.0+ requires NetBox 3.3+
When working on features, verify compatibility with the supported NetBox versions via integration tests.