-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1148 from digitalocean/develop
Release v2.0.0
- Loading branch information
Showing
287 changed files
with
34,057 additions
and
22,542 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
The NetBox API employs token-based authentication. For convenience, cookie authentication can also be used when navigating the browsable API. | ||
|
||
# Tokens | ||
|
||
A token is a unique identifier that identifies a user to the API. Each user in NetBox may have one or more tokens which he or she can use to authenticate to the API. To create a token, navigate to the API tokens page at `/user/api-tokens/`. | ||
|
||
Each token contains a 160-bit key represented as 40 hexadecimal characters. When creating a token, you'll typically leave the key field blank so that a random key will be automatically generated. However, NetBox allows you to specify a key in case you need to restore a previously deleted token to operation. | ||
|
||
By default, a token can be used for all operations available via the API. Deselecting the "write enabled" option will restrict API requests made with the token to read operations (e.g. GET) only. | ||
|
||
Additionally, a token can be set to expire at a specific time. This can be useful if an external client needs to be granted temporary access to NetBox. | ||
|
||
# Authenticating to the API | ||
|
||
By default, read operations will be available without authentication. In this case, a token may be included in the request, but is not necessary. | ||
|
||
``` | ||
$ curl -H "Accept: application/json; indent=4" http://localhost/api/dcim/sites/ | ||
{ | ||
"count": 10, | ||
"next": null, | ||
"previous": null, | ||
"results": [...] | ||
} | ||
``` | ||
|
||
However, if the [`LOGIN_REQUIRED`](../configuration/optional-settings/#login_required) configuration setting has been set to `True`, all requests must be authenticated. | ||
|
||
``` | ||
$ curl -H "Accept: application/json; indent=4" http://localhost/api/dcim/sites/ | ||
{ | ||
"detail": "Authentication credentials were not provided." | ||
} | ||
``` | ||
|
||
To authenticate to the API, set the HTTP `Authorization` header to the string `Token ` (note the trailing space) followed by the token key. | ||
|
||
``` | ||
$ curl -H "Authorization: Token d2f763479f703d80de0ec15254237bc651f9cdc0" -H "Accept: application/json; indent=4" http://localhost/api/dcim/sites/ | ||
{ | ||
"count": 10, | ||
"next": null, | ||
"previous": null, | ||
"results": [...] | ||
} | ||
``` | ||
|
||
Additionally, the browsable interface to the API (which can be seen by navigating to the API root `/api/` in a web browser) will attempt to authenticate requests using the same cookie that the normal NetBox front end uses. Thus, if you have logged into NetBox, you will be logged into the browsable API as well. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# API Examples | ||
|
||
Supported HTTP methods: | ||
|
||
* `GET`: Retrieve an object or list of objects | ||
* `POST`: Create a new object | ||
* `PUT`: Update an existing object | ||
* `DELETE`: Delete an existing object | ||
|
||
To authenticate a request, attach your token in an `Authorization` header: | ||
|
||
``` | ||
curl -H "Authorization: Token d2f763479f703d80de0ec15254237bc651f9cdc0" | ||
``` | ||
|
||
### Retrieving a list of sites | ||
|
||
Send a `GET` request to the object list endpoint. The response contains a paginated list of JSON objects. | ||
|
||
``` | ||
$ curl -H "Accept: application/json; indent=4" http://localhost/api/dcim/sites/ | ||
{ | ||
"count": 14, | ||
"next": null, | ||
"previous": null, | ||
"results": [ | ||
{ | ||
"id": 6, | ||
"name": "Corporate HQ", | ||
"slug": "corporate-hq", | ||
"region": null, | ||
"tenant": null, | ||
"facility": "", | ||
"asn": null, | ||
"physical_address": "742 Evergreen Terrace, Springfield, USA", | ||
"shipping_address": "", | ||
"contact_name": "", | ||
"contact_phone": "", | ||
"contact_email": "", | ||
"comments": "", | ||
"custom_fields": {}, | ||
"count_prefixes": 108, | ||
"count_vlans": 46, | ||
"count_racks": 8, | ||
"count_devices": 254, | ||
"count_circuits": 6 | ||
}, | ||
... | ||
] | ||
} | ||
``` | ||
|
||
### Retrieving a single site by ID | ||
|
||
Send a `GET` request to the object detail endpoint. The response contains a single JSON object. | ||
|
||
``` | ||
$ curl -H "Accept: application/json; indent=4" http://localhost/api/dcim/sites/6/ | ||
{ | ||
"id": 6, | ||
"name": "Corporate HQ", | ||
"slug": "corporate-hq", | ||
"region": null, | ||
"tenant": null, | ||
"facility": "", | ||
"asn": null, | ||
"physical_address": "742 Evergreen Terrace, Springfield, USA", | ||
"shipping_address": "", | ||
"contact_name": "", | ||
"contact_phone": "", | ||
"contact_email": "", | ||
"comments": "", | ||
"custom_fields": {}, | ||
"count_prefixes": 108, | ||
"count_vlans": 46, | ||
"count_racks": 8, | ||
"count_devices": 254, | ||
"count_circuits": 6 | ||
} | ||
``` | ||
|
||
### Creating a new site | ||
|
||
Send a `POST` request to the site list endpoint with token authentication and JSON-formatted data. Only mandatory fields are required. | ||
|
||
``` | ||
$ curl -X POST -H "Authorization: Token d2f763479f703d80de0ec15254237bc651f9cdc0" -H "Content-Type: application/json" -H "Accept: application/json; indent=4" http://localhost:8000/api/dcim/sites/ --data '{"name": "My New Site", "slug": "my-new-site"}' | ||
{ | ||
"id": 16, | ||
"name": "My New Site", | ||
"slug": "my-new-site", | ||
"region": null, | ||
"tenant": null, | ||
"facility": "", | ||
"asn": null, | ||
"physical_address": "", | ||
"shipping_address": "", | ||
"contact_name": "", | ||
"contact_phone": "", | ||
"contact_email": "", | ||
"comments": "" | ||
} | ||
``` | ||
|
||
### Modify an existing site | ||
|
||
Make an authenticated `PUT` request to the site detail endpoint. As with a create (POST) request, all mandatory fields must be included. | ||
|
||
``` | ||
$ curl -X PUT -H "Authorization: Token d2f763479f703d80de0ec15254237bc651f9cdc0" -H "Content-Type: application/json" -H "Accept: application/json; indent=4" http://localhost:8000/api/dcim/sites/16/ --data '{"name": "Renamed Site", "slug": "renamed-site"}' | ||
``` | ||
|
||
### Delete an existing site | ||
|
||
Send an authenticated `DELETE` request to the site detail endpoint. | ||
|
||
``` | ||
$ curl -v X DELETE -H "Authorization: Token d2f763479f703d80de0ec15254237bc651f9cdc0" -H "Content-Type: application/json" -H "Accept: application/json; indent=4" http://localhost:8000/api/dcim/sites/16/ | ||
* Connected to localhost (127.0.0.1) port 8000 (#0) | ||
> DELETE /api/dcim/sites/16/ HTTP/1.1 | ||
> User-Agent: curl/7.35.0 | ||
> Host: localhost:8000 | ||
> Authorization: Token d2f763479f703d80de0ec15254237bc651f9cdc0 | ||
> Content-Type: application/json | ||
> Accept: application/json; indent=4 | ||
> | ||
* HTTP 1.0, assume close after body | ||
< HTTP/1.0 204 No Content | ||
< Date: Mon, 20 Mar 2017 16:13:08 GMT | ||
< Server: WSGIServer/0.1 Python/2.7.6 | ||
< Vary: Accept, Cookie | ||
< X-Frame-Options: SAMEORIGIN | ||
< Allow: GET, PUT, PATCH, DELETE, OPTIONS | ||
< | ||
* Closing connection 0 | ||
``` | ||
|
||
The response to a successfull `DELETE` request will have code 204 (No Content); the body of the response will be empty. |
Oops, something went wrong.