C# API clients
The C# API clients let you interact with Algolia’s APIs from your C# backend.
Install the library
All API clients are part of the Algolia.Search
package, which you can get from NuGet.
To install the package, use the .NET CLI:
1
dotnet add package Algolia.Search --version 7.0.0
Test your installation
To test your installation,
create a new .NET/C# project in your favorite IDE,
or on the command line with dotnet new console
.
Run 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 your IDE. Replace
ALGOLIA_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
using Algolia.Search.Clients; using Algolia.Search.Models.Search; var appID = "ALGOLIA_APPLICATION_ID"; // API key with `addObject` and `search` ACL var apiKey = "ALGOLIA_API_KEY"; var indexName = "test-index"; var client = new SearchClient(appID, apiKey); // Create a new record var record = new Dictionary<string, string> { { "objectID", "object-1" }, { "name", "test record" }, }; // Add record to an index var saveResp = await client.SaveObjectAsync(indexName, record); // Wait until indexing is done await client.WaitForTaskAsync(indexName, saveResp.TaskID); // Search for 'test' var searchResp = await client.SearchAsync<Object>( new SearchMethodParams { Requests = new List<SearchQuery> { new SearchQuery(new SearchForHits { IndexName = indexName, Query = "test" }) } } ); Console.WriteLine(searchResp.ToJson());
In production, don’t include your credentials in your code. Use environment variables instead.
-
Run the program. For example, with
dotnet run
.
If the program completed successfully, you’ll see the API response in JSON format.
Next steps
You can view your new index in the Algolia dashboard.