Skip to main content

NetBox API client library

Project description

Pynetbox

Python API client library for NetBox.

Note: Version 6.7 and later of the library only supports NetBox 3.3 and above.

Compatibility

Each pyNetBox Version listed below has been tested with its corresponding NetBox Version.

NetBox Version Plugin Version
4.6 7.7.0
4.5 7.6.1
4.5 7.6.0
4.4 7.5.0
4.3 7.5.0
4.2 7.5.0
4.1 7.5.0
4.0.6 7.4.1
4.0.0 7.3.4
3.7 7.3.0
3.6 7.2.0
3.5 7.1.0
3.3 7.0.0

Installation

To install run pip install pynetbox.

Alternatively, you can clone the repo and run python setup.py install.

Quick Start

The full pynetbox API is documented on GitHub Pages, but the following should be enough to get started using it.

To begin, import pynetbox and instantiate the API.

import pynetbox
nb = pynetbox.api(
    'http://localhost:8000',
    token='d6f4e314a5b5fefd164995169f28ae32d987704f'
)

The first argument the .api() method takes is the NetBox URL. There are a handful of named arguments you can provide, but in most cases none are required to simply pull data. In order to write, the token argument should to be provided.

Queries

The pynetbox API is setup so that NetBox's apps are attributes of the .api() object, and in turn those apps have attribute representing each endpoint. Each endpoint has a handful of methods available to carry out actions on the endpoint. For example, in order to query all the objects in the devices endpoint you would do the following:

>>> devices = nb.dcim.devices.all()
>>> for device in devices:
...     print(device.name)
...
test1-leaf1
test1-leaf2
test1-leaf3
>>>

Note that the all() and filter() methods are generators and return an object that can be iterated over only once. If you are going to be iterating over it repeatedly you need to either call the all() method again, or encapsulate the results in a list object like this:

>>> devices = list(nb.dcim.devices.all())

Threading

pynetbox supports multithreaded calls for .filter() and .all() queries. It is highly recommended you have MAX_PAGE_SIZE in your Netbox install set to anything except 0 or None. The default value of 1000 is usually a good value to use. To enable threading, add threading=True parameter to the .api:

nb = pynetbox.api(
    'http://localhost:8000',
    threading=True,
)

Filters validation

NetBox doesn't validate filters passed to the GET API endpoints, which are accessed with .get() and .filter(). If a filter is incorrect, NetBox silently returns the entire database table content. Pynetbox allows to check provided parameters against NetBox OpenAPI specification before doing the call, and raise an exception if a parameter is incorrect.

This can be enabled globally by setting strict_filters=True in the API object initialization:

nb = pynetbox.api(
    'http://localhost:8000',
    strict_filters=True,
)

This can also be enabled and disabled on a per-request basis:

# Disable for one request when enabled globally.
# Will not raise an exception and return the entire Device table.
nb.dcim.devices.filter(non_existing_filter="aaaa", strict_filters=False)

# Enable for one request when not enabled globally.
# Will raise an exception.
nb.dcim.devices.filter(non_existing_filter="aaaa", strict_filters=True)

Running Tests

First, create and activate a Python virtual environment in the pynetbox directory to isolate the project dependencies:

python3 -m venv venv
source venv/bin/activate

Install both requirements files:

pip install -r requirements.txt
pip install -r requirements-dev.txt

The test suite requires Docker to be installed and running, as it will download and launch netbox-docker containers during test execution.

With Docker installed and running, execute the following command to run the test suite:

pytest

Project details


Release history Release notifications | RSS feed

This version

7.7.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynetbox-7.7.0.tar.gz (98.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynetbox-7.7.0-py3-none-any.whl (40.0 kB view details)

Uploaded Python 3

File details

Details for the file pynetbox-7.7.0.tar.gz.

File metadata

  • Download URL: pynetbox-7.7.0.tar.gz
  • Upload date:
  • Size: 98.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pynetbox-7.7.0.tar.gz
Algorithm Hash digest
SHA256 6ebf1a68fe19e3c9528f1fcf214ceee719ace937c5f78f3f33964738e3cd3f25
MD5 a9e9600f8fdc011caab3fb56d48665ef
BLAKE2b-256 f799dd840cd6a09956b2f762319e9c88c6afcd58b8094a127c7bd0e7017caca1

See more details on using hashes here.

File details

Details for the file pynetbox-7.7.0-py3-none-any.whl.

File metadata

  • Download URL: pynetbox-7.7.0-py3-none-any.whl
  • Upload date:
  • Size: 40.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pynetbox-7.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48de7c1d23aa0a16e78e76e68a58fbdd72782e13be7f25facafdf325ef0d3640
MD5 bb8ad2db26a67604ebdbf03016f8937f
BLAKE2b-256 8ebd43f14b5d0c3f7a801e6bc40295667ae6be262560da063cd240f012410309

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page