Skip to content

Commit 30af617

Browse files
authored
Merge pull request #500 from dsvetlov/feature/netbox-4.5-v2-token-support
feat: add support for NetBox 4.5+ v2 API tokens (Bearer auth)
2 parents 026c918 + f3894c1 commit 30af617

4 files changed

Lines changed: 9 additions & 3 deletions

File tree

README.md

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

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

module/netbox/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def __init__(self):
2626
ConfigOption("api_token",
2727
str,
2828
description="""Requires an NetBox API token with full permissions on all objects except
29-
'auth', 'secrets' and 'users'
29+
'auth', 'secrets' and 'users'.
30+
Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are supported.
3031
""",
3132
config_example="XYZ",
3233
mandatory=True,

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
[netbox]
4545

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

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

0 commit comments

Comments
 (0)