# Connect to Meilisearch

<Badge text="⭐ Community Toolkit" variant="tip" size="large" />

<Image
  src={meilisearchIcon}
  alt="Meilisearch logo"
  width={100}
  height={100}
  class:list={'float-inline-left icon'}
  data-zoom-off
/>

This page describes how consuming apps connect to a Meilisearch resource that's already modeled in your AppHost. For the AppHost API surface — adding a Meilisearch resource, data volumes, bind mounts, and master key parameters — see [Meilisearch Hosting integration](../meilisearch-host/).

When you reference a Meilisearch resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire Meilisearch client integration for automatic dependency injection and health checks.

## Connection properties

Aspire exposes each property as an environment variable named `[RESOURCE]_[PROPERTY]`. For instance, the `Url` property of a resource called `meilisearch` becomes `MEILISEARCH_URL`.

The Meilisearch resource exposes the following connection properties:

| Property Name | Description |
| ------------- | ----------- |
| `Host`        | The hostname or IP address of the Meilisearch server |
| `Port`        | The port number the Meilisearch server is listening on (default: `7700`) |
| `Url`         | The full base URL of the Meilisearch server, with the format `http://{Host}:{Port}` |
| `MasterKey`   | The master key used to authenticate with the Meilisearch server |

**Example connection values:**

```
Url: http://localhost:7700
MasterKey: p%40ssw0rd1
```

## Connect from your app

Pick the language your consuming app is written in. Each example assumes your AppHost adds a Meilisearch resource named `meilisearch` and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Meilisearch client integration. It registers a `MeilisearchClient` through dependency injection and adds health checks automatically. If you'd rather read environment variables directly, see the [Read environment variables](#read-environment-variables-in-c) section at the end of this tab.

#### Install the client integration

Install the [📦 CommunityToolkit.Aspire.Meilisearch](https://nuget.org/packages/CommunityToolkit.Aspire.Meilisearch) NuGet package in the client-consuming project:

<InstallDotNetPackage packageName="CommunityToolkit.Aspire.Meilisearch" />

#### Add the Meilisearch client

In _Program.cs_, call `AddMeilisearchClient` on your `IHostApplicationBuilder` to register a `MeilisearchClient`:

```csharp title="C# — Program.cs"
builder.AddMeilisearchClient(connectionName: "meilisearch");
```
**Tip:** The `connectionName` must match the Meilisearch resource name from the AppHost. For more information, see [Add Meilisearch resource](../meilisearch-host/#add-meilisearch-resource).

Resolve the client through dependency injection:

```csharp title="C# — ExampleService.cs"
public class ExampleService(MeilisearchClient client)
{
    // Use client...
}
```

#### Add keyed Meilisearch clients

To register multiple `MeilisearchClient` instances with different connection names, use `AddKeyedMeilisearchClient`:

```csharp title="C# — Program.cs"
builder.AddKeyedMeilisearchClient(name: "products");
builder.AddKeyedMeilisearchClient(name: "orders");
```

Then resolve each instance by key:

```csharp title="C# — ExampleService.cs"
public class ExampleService(
    [FromKeyedServices("products")] MeilisearchClient productsClient,
    [FromKeyedServices("orders")] MeilisearchClient ordersClient)
{
    // Use clients...
}
```

For more information, see [.NET dependency injection: Keyed services](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection#keyed-services).

#### Configuration

The Aspire Meilisearch client integration offers multiple ways to provide configuration.

**Connection strings.** When using a connection string from the `ConnectionStrings` configuration section, pass the connection name to `AddMeilisearchClient`:

```csharp title="C# — Program.cs"
builder.AddMeilisearchClient("meilisearch");
```

The connection string is resolved from the `ConnectionStrings` section:

```json title="JSON — appsettings.json"
{
  "ConnectionStrings": {
    "meilisearch": "Endpoint=http://localhost:7700/;MasterKey=your-master-key"
  }
}
```

**Configuration providers.** The client integration supports `Microsoft.Extensions.Configuration`. It loads settings from _appsettings.json_ (or any other configuration source) using the `Aspire:Meilisearch:Client` key:

```json title="JSON — appsettings.json"
{
  "Aspire": {
    "Meilisearch": {
      "Client": {
        "Endpoint": "http://localhost:7700/",
        "MasterKey": "your-master-key"
      }
    }
  }
}
```

#### Client integration health checks

Aspire client integrations enable health checks by default. The Meilisearch client integration adds a health check that verifies the Meilisearch instance is reachable and can respond to requests. The health check is wired into the `/health` HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

#### Read environment variables in C\#

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection properties from the environment and construct a `MeilisearchClient` directly using the [📦 Meilisearch.NET](https://www.nuget.org/packages/Meilisearch/) NuGet package:

```csharp title="C# — Program.cs"
using Meilisearch;

var url = Environment.GetEnvironmentVariable("MEILISEARCH_URL");
var masterKey = Environment.GetEnvironmentVariable("MEILISEARCH_MASTERKEY");

var client = new MeilisearchClient(url!, masterKey);

// Use client to interact with Meilisearch...
```

Install the official [Meilisearch Go client](https://github.com/meilisearch/meilisearch-go):

```bash title="Terminal"
go get github.com/meilisearch/meilisearch-go
```

Read the injected environment variables and connect:

```go title="Go — main.go"
package main

import (
    "os"
    meilisearch "github.com/meilisearch/meilisearch-go"
)

func main() {
    // Read the Aspire-injected connection properties
    client := meilisearch.New(
        os.Getenv("MEILISEARCH_URL"),
        meilisearch.WithAPIKey(os.Getenv("MEILISEARCH_MASTERKEY")),
    )

    // Use client to interact with Meilisearch...
}
```

Install the official [Meilisearch Python SDK](https://github.com/meilisearch/meilisearch-python):

```bash title="Terminal"
pip install meilisearch
```

Read the injected environment variables and connect:

```python title="Python — app.py"
import os
import meilisearch

# Read the Aspire-injected connection properties
client = meilisearch.Client(
    os.getenv("MEILISEARCH_URL"),
    os.getenv("MEILISEARCH_MASTERKEY"),
)

# Use client to interact with Meilisearch...
```

Install the official [Meilisearch JavaScript/TypeScript client](https://github.com/meilisearch/meilisearch-js):

```bash title="Terminal"
npm install meilisearch
```

Read the injected environment variables and connect:

```typescript title="TypeScript — index.ts"
import { MeiliSearch } from 'meilisearch';

// Read Aspire-injected connection properties
const client = new MeiliSearch({
    host: process.env.MEILISEARCH_URL!,
    apiKey: process.env.MEILISEARCH_MASTERKEY,
});

// Use client to interact with Meilisearch...
```
**Tip:** If your app expects specific environment variable names different from the Aspire defaults, you can pass individual connection properties from the AppHost using `WithEnvironment`. See the [AppHost reference](/get-started/app-host/) for details.