Ruby client for easy use of the Recombee recommendation API
Switch branches/tags
Clone or download


A Ruby client for easy use of the Recombee recommendation API.

If you don't have an account at Recombee yet, you can create a free account here.

Documentation of the API can be found at


Add this line to your application's Gemfile:

gem 'recombee_api_client'

And then execute:

$ bundle

Or install it yourself as:

$ gem install recombee_api_client


Basic example

require 'recombee_api_client'
include RecombeeApiClient

client = RecombeeClient('--my-database-id--', '--my-secret-token--')

# Generate some random purchases of items by users
NUM = 100

users = (1..NUM).map { |i| "user-#{i}" }
items = (1..NUM).map { |i| "item-#{i}" }
purchases = []

users.each do |user_id|
  purchased = { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED }
  purchased.each { |item_id| purchases.push(

      , item_id,'cascadeCreate' => true)
                                                  # Use cascadeCreate to create the
                                                  # yet non-existing users and items


  # Send the data to Recombee, use Batch for faster processing of larger data
  # Get recommendations for user 'user-25'
  recommended = client.send('user-25', 5))
  puts "Recommended items for user-25: #{recommended}"
rescue APIError => e
  puts e
  # Use fallback

Using property values

require 'recombee_api_client'
include RecombeeApiClient

NUM = 100

client = RecombeeClient('--my-database-id--', '--my-secret-token--')
client.send( # Clear everything from the database

# We will use computers as items in this example
# Computers have five properties 
#   - price (floating point number)
#   - number of processor cores (integer number)
#   - description (string)
#   - date from which it is in stock (timestamp)
#   - image (url of computer's photo)

# Add properties of items
client.send('price', 'double'))
client.send('num-cores', 'int'))
client.send('description', 'string'))
client.send('in_stock_from', 'timestamp'))
client.send('image', 'image'))

# Prepare requests for setting a catalog of computers
requests = (1..NUM).map do |i|
      "computer-#{i}", #itemId
        'price' => rand(15000.0 .. 25000.0),
        'num-cores' => rand(1..8),
        'description' => 'Great computer',
        'in_stock_from' =>,
        'image' => "{i}.jpg"
      #optional parameters:
        'cascadeCreate' => true  # Use cascadeCreate for creating item
                                 # with given itemId, if it doesn't exist

# Send catalog to the recommender system
puts client.send(

# Prepare some purchases of items by users
requests = []
(1..NUM).map{|i| "computer-#{i}"}.each do |item_id|
  user_ids = (1..NUM).map{|i| "user-#{i}"}
  user_ids = { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED }
  # Use cascadeCreate to create unexisting users
  user_ids.each { |user_id| requests.push(, item_id, 'cascadeCreate' => true)) }

# Send purchases to the recommender system

# Get 5 recommendations for user-42, who is currently viewing computer-6
recommended = client.send('computer-6', 'user-42', 5) )
puts "Recommended items: #{recommended}"

# Recommend only computers that have at least 3 cores
recommended = client.send('computer-6', 'user-42', 5, {'filter' => "'num-cores'>=3"})
puts "Recommended items with at least 3 processor cores: #{recommended}"

# Recommend only items thatare more expensive then currently viewed item (up-sell)
recommended = client.send('computer-6', 'user-42', 5,
    {'filter' => "'price' > context_item[\"price\"]"})
puts "Recommended up-sell items: #{recommended}"

Exception handling

For the sake of brevity, the above examples omit exception handling. However, various exceptions can occur while processing request, for example because of adding an already existing item, submitting interaction of nonexistent user or because of timeout.

We are doing our best to provide the fastest and most reliable service, but production-level applications must implement a fallback solution since errors can always happen. The fallback might be, for example, showing the most popular items from the current category, or not displaying recommendations at all.


  recommended = client.send('computer-6', 'user-42', 5,
    {'filter' => "'price' > context_item[\"price\"]"})
rescue ResponseError => e
  # Handle errorneous request => use fallback
rescue ApiTimeout => e
  # Handle timeout => use fallback
rescue APIError => e
  # APIError is parent of both ResponseError and ApiTimeout