The .NET API client
Algolia’s .NET API client lets you use Algolia’s APIs from your .NET application. Compared to using the APIs directly, using the .NET 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.
Algolia’s .NET API client is open source. You can find the source code on GitHub.
You’re reading the documentation for version 6 of the .NET API client. Earlier versions are no longer under active development. If you want to update to version 6, see the upgrade guide.
Quickstart sample app
To download and run a self-contained example, see the .NET quickstart on GitHub.
ASP.NET
If you’re using ASP.NET, check out this tutorial.
Install the .NET API client
The API client supports:
- .NET Standard versions 1.3 to 2.1
- .NET Core versions 1.0 to 3.1
- .NET Framework versions 4.6 to 4.8
Install via the .NET CLI
If you already initialized a .NET project with the CLI, run:
1
dotnet add package Algolia.Search
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
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 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.Model.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 API client.