{"id":8504,"library":"pynautobot","title":"Nautobot API Client Library","description":"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.","status":"active","version":"3.1.0","language":"en","source_language":"en","source_url":"https://github.com/nautobot/pynautobot","tags":["nautobot","api","network automation","dcim","ipam","source of truth"],"install":[{"cmd":"pip install pynautobot","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"HTTP client for API communication.","package":"requests","optional":false},{"reason":"HTTP client dependency for requests; recent versions widened support due to CVEs.","package":"urllib3","optional":false}],"imports":[{"note":"The 'api' object is directly exposed at the top level of the pynautobot package for convenience.","wrong":"import pynautobot.api","symbol":"api","correct":"from pynautobot import api"}],"quickstart":{"code":"import os\nfrom pynautobot import api\n\nNAUTOBOT_URL = os.environ.get(\"NAUTOBOT_URL\", \"https://nautobot.example.com\")\nNAUTOBOT_TOKEN = os.environ.get(\"NAUTOBOT_TOKEN\", \"YOUR_NAUTOBOT_TOKEN_HERE\")\n\nif not NAUTOBOT_URL or not NAUTOBOT_TOKEN:\n    print(\"Please set NAUTOBOT_URL and NAUTOBOT_TOKEN environment variables.\")\n    exit(1)\n\ntry:\n    # Initialize the pynautobot API client\n    nautobot = api(url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN)\n\n    # Fetch all devices\n    print(f\"Connecting to Nautobot at {NAUTOBOT_URL}...\")\n    devices = nautobot.dcim.devices.all()\n\n    if devices:\n        print(f\"Found {len(devices)} devices:\")\n        for device in devices[:5]: # Print first 5 devices\n            print(f\"- {device.name} ({device.device_type.model}) in {device.site.name}\")\n    else:\n        print(\"No devices found.\")\n\n    # Example: Create a new tag\n    # new_tag = nautobot.extras.tags.create(name=\"my-new-tag\", slug=\"my-new-tag\", description=\"A tag created by pynautobot\")\n    # print(f\"Created tag: {new_tag.name}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your pynautobot version matches your Nautobot instance version: use pynautobot v3.x.x for Nautobot v3.x.x+ and pynautobot v2.x.x (LTM) for Nautobot v2.x.x.","message":"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.","severity":"breaking","affected_versions":"3.0.0+"},{"fix":"Upgrade your Nautobot instance to v3.x.x+ and pynautobot to v3.x.x+ to benefit from new features and ongoing development. If stuck on Nautobot 2.x.x, use pynautobot v2.x.x but be aware of limited support.","message":"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.","severity":"deprecated","affected_versions":"<3.0.0"},{"fix":"For pynautobot v3.1.0 and later, nested attributes are often directly accessible without explicit `full_details()`. Review your code to remove unnecessary `full_details()` calls for related objects if upgrading, as they might still trigger extra API requests. If on older versions, be aware of this limitation and use `full_details()` as needed.","message":"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.","severity":"gotcha","affected_versions":"<3.1.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify 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.","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.","error":"requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))"},{"fix":"Ensure 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.","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.","error":"requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://nautobot.example.com/api/..."},{"fix":"Double-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.","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'.","error":"pynautobot.exceptions.ObjectDoesNotExist: No object found for query: {'slug': 'my-missing-object'}"}]}