Node.js API ClientΒΆ

var recombee = require('recombee-api-client');
var rqs = recombee.requests;

var client = new recombee.ApiClient('...your-db...', secretToken);

// Prepare some userIDs and itemIDs
const NUM = 100;
var userIds = Array.apply(0, Array(NUM)).map((_, i) => {
  return `user-${i}`;
});

var itemIds = Array.apply(0, Array(NUM)).map((_, i) => {
  return `item-${i}`;
});


// Generate some random purchases of items by users
const PROBABILITY_PURCHASED = 0.1;
var purchases = [];
userIds.forEach((userId) => {
  var purchased = itemIds.filter(() => Math.random() < PROBABILITY_PURCHASED);
  purchased.forEach((itemId) => {

    purchases.push(new rqs.AddPurchase(userId, itemId, {'cascadeCreate': true}))

  });
});

// Send the data to Recombee, use Batch for faster processing of larger data
client.send(new rqs.Batch(purchases))
  .then(() => {
    //Get 5 recommended items for user 'user-25'
    client.send(new rqs.UserBasedRecommendation('user-25', 5))
      .then((recommended) => {
        console.log(`Recommended items for user-25: ${recommended}`);
      });
  })
  .catch((error) => {
    console.error(error);
    // Use fallback
  });