Endpoint
Endpoint objects provide CRUD operations for NetBox API endpoints. They are created automatically when you access an attribute on an App instance.
Overview
import pynetbox
nb = pynetbox.api('http://localhost:8000', token='your-token')
# Accessing an attribute on an App returns an Endpoint
devices = nb.dcim.devices # Endpoint
# Endpoint methods perform CRUD operations
all_devices = devices.all()
device = devices.get(1)
filtered = devices.filter(site='headquarters')
new_device = devices.create(name='test', site=1, device_type=1, role=1)
Slugs with underscores
Attribute access converts underscores to dashes (nb.dcim.ip_addresses → the ip-addresses endpoint), since Python attribute names can't contain dashes. For the rare endpoint whose URL slug legitimately contains an underscore (e.g. some custom-objects plugin types), use App.endpoint() to pass the slug verbatim:
# Attribute access — underscores become dashes:
nb.plugins.custom_objects.my_object # → .../custom-objects/my-object/
# endpoint() — slug used as-is:
nb.plugins.custom_objects.endpoint("my_object").all() # → .../custom-objects/my_object/
Endpoint Class
pynetbox.core.endpoint.Endpoint
Represent actions available on endpoints in the Netbox API.
Takes name and app passed from App() and builds the correct
url to make queries to and the proper Response object to return
results in.
Parameters
- api (Api): Takes Api created at instantiation.
- app (App): Takes App.
- name (str): Name of endpoint passed to App().
- model (obj, optional): Custom model for given app.
- literal_name (bool, optional): When True, use
nameverbatim as the URL slug instead of converting underscores to dashes. Used to reach endpoints whose slug legitimately contains underscores (e.g. custom objects). Defaults to False.
Note
In order to call NetBox endpoints with dashes in their
names you should convert the dash to an underscore.
(E.g. querying the ip-addresses endpoint is done with
nb.ipam.ip_addresses.all().)
For the rare endpoint whose slug really does contain underscores,
use App.endpoint() (e.g.
nb.plugins.custom_objects.endpoint("my_custom_object")) which sets
literal_name=True so no conversion is applied.
Source code in pynetbox/core/endpoint.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
all(limit=0, offset=None)
Queries the 'ListView' of a given endpoint.
Returns all objects from an endpoint.
Parameters
- limit (int, optional): Overrides the max page size on paginated returns. This defines the number of records that will be returned with each query to the Netbox server. The queries will be made as you iterate through the result set.
- offset (int, optional): Overrides the offset on paginated returns.
Returns
A RecordSet object.
Examples
devices = list(nb.dcim.devices.all())
for device in devices:
print(device.name)
# test1-leaf1
# test1-leaf2
# test1-leaf3
If you want to iterate over the results multiple times then encapsulate them in a list like this:
devices = list(nb.dcim.devices.all())
This will cause the entire result set to be fetched from the server.
Source code in pynetbox/core/endpoint.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
choices()
Returns all choices from the endpoint if it has them.
Returns
Dictionary of available choices.
Examples
choices = nb.dcim.devices.choices()
print(choices['status'])
{
'label': 'Active',
'value': 'active'
}
Source code in pynetbox/core/endpoint.py
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
count(*args, **kwargs)
Returns the count of objects in a query.
Takes named arguments that match the usable filters on a given endpoint. If an argument is passed then it's used as a freeform search argument if the endpoint supports it.
Parameters
- args (str, optional): Freeform search string that's accepted on given endpoint.
- kwargs (str, optional): Any search argument the endpoint accepts can be added as a keyword arg.
Returns
Integer of count of objects.
Examples
nb.dcim.devices.count(site='test1')
# 27
Source code in pynetbox/core/endpoint.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
create(*args, **kwargs)
Creates an object on an endpoint.
Takes named arguments that match the given endpoint's available fields. Returns a new object.
Parameters
- args: Not used.
- kwargs: Fields and values to create the object with.
Returns
A Record object.
Examples
Creating a new device:
new_device = nb.dcim.devices.create(
name='test-device',
device_type=1,
device_role=1,
site=1
)
Creating a new device with a nested object:
new_device = nb.dcim.devices.create(
name='test-device',
device_type={'id': 1},
device_role={'id': 1},
site={'id': 1}
)
Source code in pynetbox/core/endpoint.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
delete(objects)
Deletes objects from NetBox.
Takes a list of objects and deletes them from NetBox.
Parameters
- objects (list): A list of Record objects to delete.
Returns
True if the delete operation was successful.
Examples
devices = nb.dcim.devices.filter(site='test1')
nb.dcim.devices.delete(devices)
Source code in pynetbox/core/endpoint.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
filter(*args, **kwargs)
Queries the 'ListView' of a given endpoint.
Takes named arguments that match the usable filters on a given endpoint. If an argument is passed then it's used as a freeform search argument if the endpoint supports it.
Parameters
- args (str, optional): Freeform search string that's accepted on given endpoint.
- kwargs (str, optional): Any search argument the endpoint accepts can be added as a keyword arg.
- limit (int, optional): Overrides the max page size on paginated returns. This defines the number of records that will be returned with each query to the Netbox server. The queries will be made as you iterate through the result set.
- offset (int, optional): Overrides the offset on paginated returns.
- strict_filters (bool, optional): Overrides the global filter validation per-request basis.
Returns
A RecordSet object.
Examples
To return a list of objects matching a named argument filter:
devices = nb.dcim.devices.filter(role='leaf-switch')
for device in devices:
print(device.name)
# test1-leaf1
# test1-leaf2
# test1-leaf3
devices = nb.dcim.devices.filter(site='site-1')
for device in devices:
print(device.name)
# test1-a2-leaf1
# test2-a2-leaf2
Note
If a keyword argument is incorrect a TypeError will not be returned by pynetbox. Instead, pynetbox will return
all records filtered up to the last correct keyword argument. For example, if we used site="Site 1" instead of
site=site-1 when using filter on the devices endpoint, then pynetbox will return all devices across all
sites instead of devices at Site 1.
Using a freeform query along with a named argument:
devices = nb.dcim.devices.filter('a3', role='leaf-switch')
for device in devices:
print(device.name)
# test1-a3-leaf1
# test1-a3-leaf2
Source code in pynetbox/core/endpoint.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
get(*args, **kwargs)
Queries the DetailsView of a given endpoint.
Parameters
- key (int, optional): id for the item to be retrieved.
- kwargs: Accepts the same keyword args as filter(). Any search argument the endpoint accepts can be added as a keyword arg.
- strict_filters (bool, optional): Overrides the global filter validation per-request basis. Handled by the filter() method.
Returns
A single Record object or None
Raises
ValueError: if kwarg search return more than one value.
Examples
Referencing with a kwarg that only returns one value:
nb.dcim.devices.get(name='test1-a3-tor1b')
# test1-a3-tor1b
Referencing with an id:
nb.dcim.devices.get(1)
# test1-edge1
Using multiple named arguments. For example, retrieving the location when the location name is not unique and used in multiple sites:
nb.locations.get(site='site-1', name='Row 1')
# Row 1
Source code in pynetbox/core/endpoint.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
update(objects)
Updates objects in NetBox.
Takes a list of objects and updates them in NetBox.
Parameters
- objects (list): A list of Record objects to update.
Returns
A list of Record objects.
Examples
devices = nb.dcim.devices.filter(site='test1')
for device in devices:
device.status = 'active'
nb.dcim.devices.update(devices)
Source code in pynetbox/core/endpoint.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
DetailEndpoint Class
DetailEndpoint represents a detail route on an existing record (e.g. /api/ipam/prefixes/{id}/available-ips/). It is returned by model-specific properties such as Prefixes.available_ips, not constructed directly.
pynetbox.core.endpoint.DetailEndpoint
Enables read/write operations on detail endpoints.
Endpoints like available-ips that are detail routes off
traditional endpoints are handled with this class.
Source code in pynetbox/core/endpoint.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | |
create(data=None, **kwargs)
The write operation for a detail endpoint.
Creates objects on a detail endpoint in NetBox.
Parameters
- data (dict/list, optional): A dictionary or list containing the key/value pair of the items you're creating on the parent object. Defaults to empty dict which will create a single item with default values.
- kwargs: Alternative to
data, fields and values to create the object with — mirrorsEndpoint.create(). Cannot be combined withdata.
Returns
A Record object or list of Record objects created from data created in NetBox.
Source code in pynetbox/core/endpoint.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | |
list(**kwargs)
The view operation for a detail endpoint.
Returns the response from NetBox for a detail endpoint.
Parameters
- kwargs: Key/value pairs that get converted into URL
parameters when passed to the endpoint.
E.g.
.list(method='get_facts')would be converted to.../?method=get_facts.
Returns
A Record object or list of Record objects created from data retrieved from NetBox.
Source code in pynetbox/core/endpoint.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | |
ROMultiFormatDetailEndpoint Class
A read-only detail endpoint that supports multiple response formats. Used for endpoints (such as rack elevation) that can return either structured JSON or raw content like SVG.
pynetbox.core.endpoint.ROMultiFormatDetailEndpoint
Bases: RODetailEndpoint
Read-only detail endpoint supporting multiple response formats.
Handles endpoints that return data in different formats based on query parameters. Supports both structured data (JSON) and raw formats (e.g., SVG).
The endpoint inspects the 'render' parameter to determine response format: - No parameter or render='json': Returns structured JSON data - render='svg': Returns raw SVG content
Examples
rack = nb.dcim.racks.get(123)
rack.elevation.list() # Returns: list of rack unit objects
rack.elevation.list(render='svg') # Returns: SVG string
rack.elevation.list(render='json') # Returns: list of rack unit objects
Source code in pynetbox/core/endpoint.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 | |
list(**kwargs)
Returns data in the requested format.
Parameters
- kwargs: Key/value pairs that get converted into URL parameters. Supports 'render' parameter for format selection.
Returns
- If render is non-JSON format: Raw content (string)
- If render is 'json' or absent: Structured data (list/generator)
Source code in pynetbox/core/endpoint.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 | |