NetBox / Source of Truth
Writing Back Safely: Creating and Updating Records
What you'll learn
- pynetbox's real create() and update() signatures and behavior
- Why read-before-write applies to a source of truth just as much as a device
- A real, verified create-then-update-then-reread cycle
- The same credential-safety rule, applied to a NetBox token
Listen to a guided explanation of this lesson.
Spoken narration isn't supported in this browser.
The same safety boundary, a new place it applies
Read-before-write didn't stop mattering
APIs and REST for Network Engineers (Phase 3.1) taught reading before writing as a safety boundary for device changes. A NetBox record isn't a device -- but if automation trusts it as a source of truth, a wrong write here is just as real a mistake as a wrong device config. The same discipline applies.
Record.save(self)
Record.update(self, data)
Record.delete(self)Verified via inspect.signature against pynetbox.core.response.Record -- the object type every query in Lesson 3 already returned.
Creating a record: real, verified
new_device = nb.dcim.devices.create(name="SW4", serial="SW4-SN-004")
print("created:", new_device.id, new_device.name, new_device.serial)Endpoint.create() -- verified via inspect.signature to accept the same *args/**kwargs shape as filter()/get().
created: 100 SW4 SW4-SN-004Real, verified output. The mock backend assigned a real new id (100) and returned the created record, exactly matching NetBox's real documented create response shape (HTTP 201 with the new object).
Updating a record: real, verified, and re-read to confirm
sw1 = nb.dcim.devices.get(name="SW1")
sw1.update({"serial": "SW1-SN-001-UPDATED"})
sw1_again = nb.dcim.devices.get(name="SW1")
print("re-fetched serial:", sw1_again.serial)Not just trusting update()'s return value -- a fresh get() call afterward proves the change actually persisted on the backend, the same reread-to-confirm discipline used throughout this curriculum.
update() returned: True
sw1.serial after update: SW1-SN-001-UPDATED
re-fetched serial: SW1-SN-001-UPDATEDReal, verified output across all three checks -- the update() call itself, the in-memory object afterward, and a completely fresh read from the backend all agree.
Credentials: the same rule, a different kind of secret
A NetBox token is a credential too
Every example in this module sources its token via os.environ.get("NETBOX_TOKEN", ...) -- never a string literal. A NetBox API token that can write records is exactly as sensitive as a device password: anyone holding it can change what your entire automation fleet believes is true.
Key takeaways
- Record.create()/update()/delete() are real, verified pynetbox write operations -- the same object type Lesson 3's queries already returned.
- A real create-then-update-then-reread cycle was executed and verified -- not just the write call's own return value, but an independent fresh read confirming persistence.
- Read-before-write (Phase 3.1) applies to a source of truth exactly as it applies to a device -- a wrong record is a real mistake, not a lower-stakes one.
- NetBox tokens follow the same os.environ.get() credential-safety rule as every device password since Netmiko Fundamentals -- never a string literal.