Skip to content

Commit 179e628

Browse files
implement generate key script in Go (#43)
* implement generate key script in Go * Update readme
1 parent 26efec6 commit 179e628

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

go/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ This quickstart demonstrates various usages of the the [Algolia Go API Client](h
2424

2525
## How to use
2626

27+
The module has two dependencies:
28+
* [Algolia GO API client](github.com/algolia/algoliasearch-client-go/v3)
29+
* [GoDotEnv](https://github.com/joho/godotenv) - for using environment variables
30+
31+
Install these dependencies using the go build command.
32+
33+
```bash
34+
go build
35+
```
2736
Once setup, you can run each of the script in this folder using the Go command line.
2837
Example: to execute the `simple.go` script:
2938

@@ -37,3 +46,4 @@ go run simple.go
3746
| ------------- | ------------- |
3847
| [simple.go](./simple.go) | Index a single object and run a search query |
3948
| [indexing.go](./indexing.go) | Showcase of the main indexing methods |
49+
| [generate_key.go](./generate_key.go) | Generate a rate limted search API key |

go/generate_key.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// generate_key creates an API key for an Algolia application.
2+
// The generated key will be valid for Search operations only, and is limited to 100
3+
// queries per hour.
4+
5+
package main
6+
7+
// Install the API client: https://www.algolia.com/doc/api-client/getting-started/install/go/?client=go
8+
import (
9+
"errors"
10+
"fmt"
11+
"log"
12+
"os"
13+
14+
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
15+
"github.com/joho/godotenv"
16+
)
17+
18+
func main() {
19+
var err error
20+
21+
err = godotenv.Load()
22+
if err != nil {
23+
log.Fatal("Error loading .env file")
24+
}
25+
// Get your Algolia Application ID and (admin) API key from the dashboard: https://www.algolia.com/account/api-keys
26+
// and choose a name for your index. Add these environment variables to a `.env` file:
27+
appID, apiKey, indexName := os.Getenv("ALGOLIA_APP_ID"), os.Getenv("ALGOLIA_API_KEY"), os.Getenv("ALGOLIA_INDEX_NAME")
28+
29+
// Start the API client
30+
// https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/
31+
client := search.NewClient(appID, apiKey)
32+
33+
// Create the API key.
34+
// https://www.algolia.com/doc/api-reference/api-methods/add-api-key/?client=go
35+
fmt.Println("Generating key...")
36+
37+
keyParams := search.Key{
38+
ACL: []string{"search"},
39+
Description: "Restricted search-only API key for algolia.com",
40+
MaxQueriesPerIPPerHour: 100,
41+
}
42+
43+
var key string
44+
var keyRes search.CreateKeyRes
45+
46+
keyRes, err = client.AddAPIKey(keyParams)
47+
err = keyRes.Wait()
48+
if err != nil {
49+
panic(errors.New("Error generating key."))
50+
} else {
51+
key = keyRes.Key
52+
fmt.Println("Key generated successfully:", key)
53+
}
54+
55+
// Test the new key
56+
fmt.Println("Testing key...")
57+
58+
// Initialise a new client with the generated key
59+
client = search.NewClient(appID, key)
60+
61+
// Create an index (or connect to it, if an index with the name `ALGOLIA_INDEX_NAME` already exists)
62+
// https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/#initialize-an-index
63+
index := client.InitIndex(indexName)
64+
65+
// Search the index with an empty string
66+
// https://www.algolia.com/doc/api-reference/api-methods/search/
67+
var indexRes search.QueryRes
68+
69+
indexRes, err = index.Search("")
70+
if err != nil {
71+
panic(errors.New("Error testing key."))
72+
} else {
73+
fmt.Println("Key tested successfully.", indexRes.NbHits, "hits found.")
74+
}
75+
}

0 commit comments

Comments
 (0)