Using Husarnet Dashboard API

The code below illustrates an example workflow for the Husarnet API.

The example is written in Python but can be easily converted into any programming language.

This tutorial is based on the documentation in its entirety.

At the top of the file, all appropriate functions are defined. Below the definitions, a demo use case can be found.

Code#

import requests
from pprint import pprint # for prettier JSON output
import json
#import os
# It is highly recommended you don't store sensitive data in plaintext.
# Preferabely set an environment variable instead.
#API_TOKEN = os.environ.get('HUSARNET_API_TOKEN')
API_TOKEN = '<YOUR_API_TOKEN>'
ROOT_API_LINK = 'https://app.staging.husarnet.com'
custom_headers = {
'Authorization': f'Token {API_TOKEN}',
'Content-Type': 'application/json', # both JSON and URL-encoded data are supported for request body
}
def all_networks():
# get all networks
r = requests.get(f'{ROOT_API_LINK}/api/networks', headers=headers)
pprint(r.json())
return r.json()
def create_new_network(name: str):
create_network_data = {
'name': name,
}
create_network = requests.post(
f'{ROOT_API_LINK}/api/networks/',
headers=headers,
data=json.dumps(create_network_data),
allow_redirects=False, # sometimes the requests module might redirect "POST" to "GET"
)
print(create_network.status_code, '\n', create_network.text)
def get_network(id: int = None, url:str = None):
"""
set one of parameters:
id - if you have the network id
url - retrieved via GET /api/networks/
"""
headers = custom_headers
del headers['Content-Type'] # not necessary for GET requests
if id:
network = requests.get(f'{ROOT_API_LINK}/api/network/{id}', headers=headers)
return network.json()
elif url:
network = requests.get(f'{ROOT_API_LINK}{url}', headers=headers)
return network.json()
else:
print("Set id or url!")
return None
def delete_network(id: int = None, url: str = None):
if id:
r = requests.delete(f'{ROOT_API_LINK}/api/network/{id}', headers=custom_headers)
print(r.status_code)
elif url:
r = requests.delete(f'{ROOT_API_LINK}{url}', headers=custom_headers)
print(r.status_code)
else:
print("Set id or url!")
return None
def add_member_websetup(network_url: str, websetup_url: str, name: str, is_managed: bool):
member_data = {
'op': 'add-member',
'link': websetup_url,
'name': name,
'is-fully-managed': is_managed,
}
r = requests.patch(
f'{ROOT_API_LINK}{network_url}',
data=json.dumps(member_data),
headers=custom_headers
)
print('Add member status:', r.status_code)
def delete_member(network_url: str, member_id: int):
member_data = {
'op': 'delete-member',
'id': member_id,
}
r = requests.patch(
f'{ROOT_API_LINK}{network_url}',
data=json.dumps(member_data),
headers=custom_headers
)
print(r.status_code)
def add_user_share(network_url: str, email_address: str, permissions: str):
"""
Parameters
----------
network_url: retrieved from GET /api/networks
email_address: email address of the user one wants to give access to (must be an active Husarnet account)
permissions: currently two options: member/admin
"""
share_data = {
'op': 'add-share',
'email': email_address,
'permissions': permissions,
}
r = requests.patch(
f'{ROOT_API_LINK}{network_url}',
data=json.dumps(share_data),
headers=custom_headers
)
print(r.status_code)
def remove_user_share(network_url, email_address):
remove_share_data = {
'op': 'remove-share',
'email': email_address,
}
r = requests.patch(
f'{ROOT_API_LINK}{network_url}',
data=json.dumps(remove_share_data),
headers=custom_headers
)
# DEMO
# STEP 1 - basic network operations
my_networks = all_networks()
create_new_network('my-new-net-name')
my_networks = all_networks()['networks']
# STEP 2 - network details
edited_network = my_networks[0]
edited_network_details = get_network(url=edited_network['url'])
pprint(edited_network_details)
# delete the edited network
delete_network(url=edited_network['url'])
# STEP 3 - modify network members
# websetup
websetup_link = 'https://app.staging.husarnet.com//husarnet/fc94...'
add_member_websetup(
network_url=edited_network['url'],
websetup_url=websetup_link,
name='my-device-name',
is_managed=False
)
# Worth noting: it is possible to add an existing device to a network.
# In that case, the only parameter required is "device_id".
# See documentation for more details.
edited_network_details = get_network(url=edited_network['url'])
pprint(edited_network_details) # new device appears in members
member_id = edited_network_details['members'][0]['id']
delete_member(edited_network['url'], member_id)
# see updated details - old member disappears
edited_network_details = get_network(url=edited_network['url'])
pprint(edited_network_details)
# STEP 4 - share the network with other users
user_email = 'user@example.com'
add_user_share(edited_network['url'], user_email, 'member')
edited_network_details = get_network(url=edited_network['url'])
pprint(edited_network_details)
remove_user_share(edited_network['url'], user_email) # remove the added share