Recapi 0.1.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package Recapi --version 0.1.9
                    
NuGet\Install-Package Recapi -Version 0.1.9
                    
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="Recapi" Version="0.1.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Recapi" Version="0.1.9" />
                    
Directory.Packages.props
<PackageReference Include="Recapi" />
                    
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 Recapi --version 0.1.9
                    
#r "nuget: Recapi, 0.1.9"
                    
#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 Recapi@0.1.9
                    
#: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=Recapi&version=0.1.9
                    
Install as a Cake Addin
#tool nuget:?package=Recapi&version=0.1.9
                    
Install as a Cake Tool

recapi for .NET

C# client for the recapi recommendation API. .NET 8 or newer, standard library only (HttpClient, System.Text.Json), no NuGet dependency.

Like the Python and Go libraries, it keeps each user's decayed history (docs/history-spec.md) and sends it with every recommendation request, sends events in the background (within 2 s, a full batch of 1,000 at once) with retries, falls back to popular items when recapi is slow or failing, and attributes events on recommended items to the recommendation that showed them (the dashboard's clicks and conversions). It passes the shared vectors in spec/.

Apps published trimmed (PublishTrimmed) or as Native AOT (PublishAot) are supported, with no IL warning from the library: checked on .NET 8, .NET 10 and .NET 11.

Install

The library is the NuGet package Recapi (production releases publish it: docs/deployment.md):

dotnet add package Recapi

Without access to NuGet, the API serves the library's project (Recapi/: the .csproj and its .cs files, no tests) as a zip: unzip it next to your project and reference it. The dashboard's quick start shows the URL with your address.

curl -fLo recapi-csharp.zip https://api.<your-domain>/sdk/csharp/recapi-csharp-0.1.9.zip
unzip -o recapi-csharp.zip -d vendor
dotnet add reference vendor/Recapi/Recapi.csproj

In ASP.NET Core, register one client for the process; the container disposes it at shutdown, which sends the events still queued:

builder.Services.AddSingleton(_ => new RecapiClient("rk_sec_…", "https://api.example.com"));

Quick start

using Recapi;

// A secret key (rk_sec_…): it stays on your server. One client per process
// (a singleton in dependency injection); dispose it at exit.
await using var client = new RecapiClient("rk_sec_…", "https://api.example.com");

// Levels: Levels.View, Cart, Purchase, Reject, or any integer from -10 to 10.
// Queued: the background sender sends it within 2 s.
await client.TrackAsync(new RecapiEvent { ItemId = "sku-42", Level = Levels.View, UserId = "cust-881" });

var recs = await client.RecommendAsync(new RecommendRequest { UserId = "cust-881", Count = 4 });
foreach (var item in recs.Items)
{
    // Metadata: the item's stored object, or null. Why: why it is listed
    // ("for_you", "similar", "bought_together"...; docs/api.md "Reason codes"):
    // map the codes you want to a label, show none for one you don't map.
    Console.WriteLine($"{item.ItemId} {item.Metadata?["name"]} {item.Why}");
}

Calls

Each takes an optional CancellationToken. A refused argument (an invalid id or level) throws ArgumentException, except an ItemId or ContextItemId the API would refuse, as a page often takes those from a visitor's URL (see the table); an API failure a RecapiException (Code, Status, RetryAfter, Retryable, Outage), but for those RecommendAsync and PopularAsync answer with a list.

