From 63f61dbd3eabd05a3f67704e437d7a54ef53088c Mon Sep 17 00:00:00 2001 From: Daniil Svetlov Date: Sat, 4 Apr 2026 17:36:18 +0300 Subject: [PATCH 1/2] feat: add support for NetBox 4.5+ v2 API tokens NetBox 4.5 introduced v2 API tokens (nbt_ prefix) that require Bearer authorization instead of Token. Auto-detect token version by prefix and use the appropriate authorization header keyword. - nbt_* tokens -> Authorization: Bearer - legacy tokens -> Authorization: Token (backward compatible) Co-Authored-By: Claude Opus 4.6 (1M context) --- README.md | 2 ++ module/netbox/connection.py | 4 +++- settings-example.ini | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce8c6be..bf09a1f 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ In order to updated data in NetBox you need a NetBox API token. * auth * secrets * users +* Both v1 (legacy) and v2 tokens (NetBox 4.5+, `nbt_` prefix) are supported. + The correct authorization header (`Token` or `Bearer`) is detected automatically. A short description can be found [here](https://docs.netbox.dev/en/stable/integrations/rest-api/#authentication) diff --git a/module/netbox/connection.py b/module/netbox/connection.py index 02ba53b..591228a 100644 --- a/module/netbox/connection.py +++ b/module/netbox/connection.py @@ -152,8 +152,10 @@ def create_session(self) -> requests.Session: requests.Session: session handler of new NetBox session """ + token = self.settings.api_token + keyword = "Bearer" if token.startswith("nbt_") else "Token" header = { - "Authorization": f"Token {self.settings.api_token}", + "Authorization": f"{keyword} {token}", "User-Agent": f"netbox-sync/{__version__}", "Content-Type": "application/json" } diff --git a/settings-example.ini b/settings-example.ini index ec2d5f1..9ea577a 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -45,6 +45,7 @@ ; Requires an NetBox API token with full permissions on all objects except 'auth', ; 'secrets' and 'users' +; Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are supported. api_token = XYZ ; Requires a hostname or IP which points to your NetBox instance From f3894c158c23459d781655480900db6a82d532c0 Mon Sep 17 00:00:00 2001 From: Daniil Svetlov Date: Sun, 5 Apr 2026 04:11:48 +0300 Subject: [PATCH 2/2] fix: move v2 token comment to config.py and regenerate settings-example Per maintainer review: add v2 token description to ConfigOption in config.py and regenerate settings-example.ini via netbox-sync.py -gc. Co-Authored-By: Claude Opus 4.6 (1M context) --- module/netbox/config.py | 3 ++- settings-example.ini | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/module/netbox/config.py b/module/netbox/config.py index 7bf00f1..a4f1c89 100644 --- a/module/netbox/config.py +++ b/module/netbox/config.py @@ -26,7 +26,8 @@ def __init__(self): ConfigOption("api_token", str, description="""Requires an NetBox API token with full permissions on all objects except - 'auth', 'secrets' and 'users' + 'auth', 'secrets' and 'users'. + Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are supported. """, config_example="XYZ", mandatory=True, diff --git a/settings-example.ini b/settings-example.ini index 9ea577a..b67a46c 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -44,8 +44,8 @@ [netbox] ; Requires an NetBox API token with full permissions on all objects except 'auth', -; 'secrets' and 'users' -; Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are supported. +; 'secrets' and 'users'. Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are +; supported. api_token = XYZ ; Requires a hostname or IP which points to your NetBox instance