Skip to content

Commit 63f61db

Browse files
Daniil Svetlovclaude
andcommitted
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 <token> - legacy tokens -> Authorization: Token <token> (backward compatible) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e972c94 commit 63f61db

3 files changed

Lines changed: 6 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ In order to updated data in NetBox you need a NetBox API token.
8888
* auth
8989
* secrets
9090
* users
91+
* Both v1 (legacy) and v2 tokens (NetBox 4.5+, `nbt_` prefix) are supported.
92+
The correct authorization header (`Token` or `Bearer`) is detected automatically.
9193

9294
A short description can be found [here](https://docs.netbox.dev/en/stable/integrations/rest-api/#authentication)
9395

module/netbox/connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ def create_session(self) -> requests.Session:
152152
requests.Session: session handler of new NetBox session
153153
"""
154154

155+
token = self.settings.api_token
156+
keyword = "Bearer" if token.startswith("nbt_") else "Token"
155157
header = {
156-
"Authorization": f"Token {self.settings.api_token}",
158+
"Authorization": f"{keyword} {token}",
157159
"User-Agent": f"netbox-sync/{__version__}",
158160
"Content-Type": "application/json"
159161
}

settings-example.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
; Requires an NetBox API token with full permissions on all objects except 'auth',
4747
; 'secrets' and 'users'
48+
; Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are supported.
4849
api_token = XYZ
4950

5051
; Requires a hostname or IP which points to your NetBox instance

0 commit comments

Comments
 (0)