Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple Python client for the Ubiquiti UDM Pro
import requests
import json
import urllib3
UNIFI_LOGIN_PATH = '/api/auth/login'
# Disable SSL verification warnings
class Unifi(object):
def __init__(self, host, username, password): = host
self.username = username
self.password = password
self.session = requests.Session()
self.csrf = ""
def login(self):
payload = {
"username": self.username,
"password": self.password,
r = self.request(UNIFI_LOGIN_PATH, payload)
return r.ok
def request(self, path, data={}, method='POST'):
uri = 'https://{}{}'.format(, path)
headers = {
"Accept": "application/json",
"Content-Type": "application/json; charset=utf-8",
if self.csrf:
headers["X-CSRF-Token"] = self.csrf
r = getattr(self.session, method.lower())(uri, json=data, verify=False, headers=headers)
self.csrf = r.headers['X-CSRF-Token']
except KeyError:
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment