api.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. =============================================
  2. API Reference
  3. =============================================
  4. API Usage
  5. ==========================
  6. Most actions that can be performed via API can be performed via UI. We recommend managing your networks using the official netmaker-ui project. However, Netmaker can also be run without the UI, and all functions can be achieved via API calls. If your use case requires using Netmaker without the UI or you need to do some troubleshooting/advanced configuration, using the API directly may help.
  7. Authentication
  8. ==============
  9. API calls must be authenticated via a header of the format `-H "Authorization: Bearer <YOUR_SECRET_KEY>"` There are two methods to obtain YOUR_SECRET_KEY:
  10. 1. Using the masterkey. By default, this value is "secret key," but you should change this on your instance and keep it secure. This value can be set via env var at startup or in a config file (config/environments/< env >.yaml). See the [general usage](./USAGE.md) documentation for more details.
  11. 2. Using a JWT received for a node. This can be retrieved by calling the `/api/nodes/<network>/authenticate` endpoint, as documented below.
  12. Format of Calls for Curl
  13. ========================
  14. Requests take the format of
  15. .. code-block::
  16. curl -H "Authorization: Bearer <YOUR_SECRET_KEY>" -H 'Content-Type: application/json' localhost:8081/api/path/to/endpoint
  17. API Documentation
  18. =================
  19. Networks API
  20. ------------
  21. **Get All Networks:** `/api/networks`, `GET`
  22. **Create Network:** `/api/network`, `POST`
  23. **Get Network:** `/api/networks/{network id}`, `GET`
  24. **Update Network:** `/api/networks/{network id}`, `PUT`
  25. **Delete Network:** `/api/networks/{network id}`, `DELETE`
  26. **Cycle PublicKeys on all Nodes:** `/api/networks/{network id}/keyupdate`, `POST`
  27. Networks API Call Examples
  28. --------------------------
  29. .. code-block::
  30. Get All Networks: curl -H "Authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/networks | jq
  31. Create Network: curl -d '{"addressrange":"10.70.0.0/16","netid":"skynet"}' -H "Authorization: Bearer YOUR_SECRET_KEY" -H 'Content-Type: application/json' localhost:8081/api/networks
  32. Get Network: curl -H "Authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/networks/skynet | jq
  33. Update Network: curl -X PUT -d '{"displayname":"my-house"}' -H "Authorization: Bearer YOUR_SECRET_KEY" -H 'Content-Type: application/json' localhost:8081/api/networks/skynet
  34. Delete Network: curl -X DELETE -H "Authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/networks/skynet
  35. Cycle PublicKeys on all Nodes: curl -X POST -H "Authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/networks/skynet/keyupdate
  36. Access Keys API
  37. ---------------
  38. **Get All Keys:** `/api/networks/{network id}/keys`, `GET`
  39. **Create Key:** `/api/networks/{network id}/keys`, `GET`
  40. **Delete Key:** `/api/networks/{network id}/keys/{keyname}`, `DELETE`
  41. Access Keys API Call Examples
  42. -----------------------------
  43. .. code-block::
  44. Get All Keys: curl -H "Authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/networks/skynet/keys | jq
  45. Create Key: curl -d '{"uses":10,"name":"mykey"}' -H "Authorization: Bearer YOUR_SECRET_KEY" -H 'Content-Type: application/json' localhost:8081/api/networks/skynet/keys
  46. Delete Key: curl -X DELETE -H "Authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/networks/skynet/keys/mykey
  47. Nodes API
  48. ---------
  49. **Get All Nodes:** `/api/nodes`, `GET`
  50. **Get Network Nodes:** `/api/nodes/{network id}`, `GET`
  51. **Create Node:** `/api/nodes/{network id}`, `POST`
  52. **Get Node:** `/api/nodes/{network id}/{macaddress}`, `GET`
  53. **Update Node:** `/api/nodes/{network id}/{macaddress}`, `PUT`
  54. **Delete Node:** `/api/nodes/{network id}/{macaddress}`, `DELETE`
  55. **Check In Node:** `/api/nodes/{network id}/{macaddress}/checkin`, `POST`
  56. **Create a Gateway:** `/api/nodes/{network id}/{macaddress}/creategateway`, `POST`
  57. **Delete a Gateway:** `/api/nodes/{network id}/{macaddress}/deletegateway`, `DELETE`
  58. **Uncordon (Approve) a Pending Node:** `/api/nodes/{network id}/{macaddress}/uncordon`, `POST`
  59. **Get Last Modified Date (Last Modified Node in Network):** `/api/nodes/adm/{network id}/lastmodified`, `GET`
  60. **Authenticate:** `/api/nodes/adm/{network id}/authenticate`, `POST`
  61. Nodes API Call Examples
  62. -----------------------
  63. .. code-block::
  64. Get All Nodes: curl -H "Authorization: Bearer YOUR_SECRET_KEY" http://localhost:8081/api/nodes | jq
  65. Get Network Nodes: curl -H "Authorization: Bearer YOUR_SECRET_KEY" http://localhost:8081/api/nodes/skynet | jq
  66. Create Node: curl -d '{ "endpoint": 100.200.100.200, "publickey": aorijqalrik3ajflaqrdajhkr,"macaddress": "8c:90:b5:06:f1:d9","password": "reallysecret","localaddress": "172.16.16.1","accesskey": "aA3bVG0rnItIRXDx","listenport": 6400}' -H 'Content-Type: application/json' -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/nodes/skynet
  67. Get Node: curl -H "Authorization: Bearer YOUR_SECRET_KEY" http://localhost:8081/api/nodes/skynet/{macaddress} | jq
  68. Update Node: curl -X PUT -d '{"name":"laptop1"}' -H 'Content-Type: application/json' -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/nodes/skynet/8c:90:b5:06:f1:d9
  69. Delete Node: curl -X DELETE -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/skynet/nodes/8c:90:b5:06:f1:d9
  70. Create a Gateway: curl -d '{ "rangestring": "172.31.0.0/16", "interface": "eth0"}' -H 'Content-Type: application/json' -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/nodes/skynet/8c:90:b5:06:f1:d9/creategateway
  71. Delete a Gateway: curl -X DELETE -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/nodes/skynet/8c:90:b5:06:f1:d9/deletegateway
  72. Approve a Pending Node: curl -X POST -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/nodes/skynet/8c:90:b5:06:f1:d9/approve
  73. Get Last Modified Date (Last Modified Node in Network): curl -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/nodes/adm/skynet/lastmodified
  74. Authenticate: curl -d '{"macaddress": "8c:90:b5:06:f1:d9", "password": "YOUR_PASSWORD"}' -H 'Content-Type: application/json' localhost:8081/api/nodes/adm/skynet/authenticate
  75. Users API
  76. -----------------------
  77. **Note:** Only able to create Admin user at this time. The "user" is only used by the `user interface <https://github.com/gravitl/netmaker-ui>`_ to authenticate the single admin user.
  78. **Get User:** `/api/users/{username}`, `GET`
  79. **Update User:** `/api/users/{username}`, `PUT`
  80. **Delete User:** `/api/users/{username}`, `DELETE`
  81. **Check for Admin User:** `/api/users/adm/hasadmin`, `GET`
  82. **Create Admin User:** `/api/users/adm/createadmin`, `POST`
  83. **Authenticate:** `/api/users/adm/authenticate`, `POST`
  84. Users API Calls Examples
  85. ------------------------
  86. .. code-block::
  87. Get User: curl -H "Authorization: Bearer YOUR_SECRET_KEY" http://localhost:8081/api/users/{username} | jq
  88. Update User: curl -X PUT -d '{"password":"noonewillguessthis"}' -H 'Content-Type: application/json' -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/users/{username}
  89. Delete User: curl -X DELETE -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/users/{username}
  90. Check for Admin User: curl -H "Authorization: Bearer YOUR_SECRET_KEY" http://localhost:8081/api/users/adm/hasadmin
  91. Create Admin User: curl -d '{ "username": "smartguy", "password": "YOUR_PASS"}' -H 'Content-Type: application/json' -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/users/adm/createadmin
  92. Authenticate: curl -d '{"username": "smartguy", "password": "YOUR_PASS"}' -H 'Content-Type: application/json' localhost:8081/api/nodes/adm/skynet/authenticate
  93. Server Management API
  94. ---------------------
  95. The Server Mgmt. API allows you to add and remove the server from networks.
  96. **Add to Network:** `/api/server/addnetwork/{network id}`, `POST`
  97. **Remove from Network:** `/api/server/removenetwork/{network id}`, `DELETE`
  98. **Add to Network:** `curl -X POST -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/server/addnetwork/{network id}`
  99. **Remove from Network:** `curl -X DELETE -H "authorization: Bearer YOUR_SECRET_KEY" localhost:8081/api/server/removenetwork/{network id}`
  100. File Server API
  101. ---------------
  102. **Get File:** `/meshclient/files/{filename}`, `GET`
  103. **Example:** `curl localhost:8081/meshclient/files/meshclient`