APIs and REST for Network Engineers
Endpoints, Resources, and Parameters
What you'll learn
- What a REST 'resource' is, and how a URL identifies one
- The difference between a path parameter and a query parameter
- How to construct a correct request for a specific device and a filtered result
- How this compares to devices.csv-style inventory addressing
Listen to a guided explanation of this lesson.
Spoken narration isn't supported in this browser.
A URL names a resource
In REST, a URL identifies a resource — a specific thing the API knows about. /devices/SW1/interfaces doesn't describe an action; it names "SW1's interfaces." The HTTP method (GET, so far) is what says what to do with it.
GET /devices/SW1/interfaces
^devices ^SW1 ^interfaces
resource which one sub-resourceEvery segment of the path narrows down which resource this request is about — the URL structure itself carries meaning, unlike a CLI command's flat argument list.
Path parameters: which resource
The SW1 in /devices/SW1/interfaces is a path parameter — part of the URL itself, identifying exactly which device.
import requests
for hostname in ["SW1", "SW3"]:
response = requests.get(f"http://127.0.0.1:8765/devices/{hostname}/interfaces")
print(hostname, "->", response.status_code, response.json()["hostname"])An f-string builds a different URL per device — the same 'one pattern, many devices' idea as Jinja2's {% for %} loop and Ansible's loop:, now applied to constructing a URL.
SW1 -> 200 SW1
SW3 -> 200 SW3Real, verified output — the same request pattern, a different device each time, just by changing the path parameter.
Query parameters: filtering the result
A query parameter narrows down what's returned, without naming a different resource. It's appended after a ?:
response = requests.get(
"http://127.0.0.1:8765/devices/SW1/interfaces",
params={"status": "up"},
)
print(response.url)
print(response.json())requests builds the ?status=up query string for you from the params dict — no manual string concatenation needed.
http://127.0.0.1:8765/devices/SW1/interfaces?status=up
{'hostname': 'SW1', 'interfaces': [{'name': 'GigabitEthernet1/0/1', 'status': 'up'}]}Real, verified output — SW1 has two interfaces total, but only the one matching status=up came back. The resource (SW1's interfaces) didn't change; what's returned about it did.
Path parameter vs. query parameter
- /devices/SW1/interfacespart of the URL structure
- Identifies WHICH resource
- Required — the URL doesn't make sense without it
- ?status=upappended after ?
- Filters WHAT's returned about that resource
- Usually optional — omit it, get everything
devices.csv addressed one thing; this addresses two
devices.csv rows each named one device. A REST URL like /devices/SW1/interfaces names both a device (path parameter) and a sub-resource of it (interfaces) — with query parameters layered on top to filter further. More structure than a flat CSV row, for a genuinely more structured target.
Key takeaways
- A REST URL names a resource; the HTTP method says what to do with it.
- A path parameter (like SW1 in /devices/SW1/interfaces) is part of the URL and identifies which resource — required, and usually built with an f-string.
- A query parameter (like ?status=up) filters what's returned about that resource — usually optional, and requests builds it for you from a params dict.
- The two are combined routinely, not alternatives — verified directly against this module's own mock API.