Nautobot API Client Library

3.1.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart initializes the pynautobot API client using environment variables for the Nautobot URL and API token. It then fetches and prints the names of the first five devices found in your Nautobot instance. Ensure `NAUTOBOT_URL` and `NAUTOBOT_TOKEN` are set in your environment.

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}")

view raw JSON →