Call What it does
TrackAsync(RecapiEvent) Records an interaction: Level -10 to 10 (Levels.View 3, Cart 5, Purchase 10, Reject -10; Levels.Parse("view") reads a name). The history changes at once; the event is queued, and a background sender sends the queue every FlushInterval (2 s), a full batch at once. After a failed send it waits twice as long each time, up to a minute. An ItemId the API would refuse (1-256 bytes of UTF-8, no control character) drops the event with a log line: no exception, and the other events are sent as usual.
FlushAsync() Sends the queued events now, 1,000 per request (the end of a job or a serverless handler). A network error, 429 or 5xx is retried 3 times (0.5 s, 1 s, 2 s with jitter, or the Retry-After, at most 8 s), then the events stay queued; a batch over the API's body limit (413) is sent in halves; any other error drops them (logged).
DisposeAsync() Stops the sender and sends what is queued, one try per batch, so a deploy never waits on an outage. An event tracked afterwards is sent at once, one try.
RecommendAsync(RecommendRequest) A list for UserId or AnonId, with Count, Placement, TagsAny, TagsAll, Exclude, ContextItemId, Discovery. At most 1.5 s; on a network error, timeout, 5xx or 429 it returns popular items (Fallback, no id), or no item when those fail too; on a 401 or 403 no item at once. A ContextItemId the API would refuse isn't sent: you get the same popular list and a log line, with no recommendation request. Any other failure (a bad request: 400, 404, 413, other 3xx and 4xx; a reply that isn't the API's) throws.
IdentifyAsync(anonId, userId) At login: the visitor's history and shown items become the user's. The user's first restore from recapi then leaves that visitor's browsing out, so it doesn't count twice.
PopularAsync(PopularRequest) Popular items, for pages without a user. On a network error, timeout, 5xx, 429, 401 or 403: an empty list and a log line. Other failures throw as in RecommendAsync.
UpsertItemsAsync(CatalogItem[][, merge]) Creates or replaces items (ItemId, Title, Tags, Metadata…), 1,000 per request, retried like events; returns how many were stored. With merge: true, each item changes only the fields it sets and the others keep their stored values (a tags-only sync keeps the title); to clear a field, replace the item. A bad item doesn't keep the others out: once every request is sent, a RejectedItemsException (a RecapiException, code invalid_request) gives Upserted and Rejected, each refused item's Index in your list and Error.
DeleteUserAsync(userId) Erases a user's events in recapi and their state in the store, retried like events.

new RecapiOptions { Store, Timeout, FlushInterval, MaxBatch, Log } sets the options. The default MemoryStore keeps the 10,000 most recent users in the process; implement IRecapiStore (GetAsync, SetAsync, DeleteAsync) over Redis or a database table when several processes serve the same users, and have it expire entries about 30 days after their last write, or a Redis keeps them for ever. It stores the same JSON as the Python and Go libraries, so they can share one store. Failures the client handles itself go to Log. Without one they go to System.Diagnostics.Trace, which ASP.NET Core doesn't show, so events lost for good (a refused batch, rejected events, a full queue, an unusable item id) and the failures RecommendAsync and PopularAsync answer with a list also print one line on standard error, where they show in the app's normal output: at most one a minute for each error code (events and lists apart), for rejected events and for unusable item ids, so a revoked key on a busy server doesn't flood the log. A Log you give gets every line. Event sends that can still succeed print nothing.

Development

./test.sh [filter] builds the library and runs its tests (make test-sdk does), against a fake API on a raw socket; make lint checks the format with dotnet format, and the build treats warnings as errors. It also packs the NuGet package (Recapi/bin/Release/Recapi.<version>.nupkg, metadata in Recapi/Directory.Build.props), which PackageTests checks. scripts/dotnet.sh uses a local .NET 8+ SDK, else fetches Microsoft's .NET 8 SDK once into ~/.cache/recapi. The tests use their own runner (Recapi.Tests/Runner.cs): every public static Test* method runs, so no NuGet download is needed.

RECAPI_KEY=rk_sec_… ../../scripts/dotnet.sh run --project Sample [item ids] runs Sample/Program.cs against staging (RECAPI_URL picks another API): it tracks views of a new visitor, then prints their recommendations and the popular items. ./test.sh builds it.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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.
  • net8.0

    • No dependencies.

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.1.17 0 12/4/2026
0.1.16 51 12/2/2026
0.1.15 103 11/24/2026
0.1.14 95 11/22/2026
0.1.13 96 11/22/2026
0.1.12 102 11/19/2026
0.1.11 99 11/19/2026
0.1.10 107 11/15/2026
0.1.9 100 11/14/2026
0.1.8 102 11/7/2026
0.1.7 97 11/7/2026
0.1.6 94 11/5/2026
0.1.5 94 11/4/2026
0.1.4 94 11/4/2026
0.1.3 99 11/4/2026
0.1.2 103 11/4/2026
0.1.1 103 11/2/2026
0.1.0 100 11/2/2026