Skip to content

adamreed90/Unifi.NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Unifi.NET

A comprehensive collection of .NET SDKs for interacting with Ubiquiti UniFi APIs and services. This solution provides strongly-typed, async-first client libraries for UniFi's ecosystem, with built-in resilience and dependency injection support.

NuGet License .NET

πŸ“¦ Available Packages

Package Description Status NuGet
Unifi.NET.Access SDK for UniFi Access API - Door access control, user management, NFC/PIN credentials 🚧 In Development -
Unifi.NET.Network SDK for UniFi Network Controller API - Network device management, configuration, statistics πŸ“‹ Planned -
Unifi.NET.Protect SDK for UniFi Protect API - Video surveillance, camera management, event detection πŸ“‹ Planned -
Unifi.NET.SiteManager SDK for UniFi Site Manager API - Multi-site management, centralized control πŸ“‹ Planned -

πŸš€ Features

  • Native AOT Compatible - Full support for Native AOT compilation for optimal performance and size
  • Strongly Typed Models - Full request/response models for all API endpoints
  • Async/Await Support - Built for modern async programming patterns
  • Dependency Injection - Native integration with Microsoft.Extensions.DependencyInjection
  • Resilience & Retry Logic - Built-in Polly policies for transient fault handling
  • RestSharp Based - Leveraging RestSharp for robust HTTP client functionality
  • API Token Authentication - Secure authentication using UniFi API tokens
  • Comprehensive Error Handling - Detailed exception types and error codes
  • IntelliSense Support - Full XML documentation for all public APIs
  • Trimming Safe - Optimized for trimming to reduce application size

πŸ“‹ Prerequisites

  • .NET 9.0 or later
  • UniFi Console with the respective service installed
  • API Token from UniFi Portal (for authentication)
  • Network access to your UniFi Console (default port: 12445 for Access)

Native AOT Requirements

  • .NET 9 SDK for building
  • Target platform runtime identifier (e.g., linux-x64, win-x64, osx-arm64)
  • No additional runtime dependencies needed for deployment

πŸ”§ Installation

Install the SDK for the UniFi service you need via NuGet:

# For UniFi Access
dotnet add package Unifi.NET.Access

# For UniFi Network
dotnet add package Unifi.NET.Network

# For UniFi Protect
dotnet add package Unifi.NET.Protect

# For UniFi Site Manager
dotnet add package Unifi.NET.SiteManager

🎯 Quick Start

Configuration with Dependency Injection

using Microsoft.Extensions.DependencyInjection;
using Unifi.NET.Access;

var services = new ServiceCollection();

// Configure UniFi SDKs
services.AddUnifi(options =>
{
    options.BaseUrl = "https://your-console-ip:12445";
    options.ApiToken = "your-api-token-here";
    options.ValidateSsl = false; // Set to true in production with valid certificates
})
.AddAccess() // Add UniFi Access SDK
.AddNetwork() // Add UniFi Network SDK
.AddProtect(); // Add UniFi Protect SDK

var serviceProvider = services.BuildServiceProvider();

Basic Usage Example

// Resolve the Access client from DI
var accessClient = serviceProvider.GetRequiredService<IUnifiAccessClient>();

// Get all users
var users = await accessClient.Users.GetAllAsync();

// Get a specific door
var door = await accessClient.Doors.GetAsync("door-id");

// Unlock a door remotely
await accessClient.Doors.UnlockAsync("door-id");

// Create a new user
var newUser = await accessClient.Users.CreateAsync(new CreateUserRequest
{
    FirstName = "John",
    LastName = "Doe",
    Email = "john.doe@example.com",
    EmployeeNumber = "EMP001"
});

// Assign an access policy to a user
await accessClient.Users.AssignAccessPolicyAsync(newUser.Id, "policy-id");

Without Dependency Injection

using Unifi.NET.Access;

var config = new UnifiConfiguration
{
    BaseUrl = "https://your-console-ip:12445",
    ApiToken = "your-api-token-here"
};

var accessClient = new UnifiAccessClient(config);

// Use the client
var users = await accessClient.Users.GetAllAsync();

Native AOT Deployment

The SDKs are designed for full Native AOT compatibility in .NET 9:

<!-- In your project file -->
<PropertyGroup>
    <PublishAot>true</PublishAot>
    <JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
    <IsAotCompatible>true</IsAotCompatible>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
    <OptimizationPreference>Speed</OptimizationPreference>
</PropertyGroup>

The SDK uses ASP.NET Core-style JSON serialization patterns for optimal Native AOT performance:

// Internal implementation uses service-specific JsonSerializerContext classes
// Combined with TypeInfoResolver for runtime type resolution
// Following Microsoft's recommended patterns for Native AOT applications

// All JSON serialization is handled internally - no configuration needed!
services.AddUnifi(options =>
{
    options.BaseUrl = "https://your-console-ip:12445";
    options.ApiToken = "your-api-token-here";
})
.AddAccess(); // Serialization is automatically configured for AOT
# Publish with Native AOT
dotnet publish -c Release -r linux-x64

