DirectusSharp 0.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package DirectusSharp --version 0.0.7
                    
NuGet\Install-Package DirectusSharp -Version 0.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DirectusSharp" Version="0.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DirectusSharp" Version="0.0.7" />
                    
Directory.Packages.props
<PackageReference Include="DirectusSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DirectusSharp --version 0.0.7
                    
#r "nuget: DirectusSharp, 0.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DirectusSharp@0.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DirectusSharp&version=0.0.7
                    
Install as a Cake Addin
#tool nuget:?package=DirectusSharp&version=0.0.7
                    
Install as a Cake Tool

DirectusSharp

An unofficial .NET wrapper for Directus.

It's not fully complete as I add stuff to it once I need it. Contributions are welcome.

Installation

NuGet latest release

You can add this library to your project using NuGet.

Package Manager Console Run the following command in the “Package Manager Console”:

PM> Install-Package DirectusSharp

Visual Studio Right click to your project in Visual Studio, choose “Manage NuGet Packages” and search for ‘DirectusSharp’ and click ‘Install’.

.NET Core Command Line Interface Run the following command from your favorite shell or terminal:

dotnet add package DirectusSharp

Questions?

Feel free to send me an email staylluc@gmail.com or create an issue with your question.

Examples

Creating a client

There are two ways of creating a client. Both need an HttpClient with a preset BaseUri.

Unauthenticated
var httpClient = new HttpClient() {
    BaseUri = new Uri("https://directus.io")
};

var client = DirectusClient.Create(httpClient);
Authenticated

There are multiple ways to do authentication in Directus. You can read more about it in the official documentation.

Static Token
DirectusClient.Create(new StaticIdentity() {
    Token = "STATIC_TOKEN"
}, ...);
TemporaryIdentity

Temporary tokens must be refreshed manually (DirectusSharp will not automatically refresh tokens for you)

DirectusClient.Create(new TemporaryIdentity() {
    AccessToken = ...,
    RefreshToken = ...
}, ...);

You can obtain a TemporaryIdentity through the LoginRequest.

var response = client.ExecuteAsync(new LoginRequest() {
    Email = ...,
    Password = ...
});

if (!response.IsSuccess) { /* Handle Login Failure */ }

client.Identity = response.Data;

Collection examples

To Query, Create or Modify our collection, we need to create the following classes associated to it. In this example I'll use a collection called Pets.

Boilerplate

class Pet {
    public int Id { get; set; } = 0; // Directus will auto-increment it on insert
    public string Name { get; set; }
    public int Age { get; set; }
}

class GetPetRequest : GetItemRequest<Pet> {
    public required int Id { get; init; }
    
    protected override string GetCollection() => "pets";
    protected override string GetItemId() => Id.ToString();
}

class CreatePetRequest : CreateItemRequest<Pet> {
    public required Pet Pet { get; init; }
    
    protected override string GetCollection() => "pets";
    protected override Pet GetItem() => Pet;
}

class UpdatePetRequest : UpdateItemRequest<Pet> {
    public required Pet Pet { get; init; }
    
    protected override string GetCollection() => "pets";
    protected override Pet GetItem() => Pet;
    protected override string GetItemId() => Pet.Id.ToString();
}
Querying an Item
var response = await client.ExecuteAsync(new GetPetRequest() {
    Id = 0
});

if (response.IsSuccess) {
    Console.WriteLine($"Pet name: {response.Data.Name}");
}
// 
Creating an Item
var response = await client.ExecuteAsync(new CreatePetRequest() {
    Pet = new Pet() {
        Name = "Rudolf",
        Age = 3   
    }
});

if (response.IsSuccess) {
    Console.WriteLine($"Created pet with id: {response.Data.Id}");
}
Updating an Item

It's practically the same thing as CreatePetRequest but with UpdatePetRequest

var response = await client.ExecuteAsync(new UpdatePetRequest() {
    Pet = ...
};

License

DirectusSharp is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.13 159 1/25/2025
0.0.11 153 1/21/2025
0.0.10 150 1/21/2025
0.0.7 129 1/20/2025
0.0.6 143 1/20/2025
0.0.5 135 1/20/2025
0.0.3 141 1/20/2025