Nautobot API Client Library
pynautobot is the official API client library for Nautobot, a Network Source of Truth and Automation Platform. It provides a Pythonic interface to interact with the Nautobot REST API. The current version is 3.1.0, and new major versions are released to align with Nautobot core major releases, with frequent minor and patch updates in between.
Common errors
-
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))cause The Python script cannot establish a connection to the specified Nautobot URL. This could be due to an incorrect URL, the Nautobot service not running, or network/firewall issues.fixVerify the `NAUTOBOT_URL` environment variable or the `url` parameter in your `pynautobot.api()` call. Ensure the Nautobot server is running and accessible from the machine running the script. Check firewall rules. -
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://nautobot.example.com/api/...
cause The Nautobot API client is attempting to make a request without a valid authentication token, or the provided token has insufficient permissions for the requested action.fixEnsure the `NAUTOBOT_TOKEN` environment variable or the `token` parameter in your `pynautobot.api()` call is set correctly with a valid API token. Verify that the token has the necessary read/write permissions for the resources being accessed. -
pynautobot.exceptions.ObjectDoesNotExist: No object found for query: {'slug': 'my-missing-object'}cause You are attempting to retrieve a specific object (e.g., using `.get()`) with criteria that do not match any existing object in Nautobot. This often happens with unique fields like 'slug' or 'name'.fixDouble-check the lookup criteria (e.g., slug, name, ID). If the object might not exist, use `.filter()` which returns an empty list instead of raising an exception if no objects match, or wrap your `.get()` call in a `try...except pynautobot.exceptions.ObjectDoesNotExist` block.
Warnings
- breaking pynautobot v3.x.x is specifically designed for compatibility with Nautobot 3.x.x and later. Using pynautobot v3.x.x with older Nautobot 2.x.x instances will result in API incompatibility errors and unexpected behavior.
- deprecated pynautobot v2.x.x is now in Long Term Maintenance (LTM) mode, only receiving critical bug and security fixes for Nautobot 2.4. New feature development is exclusively on the pynautobot v3.x.x branch.
- gotcha Prior to pynautobot v3.1.0, accessing nested attributes of related objects (e.g., `device.site.name` directly) would often require calling `full_details()` on the parent object first, leading to additional API calls or `AttributeError` if not handled. While v3.1.0 improved this, older code might still make inefficient calls.
Install
-
pip install pynautobot
Imports
- api
import pynautobot.api
from pynautobot import api
Quickstart
import os
from pynautobot import api
NAUTOBOT_URL = os.environ.get("NAUTOBOT_URL", "https://nautobot.example.com")
NAUTOBOT_TOKEN = os.environ.get("NAUTOBOT_TOKEN", "YOUR_NAUTOBOT_TOKEN_HERE")
if not NAUTOBOT_URL or not NAUTOBOT_TOKEN:
print("Please set NAUTOBOT_URL and NAUTOBOT_TOKEN environment variables.")
exit(1)
try:
# Initialize the pynautobot API client
nautobot = api(url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN)
# Fetch all devices
print(f"Connecting to Nautobot at {NAUTOBOT_URL}...")
devices = nautobot.dcim.devices.all()
if devices:
print(f"Found {len(devices)} devices:")
for device in devices[:5]: # Print first 5 devices
print(f"- {device.name} ({device.device_type.model}) in {device.site.name}")
else:
print("No devices found.")
# Example: Create a new tag
# new_tag = nautobot.extras.tags.create(name="my-new-tag", slug="my-new-tag", description="A tag created by pynautobot")
# print(f"Created tag: {new_tag.name}")
except Exception as e:
print(f"An error occurred: {e}")