unifi-client
UniFiClient is a third party Ubiquiti UniFi API client, allowing you to easily build your own UniFi integrations in Rust. UniFiClient comes with two primary sets of APIs for communicating with UniFi controllers, a high level strongly typed semantic API, and a lower level HTTP API for extending behaviour.
Note: This crate is not officially associated with or endorsed by Ubiquiti Inc.
Features
- π Secure authentication with UniFi controllers
- π Access via a convenient singleton or multiple independent clients
- π Complete guest management: authorize, list, and unauthorize guests.
- π Async API with Tokio runtime support
- π‘οΈ Comprehensive error handling
- π§ͺ Well-tested (unit and integration tests)
- π§© Extensible architecture, ready for additional API endpoints
Installation
Run this command in your terminal to add the latest version of UniFiClient.
Quick Start
The easiest way to use unifi-client is with the singleton pattern. This provides a single, globally accessible client instance.
use ;
async
API Overview
Semantic API
The semantic API is a high level API that provides a type-safe way to
interact with the UniFi controller. It is built around a set of models
that map to the UniFi controller's API. Currently the following modules are
available.
guest- Guest access management.
Authorizing a guest
unifi-client uses a builder pattern for constructing API requests.
All methods with multiple optional parameters are built as Builder structs, allowing you to easily specify parameters.
use ;
async
Error Handling
All API methods return a Result<T, UniFiError>.
use ;
async
Advanced Usage
Builder vs. Singleton: Choosing the Right Approach
UniFiClient supports two patterns for client creation and access:
-
Builder (
UniFiClient::builder()):
Create new, independent client instances with custom configurations.
Use when:- Connecting to multiple controllers in a single application.
- Writing tests (e.g., with wiremock) or needing isolated configurations.
- Spinning up short-lived clients for specific tasks.
-
Singleton (
initialize()/instance()):
Set up a global client for simplified, centralized access.
Use when:- Your app interacts with a single UniFi controller.
- You want to avoid passing client instances around.
- A uniform configuration is needed throughout your codebase.
Best Practices:
- Single Controller: Call
initialize()early (typically inmain) and useinstance()anywhere else. - Multiple or Custom Instances: Use the builder to create each client independently.
- If
initialize()isnβt called, any singleton usage should gracefully return a configuration error.
Creating Multiple Clients
If you need to connect to multiple UniFi Controllers, or you need different client configurations
within the same application, create independent client instances using UniFiClient::builder()
without calling initialize():
use ;
async
Custom HTTP Client
use ;
use Client as ReqwestClient;
async
Planned Features
- Statistics and reporting
Compatibility
This library has been tested with:
- UniFi Controller version 9.x
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT license.
Acknowledgements
- This library is inspired by various UniFi API clients in other languages
- Thanks to the Ubiquiti community for documenting the unofficial API