C# API clients
This is documentation for v6 of the C# API clients, which is not the latest version. To see the documentation for the latest version, see C# v7.
Algolia’s C# API client lets you use Algolia’s APIs from your C# app. Compared to using the APIs directly, using the C# 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.
ASP.NET
If you’re using ASP.NET, see Integrate Algolia with ASP.NET.
Install the C# API client
The API client supports .NET Standard versions 1.3 to 2.1. For more details about supported .NET implementations, see .NET Standard versions.
Install via the .NET CLI
If you already initialized a .NET project with the CLI, run:
1
dotnet add package Algolia.Search --version 6.*
You can initialize a new .NET project with:
1
dotnet new TYPE
Replace TYPE
with the type of application you want to build.
For example, with dotnet new console
you can create a new command-line application.
Install via NuGet
1
Install-Package Algolia.Search -Version 6.*
Download the package
You can download the package from NuGet.org.
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.
The following example uses the .NET CLI.
-
Create a new .NET project and install the Algolia .NET API client:
Copy1 2 3
dotnet new console -o AlgoliaExample cd AlgoliaExample dotnet add package Algolia.Search
-
Open the file
Program.cs
with a text editor and replace its content with the following code. ReplaceYourApplicationID
with your Algolia application ID andYourWriteAPIKey
with your Admin 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
// Program.cs using System; using System.Linq; using System.Collections.Generic; using Algolia.Search.Clients; using Algolia.Search.Models.Search; namespace HelloAlgolia { // a simple record in your index class Record { public string Name { get; set; } public string ObjectID { get; set; } public override string ToString() { return $"Name: {Name}, ObjectID: {ObjectID}"; } } class Program { static void Main(string[] args) { // Connect and authenticate with your Algolia app var client = new SearchClient( "YourApplicationID", "YourWriteAPIKey" ); // Create a new index and add a record var index = client.InitIndex("test_index"); var record = new Record{ ObjectID = "1", Name = "test_record" }; index.SaveObject(record).Wait(); // Search the index and print the results var results = index.Search<Record>(new Query("test_record")); Console.WriteLine(results.Hits.ElementAt(0).ToString()); } } }
-
Inside your project directory, run:
Copy1
dotnet run
-
If the program ran successfully, you should see:
Copy1
Name: test_record, ObjectID: 1
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 C# API client.