Go API clients
This is documentation for v3 of the Go API clients, which is not the latest version. To see the documentation for the latest version, see Go v4.
Algolia’s Go API client lets you use Algolia’s APIs from your Go app. 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.
Install the Go API client
The Algolia Go API client requires Go version 1.8 or later.
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/v3
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.
-
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 andYourWriteAPIKey
with your Write API Key. You can find both in your Algolia account.Copy1 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
// 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", "YourWriteAPIKey") // Create a new index and add a record index := client.InitIndex("test_index") record := Record{ ObjectID: "1", Name: "test_record", } resSave, err := index.SaveObject(record) if err != nil { panic(err) } resSave.Wait() // Search the index and print the results results, err := index.Search("test_record") if err != nil { panic(err) } var records []Record results.UnmarshalHits(&records) fmt.Println(records) }
-
Save the file as
hello_algolia.go
. Go to the directory with the file you just created and run inside a terminal:Copy1
go run hello_algolia.go
-
If the program ran successfully, you should see:
Copy1
{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 Go API client.