Creating a prefix via the API

981 views
Skip to first unread message

vasil.d...@rootleveltech.com

unread,
Jun 25, 2018, 9:49:04 AM6/25/18
to NetBox
I am trying to create a new prefix via the API. However I am not able to find any documentation about it. I figured out how to create a child prefix on top of prefix ID, but not how to create a parent prefix. Is there such feature in the API?

Brian Candler

unread,
Jun 26, 2018, 2:51:55 AM6/26/18
to NetBox
Can you show what you're doing?  In the data model there is no explicit parent/child relationship between prefixes - that is, there's no "parent_id" field.  It's just that 192.0.2.128/25 falls within 192.0.2.0/24 (say), and that's handled via postgres' knowledge of how IP subnets work.

If you go to the API docs (http://x.x.x.x/api/docs/) on your system, then scroll down to ipam_prefixes_create, there is sample data.  The only mandatory field is "prefix", which is the actual IP prefix in the form "x.x.x.x/y".  So simply posting this one field should work.  If it doesn't, please show us the exact post you sent and the response you got back.
Message has been deleted

vasil.d...@rootleveltech.com

unread,
Jun 26, 2018, 6:36:54 AM6/26/18
to NetBox
I am trying to execute a POST request to /api/ipam/prefixes/ to add the new prefix, but I am getting a blank response. Is that the correct way to add a new prefix?

Brian Candler

unread,
Jun 27, 2018, 2:12:03 AM6/27/18
to NetBox
On Tuesday, 26 June 2018 11:36:54 UTC+1, vasil.d...@rootleveltech.com wrote:
I am trying to execute a POST request to /api/ipam/prefixes/ to add the new prefix

And what was the *content* of the POST?

Are you able to add other objects, just not prefixes?

vasil.d...@rootleveltech.com

unread,
Jun 29, 2018, 9:16:20 AM6/29/18
to NetBox
This is what I am trying:

curl -H "Authorization: Token ........." -H "Content-Type: application/json" -H "Accept: application/json; indent=4" http://dctest.rootleveltech.com/api/ipam/prefixes/ -X PUT --data '{"prefix": "23.239.128.0/24", "status": "active"}'

{

    "detail": "Method \"PUT\" not allowed."

Brian Candler

unread,
Jun 29, 2018, 5:33:26 PM6/29/18
to NetBox
You need to use a POST, not a PUT, to create a new object.

Browse to /api/docs/ in a web browser and you should see (in green) POST /ipam/prefixes/ in the row which has ipam_prefixes_create on the right-hand side.

PUT /ipam/prefixes/{id}/ is used to update an existing object. 
Reply all
Reply to author
Forward
0 new messages