# Verify AOT compatibility (produces zero warnings)
dotnet publish -c Release -r linux-x64 --verbosity detailed | grep -i warning

πŸ”‘ Authentication

All UniFi SDKs use API Token authentication. To obtain an API token:

  1. Sign in to your UniFi Portal
  2. Select the UniFi Console where the service is installed
  3. Navigate to the respective service settings:
    • Access: Access > Settings > General > Advanced > API Token
    • Network: Settings > System > Advanced > API
    • Protect: Settings > Advanced > API
  4. Create a new API token with appropriate permissions
  5. Copy and securely store the token (it's only shown once)

πŸ“Š Supported UniFi Versions

Service Minimum Version Recommended Version
UniFi Access 1.9.1 Latest
UniFi Network Controller 7.0.0 Latest
UniFi Protect 2.0.0 Latest
UniFi Site Manager 1.0.0 Latest

Note: API availability may vary based on your UniFi service version and license type. Identity Enterprise users should check API availability for their specific configuration.

πŸ—οΈ Architecture

The solution follows Clean Architecture principles with a modular design:

Unifi.NET/
β”œβ”€β”€ Unifi.NET.Access/           # UniFi Access SDK
β”‚   β”œβ”€β”€ Models/                 # Request/Response DTOs
β”‚   β”œβ”€β”€ Services/               # API service implementations
β”‚   β”œβ”€β”€ Extensions/             # DI extensions
β”‚   └── Exceptions/             # Custom exceptions
β”œβ”€β”€ Unifi.NET.Network/          # UniFi Network SDK
β”œβ”€β”€ Unifi.NET.Protect/          # UniFi Protect SDK
β”œβ”€β”€ Unifi.NET.SiteManager/      # UniFi Site Manager SDK
└── Unifi.NET.Common/           # Shared utilities (planned)

Native AOT Design Principles

All SDKs follow .NET 9 Native AOT best practices:

βœ… Fully Compatible Components

  • System.Text.Json with source generators (no reflection)
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Http with IHttpClientFactory
  • Polly v8+ for resilience (via Microsoft.Extensions.Http.Resilience)

🚫 Avoided Patterns

  • No runtime reflection for serialization
  • No dynamic code generation (System.Reflection.Emit)
  • No runtime type discovery
  • No dynamic assembly loading

🎯 Implementation Strategy

  • Service-specific JsonSerializerContext classes for organized type registration
  • JsonTypeInfoResolver.Combine() pattern for merging contexts (ASP.NET Core style)
  • JsonTypeInfo<T> and GetTypeInfo() for runtime type resolution
  • All models use [JsonSerializable] source generation
  • Configuration uses source-generated binding
  • RestSharp configured with System.Text.Json and AOT-safe serializers
  • Zero AOT warnings policy enforced

πŸ§ͺ Testing

Each SDK includes comprehensive unit and integration tests:

# Run all tests
dotnet test

# Run tests for a specific SDK
dotnet test Unifi.NET.Access.Tests

# Run with coverage
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

πŸ“ˆ Versioning

Package versions follow the UniFi API version they wrap, with an additional revision suffix for SDK updates:

  • 3.3.21 - Matches UniFi Access API v3.3.21, first SDK release
  • 3.3.21.1 - Same API version, SDK bug fixes or improvements
  • 3.4.0 - New UniFi Access API version

🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details on:

  • Code style and standards
  • Pull request process
  • Bug reporting
  • Feature requests
  • Development setup

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

πŸ—ΊοΈ Roadmap

Phase 1: UniFi Access SDK (In Progress - 37.5% Complete)

  • Project structure setup
  • Native AOT-compatible HTTP client with RestSharp
  • System.Text.Json source generators with ASP.NET Core patterns
  • User management endpoints (10/10 core endpoints)
  • User group management (10/10 endpoints)
  • NFC card management (9/9 endpoints)
  • PIN code management (3/3 endpoints)
  • Door and access control endpoints (7/7 endpoints)
  • Access policy management (5/5 endpoints)
  • Device management (1/3 endpoints)
  • Comprehensive sample application
  • Integration tested with production UniFi Access
  • Remaining endpoints (75/120 to implement)
  • Webhook/WebSocket support for real-time events
  • Complete unit test coverage
  • NuGet package publication

Phase 2: UniFi Network SDK

  • Device management
  • Network configuration
  • Statistics and monitoring
  • Guest management

Phase 3: UniFi Protect SDK

  • Camera management
  • Recording and playback
  • Event detection
  • Live streaming support

Phase 4: UniFi Site Manager SDK

  • Multi-site management
  • Centralized configuration
  • Cross-site operations

πŸ™ Acknowledgments

  • Ubiquiti Networks for creating the UniFi ecosystem
  • The .NET community for continuous support and feedback
  • Contributors and users of this project

⚠️ Disclaimer

This is an unofficial SDK and is not affiliated with, officially maintained, or endorsed by Ubiquiti Networks. Use at your own risk.


Note: This project is currently in active development. APIs may change before the first stable release. We recommend using preview versions with caution in production environments.

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published