You're viewing an archived version of our docs. Check out our current documentation →

Ruby API clients

The Ruby API clients let you interact with Algolia’s APIs from your Ruby backend.

If you’re building a Ruby on Rails app, see Set up Algolia for Ruby on Rails.

Install the library

All API clients are part of the algolia gem. You’ll need Ruby 2.2 or later.

1
gem install algolia -v '>= 3.0.0, < 4.0'

Test your installation

To test your installation, try running a short program that adds a record to a test index, searches your index, and prints the results.

  1. If you haven’t already, create an Algolia account.

  2. Copy the following code into a new file hello_algolia.rb. Replace ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY with values from your account. Make sure to use an API key with addObject and search permissions.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    # File: hello_algolia.rb
    require "algolia"
    
    app_id = "ALGOLIA_APPLICATION_ID"
    # API key with `addObject` and `search` ACL
    api_key = "ALGOLIA_API_KEY"
    index_name = "test-index"
    
    client = Algolia::SearchClient.create(app_id, api_key)
    
    record = { objectID: "object-1", name: "test record" }
    
    # Add record to an index
    save_resp = client.save_object(
      index_name = index_name,
      body = record
    )
    
    # Wait until indexing is done
    client.wait_for_task(
      index_name = index_name,
      task_id = save_resp.task_id
    )
    
    # Search for 'test'
    results = client.search(
      search_method_params = {
        requests: [{ indexName: index_name, query: "test" }]
      }
    )
    
    puts results.to_json
    

    In production, don’t include your credentials in your code. Use environment variables instead.

  3. Run: ruby hello_algolia.rb

If the command is successful, you’ll see the API response in JSON format.

Next steps

You can view your new index in the Algolia dashboard.

Ruby API clients v3