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

Install the library

All API clients are part of the algoliasearch-scala package, which you can add to your build.sbt file

1
libraryDependencies += "com.algolia" %% "algoliasearch-scala" % "2.8.0"

Test your installation

To test your installation, create a new Scala project in your favorite IDE and run 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 your IDE. 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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    
    package com.algolia.example
    
    import scala.concurrent.Await
    import scala.concurrent.duration._
    import scala.concurrent.ExecutionContext.Implicits.global
    
    import algoliasearch.api.SearchClient
    import algoliasearch.extension.SearchClientExtensions
    import algoliasearch.search.{SearchForHits, SearchMethodParams}
    import org.json4s.{JField, JObject, JString}
    
    @main
    def main(): Unit = {
      val appId = "ALGOLIA_APPLICATION_ID"
      // API key with `addObject` and `search` ACL
      val apiKey = "ALGOLIA_API_KEY"
      val indexName = "test-index"
    
      val client = SearchClient(appId, apiKey)
    
      // Create a new record
      val record = JObject(
        List(
          JField("objectID", JString("object-1")),
          JField("name", JString("test record"))
        )
      )
    
      // Add the record to an index
      val saveFuture = client.saveObject(
        indexName,
        body = record
      )
      // Block the execution until future is resolved
      val saveResponse = Await.result(saveFuture, 100.seconds)
    
      // Wait until indexing is done
      client.waitTask(indexName, resolvedResponse.taskID)
    
      // Search for 'test'
      val searchFuture = client.search(
        searchMethodParams = SearchMethodParams(
          requests = Seq(
            SearchForHits(
              indexName,
              query = Some("test"),
            )
          )
        )
      )
    
      val searchResponse = Await.result(searchFuture, 100.seconds)
      println(searchResponse)
    }
    

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

  3. Run: sbt run

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.

Did you find this page helpful?
Scala API clients v2