|
| 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