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--', '--db-private-token--', {:region => 'us-west'})

# 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'
  response = client.send('user-25', 5))
  puts "Recommended items for user-25: #{response}"

  # User scrolled down - get next 3 recommended items
  response = client.send(['recommId'], 3))
  puts "Next recommended items for user-25: #{response}"

rescue APIError => e
  puts e
  # Use fallback

Using property values

require 'recombee_api_client'
include RecombeeApiClient

NUM = 100

client = RecombeeClient('--my-database-id--', '--db-private-token--', {:region => 'ap-se'})
client.send( # Clear everything from the database (asynchronous)

# 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:
        :cascade_create => true  # Use cascade_create 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 cascade_create to create unexisting users
  user_ids.each { |user_id| requests.push(, item_id, :cascade_create => true)) }

# Send purchases to the recommender system

# Get 5 recommendations for user-42, who is currently viewing computer-6
# 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 that are 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}"

# Filters, boosters and other settings can be also set in the Admin UI (
# when scenario is specified
recommended = client.send('computer-6', 'user-42', 5, {:scenario => 'product_detail'})

# Perform personalized full-text search with a user's search query (e.g. 'computers').
matches = client.send('user-42', 'computers', 5, {:scenario => 'search_top'})
puts "Matched items: #{matches}"

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.

Example: “`ruby

begin recommended = client.send('computer-6', 'user-42', 5, {'filter' => “'price' > context_item”}) ) 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 end “`