Go API clients
The Go API clients let you interact with Algolia’s APIs from your Go backend.
Install the library
All API clients are part of the algoliasearch-client-go
package, which you can install with the go get
command.
1
go get github.com/algolia/algoliasearch-client-go/v4
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.
If you haven’t already, create an Algolia account.
Copy the following code into a new file
hello_algolia.go
. ReplaceALGOLIA_APPLICATION_ID
andALGOLIA_API_KEY
with values from your account. Make sure to use an API key withaddObject
andsearch
permissions.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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
// File: hello_algolia.gopackagemainimport("fmt""github.com/algolia/algoliasearch-client-go/v4/algolia/search")funcmain(){appID:="ALGOLIA_APPLICATION_ID"// API key with `addObject` and `search` ACLapiKey:="ALGOLIA_API_KEY"indexName:="test-index"record:=map[string]any{"objectID":"object-1","name":"test record",}client,err:=search.NewClient(appID,apiKey)iferr!=nil{panic(err)}// Add record to an indexsaveResp,err:=client.SaveObject(client.NewApiSaveObjectRequest(indexName,record),)iferr!=nil{panic(err)}// Wait until indexing is done_,err=client.WaitForTask(indexName,saveResp.TaskID)iferr!=nil{panic(err)}// Search for 'test'searchResp,err:=client.Search(client.NewApiSearchRequest(search.NewEmptySearchMethodParams().SetRequests([]search.SearchQuery{*search.SearchForHitsAsSearchQuery(search.NewEmptySearchForHits().SetIndexName(indexName).SetQuery("time"),),},),),)iferr!=nil{panic(err)}fmt.Println(searchResp.Results)}
In production, don’t include your credentials in your code. Use environment variables instead.
Run:
go run hello_algolia.go
If the command is successful, you’ll see the API response.
Next steps
You can view your new index in the Algolia dashboard.