Skip to content

VMelnalksnis/PaperlessDotNet

Repository files navigation

Nuget Nuget Codecov Run tests

PaperlessDotNet

.NET client for the Paperless-ngx API.

Usage

A separate NuGet package is provided for ASP.NET Core (IConfiguration and IServiceCollection) . For use outside of ASP.NET Core, see the example in tests.

  1. Add configuration (see options)

    "Paperless": {
        "BaseAddress": "",
        "Token": ""
    }
  2. Register required services (see tests)

    serviceCollection
        .AddSingleton<IClock>(SystemClock.Instance)
        .AddSingleton(DateTimeZoneProviders.Tzdb)
        .AddPaperlessDotNet();
  3. Use IPaperlessClient to access all endpoints, or one of the specific clients defined in IPaperlessClient

Custom fields

Paperless supports adding custom fields to documents. In order to use custom fields, first define a class with a property for each field:

internal sealed class CustomFields
{
    public string? Field1 { get; set; }

    public Uri? Field2 { get; set; }

    public LocalDate? Field3 { get; set; }

    public bool? Field4 { get; set; }

    public int? Field5 { get; set; }

    public float? Field6 { get; set; }

    public float? Field7 { get; set; }

    public int[]? Field8 { get; set; }
}

Then create a JsonSerializerContext with all the API models that use custom fields:

[JsonSerializable(typeof(PaginatedList<Document<CustomFields>>))]
[JsonSerializable(typeof(DocumentUpdate<CustomFields>))]
internal sealed partial class SerializerContext : JsonSerializerContext;

And configure JSON serialization for the client:

serviceCollection.AddPaperlessDotNet(
    configuration,
    options =>
    {
        options.Options.Converters.Add(new CustomFieldsConverter<CustomFields>(options));
        options.Options.TypeInfoResolverChain.Add(SerializerContext.Default);
    });

For a working example see unit tests and integration tests.