Recombee Docs
Visit recombee.comStart Free
docs20User Documentation
adminuiAdmin UI
reql32ReQL
codeAPI Clients & Integrations
suitcaseMisc

.NET API Client

Example

.NET
using System;
using System.Collections.Generic;
using System.Linq;
using Recombee.ApiClient;
using Recombee.ApiClient.ApiRequests;
using Recombee.ApiClient.Bindings;
using Recombee.ApiClient.Util;

public class BasicExample
{
    static int Main(string[] args)
    {
        RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--", region: Region.UsWest);

        try
        {
            const int NUM = 100;
            var userIds = Enumerable.Range(0, NUM).Select(i => String.Format("user-{0}", i));
            var itemIds = Enumerable.Range(0, NUM).Select(i => String.Format("item-{0}", i));
            // Generate some random purchases of items by users
            const double PROBABILITY_PURCHASED = 0.1;
            Random r = new Random();
            var purchases = new List<Request>();

            foreach(var userId in userIds) {
                purchases.AddRange(
                    itemIds.Where(_ => r.NextDouble() < PROBABILITY_PURCHASED)
                          .Select(itemId =>
                                    new AddPurchase(userId, itemId, cascadeCreate: true) // Use cascadeCreate parameter to create
                          )                                                             // the yet non-existing users and items
                );
            }

            Console.WriteLine("Send purchases");
            client.Send(new Batch(purchases)); //Use Batch for faster processing of larger data

            // Recommend 5 items for user 'user-25'
            RecommendationResponse recommendationResponse = client.Send(new RecommendItemsToUser("user-25", 5));
            Console.WriteLine("Recommended items:");
            foreach(Recommendation rec in recommendationResponse.Recomms) Console.WriteLine(rec.Id);

            // User scrolled down - get next 3 recommended items
            recommendationResponse = client.Send(new RecommendNextItems(recommendationResponse.RecommId, 3));
            Console.WriteLine("Next recommended items:");
            foreach(Recommendation rec in recommendationResponse.Recomms) Console.WriteLine(rec.Id);

        }
        catch(ApiException e)
        {
        Console.WriteLine(e.ToString());
        // Use fallback
        }

        return 0;
    }
}
© Copyright 2024, Recombee s.r.o
docs.recombee.com