API Reference / API Methods / The Ruby API client

Algolia’s Ruby API client lets you use Algolia’s APIs from your Ruby application. Compared to using the APIs directly, using the Ruby API client has these benefits:

  • Network retry strategy. The API client automatically retries connecting to another Algolia server, if the connection fails. Thanks to that, using the API clients is covered by Algolia’s SLA.

  • Reindexing. The API clients let you reindex your records in a single operation.

  • Automatic batching. When you add records to your index, the API client automatically batches your records to make fewer API requests.

Algolia’s Ruby API client is open source. You can find the source code on GitHub.

Do you want to integrate Algolia into your Ruby on Rails app? Use Algolia’s Rails integration to get started even quicker.

You’re reading the documentation for version 2 of the Ruby API client. Version 1 of the API client is no longer supported. If you want to update to version 2, see the upgrade guide. You can still access the version 1 documentation.

Quickstart sample app

To download and run a self-contained example, see the Ruby quickstart on GitHub.

Install the Ruby API client

The Algolia Ruby API client requires Ruby version 2.2 or higher.

Install the Ruby API client using RubyGems:

1
gem install algolia

If you’re using bundler, add algolia as a dependency:

1
bundle add algolia

Be sure to install algolia and not algoliasearch which is the old version of the API client. See the update guide for more information.

Test your installation

If you haven’t already, create an Algolia account and create a new Algolia app to get started.

To test whether you can connect to Algolia, run a simple program that adds a record to a new index, searches the index, and print the results.

  1. Copy the following code sample into a text editor. If you’re signed in, the code samples below show your Algolia application ID. If you’re not signed in, replace YourApplicationID with your Algolia application ID and YourWriteAPIKey with your Write API Key. You can find both in your Algolia account.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    # hello_algolia.rb
    require 'algolia'
    
    # Connect and authenticate with your Algolia app
    client = Algolia::Search::Client.create('YourApplicationID', 'YourWriteAPIKey')
    
    # Create a new index and add a record
    index = client.init_index('test_index')
    record = { 'objectID': 1, 'name': 'test_record'}
    index.save_object(record).wait()
    
    # Search the index and print the results
    results = index.search('test_record')
    puts results[:hits][0]
    
  2. Save the file as hello_algolia.rb. Go to the directory with the file you just created and run inside a terminal:

    1
    
    ruby hello_algolia.rb
    
  3. If the program ran successfully, you should see:

    1
    
    {:name=>"test_record", :objectID=>"1", :_highlightResult=>...}
    

You can inspect your index now in the Algolia dashboard.

Next steps

Now you can interact with the Algolia Search API, you can look at the available methods, for example, for search or indexing.

Other APIs, for example for Algolia Recommend or Analytics, come with their own clients. To get an overview, see Initialize the API client.

Did you find this page helpful?