API Reference / API Methods / The Go API client

Algolia’s Go API client lets you use Algolia’s APIs from your Go application. Compared to using the APIs directly, using the Go 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 Go API client is open source. You can find the source code on GitHub.

You’re reading the documentation for version 3 of the Go API client. Earlier versions are no longer under active development. If you want to update to version 3, see the upgrade guide.

Quickstart sample app

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

Install the Go API client

The Algolia Go API client requires Go version 1.8 or higher.

The recommended way to install the Go API client is via Go modules.

If you already initialized a Go project, run:

1
go get github.com/algolia/algoliasearch-client-go

You can initialize a new Go project with:

1
go mod init algolia-go-example

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 and API key. If you’re not signed in, replace YourApplicationID with your Algolia application ID and YourAdminAPIKey with your Admin API key. You can find both in your Algolia account.

    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
    
    // hello_algolia.go
    package main
    
    import (
      "fmt"
      "github.com/algolia/algoliasearch-client-go/v3/algolia/search"
    )
    
    type Record struct {
      ObjectID string `json:"objectID"`
      Name string `json:"name"`
    }
    
    func main() {
      // Connect and authenticate with your Algolia app
      client := search.NewClient("YourApplicationID", "YourAdminAPIKey")
    
      // Create a new index and add a record
      index := client.InitIndex("test_index")
      resSave, err := index.SaveObject(Record{ObjectID: "1", Name: "test_record"})
      if err != nil {
        fmt.Println(err.Error())
        err = nil
      }
      resSave.Wait()
    
      // Search the index and print the results
      results, err := index.Search("test_record")
      if err != nil {
        fmt.Println(err.Error())
        err = nil
      } else {
        var records []Record
        results.UnmarshalHits(&records)
        fmt.Println(records)
      }
    }
    
  2. Save the file as hello_algolia.go. Go to the directory with the file you just created and run inside a terminal:

    1
    
    go run hello_algolia.go
    
  3. If the program ran successfully, you should see:

    1
    
    {1 test_record}
    

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?