NetBox API Client Library

7.6.1 · active · verified Sun Apr 12

pynetbox is the official Python API client library for NetBox, a popular open-source IPAM and DCIM solution. It provides a convenient, object-oriented interface for interacting with the NetBox REST API, abstracting away the complexities of raw HTTP requests. The library is actively maintained with frequent releases, currently at version 7.6.1, ensuring compatibility with the latest NetBox versions (supporting NetBox 3.3+ and recent 4.x features like v2 API tokens).

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a NetBox instance using `pynetbox`, retrieve the NetBox version, and fetch a list of devices. Ensure `NETBOX_URL` and `NETBOX_TOKEN` environment variables are set or replaced in the code.

import os
import pynetbox

NETBOX_URL = os.environ.get('NETBOX_URL', 'http://localhost:8000')
NETBOX_TOKEN = os.environ.get('NETBOX_TOKEN', 'YOUR_API_TOKEN_HERE') # Required for write operations

try:
    nb = pynetbox.api(NETBOX_URL, token=NETBOX_TOKEN)
    
    # Test connection and fetch NetBox version
    print(f"Connected to NetBox version: {nb.version}")

    # Fetch the first 5 devices
    devices = list(nb.dcim.devices.all()[:5])
    if devices:
        print("\nFirst 5 Devices:")
        for device in devices:
            print(f"  - {device.name} (ID: {device.id})")
    else:
        print("\nNo devices found.")

except pynetbox.RequestError as e:
    print(f"NetBox API Request Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →