Skip to content

The API and Tokens

Outcome: you can drive NetBox the way its own automation does — a token in hand, the request shapes understood, and every earlier chapter's record readable in one call each. The web pages and the API are two doors into the same rooms; this chapter is about the second door, because it is the one that makes NetBox infrastructure instead of a notebook.

Tokens are credentials

Nothing about the API is anonymous: the lab's unauthenticated probe was refused in chapter 4, and access is granted by tokens, one per user. A token is a key to the map room (chapter 1's warning), and NetBox 4.7's current version 2 tokens are built like modern credentials:

  • Each token has a short key that identifies it, and a secret plaintext that is shown once, at creation.
  • Only a peppered hash of the secret is kept (the pepper is the API_TOKEN_PEPPERS line from chapter 3), so the database alone cannot reveal it.
  • A request presents it as Authorization: Bearer nbt_<key>.<plaintext> — the nbt_ prefix is how the server knows it is being shown a v2 token.

The lab created its token in Django's shell rather than the web pages (the same object either way), and learned the shape's sharp edge honestly: a token whose plaintext is generated for you and not captured at that moment is gone — only its hash remains, and the fix is a fresh token, not a recovery (the first, discarded token is part of transcripts t07-api-first-attempt-prefix.txt' story). Tokens can also expire, be limited to read-only use, and be restricted to source addresses; use those dials for anything real.

The wrong-prefix lesson

The lab's first API run failed every call with the same answer:

{"detail":"Invalid v1 token"}

The header had been sent without the nbt_ prefix, so the server read the v2 credential as a legacy v1 token and found no match. The error is precise once you know the two token formats exist; chapter 13 keeps it. With the prefix in place, the same calls returned 201 across the board (transcript t08-api-proof.txt).

The shapes

The lab's script (netbox-lab/api-proof.sh) is the chapter in executable form. Its vocabulary is small:

  • GET a collection to list, with a count: {"count": 1, "results": [...]}.
  • GET one record by its ID.
  • POST a JSON object to create; the answer is 201 and the record, ID included.
  • PATCH by ID to change named fields only (the primary-address step in chapter 7).
  • DELETE by ID to remove.

References between records are spent as IDs ("site": 1) and read back as small nested objects with the useful names resolved ("site": {"name": "Example North Lab", ...}) — writes are terse, reads are legible. Filtering a list is done with query parameters (?site=example-north-lab), and every record also carries the URL of its web page, so a script can hand a human a link. The status endpoint, GET /api/status/, answers with the versions of everything — the lab's reported NetBox 4.7.4 on Django 6.1.2 — and is the first call to make when something feels off.

Refusals are answers too

Two refusals from the lab deserve their place beside the successes, because a script must handle them anyway: a duplicate slug returns 400 with the field and reason in JSON; a bad token returns 403 with Invalid v2 token. Both are in t09-ui-login-and-refusals.txt. A well-behaved NetBox client treats these as information, not exceptions — they are the database's rules, arriving in the API's accent.

The other API

NetBox also exposes a GraphQL endpoint, where the caller names the fields it wants instead of receiving whole records. It is part of the same install and the same permissions. The lab did not exercise it; it is named here so you know it exists, and documented-only in this guide.