---
title: "Connecting to SurrealDB"
description: "Create a SurrealDbClient for a local, remote or embedded endpoint, connect, select a namespace and database, check the health and version of the server, and configure connection strings and options in the .NET SDK."
canonical_url: "https://surrealdb.com/docs/reference/dotnet/concepts/connecting-to-surrealdb"
sdk: ".NET SDK"
sdk_version: "1.0.1"
surrealdb_version: "4.x"
last_modified: "2026-11-24T05:29:47Z"
---

> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# Connecting to SurrealDB

Create a SurrealDbClient for a local, remote or embedded endpoint, connect, select a namespace and database, check the health and version of the server, and configure connection strings and options in the .NET SDK.

After [installing the SDK](/docs/reference/dotnet/installation.md), you can initialise a new instance of a SurrealDB client. When creating a new connection to a SurrealDB instance, you can choose to connect to a local or remote endpoint. Every asynchronous method in the SDK takes an optional `cancellationToken`, which cancels the operation.

```csharp
using SurrealDb.Net;

using var db = new SurrealDbClient("ws://127.0.0.1:8000/rpc");

await db.Connect();
await db.Use("main", "main");
```

From the code snippet above, you can see that the .NET SDK has a couple of methods that you can use to initialise a new project with SurrealDB.

## `SurrealDbClient`

Creates a new client, detecting the right protocol from the provided endpoint.

```csharp title="Method Syntax"
new SurrealDbClient(endpoint)
```

### Connection options

You can specify your connection protocol either as `http`, `https`, `ws`, or `wss`. Since SurrealDB also supports RPC over WebSocket, by default, it is specified with a `/rpc` suffix.

**Local endpoint**

```csharp
// Creates a new client using a local endpoint
using var db = new SurrealDbClient("http://127.0.0.1:8000");
```

**Remote endpoint**

```csharp
// Creates a new client using a remote endpoint
using var db = new SurrealDbClient("wss://cloud.surrealdb.com/rpc");
```

**Namespace and database**

```csharp
var options = new SurrealDbOptions
{
    Endpoint = "wss://cloud.surrealdb.com/rpc",
    Namespace = "surrealdb",
    Database = "docs",
};

// Specify a namespace and database pair to use
using var db = new SurrealDbClient(options);
```

**Token**

```csharp
var options = new SurrealDbOptions
{
    Endpoint = "wss://cloud.surrealdb.com/rpc",
    Token = "......",
};

// Authenticate with an existing token
using var db = new SurrealDbClient(options);
```

**Credentials**

```csharp
var options = new SurrealDbOptions
{
    Endpoint = "wss://cloud.surrealdb.com/rpc",
    Username = "root",
    Password = "surrealdb",
};

// Authenticate using a pair of credentials
using var db = new SurrealDbClient(options);
```

> [!NOTE]
> Having to manually set all these options into a `SurrealDbOptions` object can be cumbersome. If you are familiar with the concept of Connection Strings, you can simply pass a connection string to the `SurrealDbClient` constructor. See the [Connection Strings](#connection-strings) section for more information.

<br />

### Effect of connection protocol on token & session duration

The connection protocol you choose affects how authentication tokens and sessions work:

With websockets connections (`ws://`, `wss://`) you open a single long-lived stateful connection where after the initial authentication, the session duration applies and if not specified, defaults to `NONE` meaning that the session never expires unless otherwise specified.

When you connect with an HTTP connection (`http://`, `https://`), every request is short-lived and stateless, so each one is authenticated on its own and creates a short-lived session. After a system user signs in, the SDK sends the username and password with every request, so the token duration does not apply. After a record user signs in, the SDK sends the token, so the token duration applies and requests fail once it expires. The token duration defaults to 1 hour.

The figure below shows which duration ends the session in each case, with the default durations.

<img src="~/assets/img/diagrams/light/conn-token-session-light.webp" darkSrc="~/assets/img/diagrams/conn-token-session.webp" alt="A timeline over two hours shows which duration ends a session when the token duration is the default of one hour. A WebSocket connection that signed in with credentials keeps one session, and with the default session duration of NONE it keeps working after the token that signin returned has expired. A WebSocket connection that authenticated with a token has its session end when the token expires, and later queries fail with 'The session has expired'. Over HTTP, requests that send the token in an Authorization Bearer header succeed until the token expires and then fail with 401, while requests that send Basic credentials sign in again each time and keep succeeding. Over HTTP, the JavaScript, Python and Go SDKs send the token; the Rust, Java and .NET SDKs send a system user's username and password with every request, and a token for record users." />

You can extend the session duration of a token or a session by setting the `DURATION` clause when creating a new access method with the [`DEFINE ACCESS METHOD`](/docs/reference/query-language/statements/define/access.md) statement or when defining a new user with the [`DEFINE USER`](/docs/reference/query-language/statements/define/user.md) statement.

Learn more about token and session duration in our [security best practices](/docs/learn/security/best-practices/security-best-practices.md#expiration) documentation.

<br />
## `.Connect()` {#connect}

The `.Connect()` executes a connection attempt to the underlying endpoint using the provided connection options.

```csharp title="Method Syntax"
await db.Connect()
```

### Arguments

<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="col" scope="row" data-label="Arguments">
                <code>cancellationToken</code>
                <label label="optional" />
            </td>
            <td colspan="2" scope="col" scope="row" data-label="Description">
                The cancellationToken enables graceful cancellation of asynchronous operations.
            </td>
        </tr>
    </tbody>
</table>

> [!NOTE]
>This method is automatically called before executing any other call to the SurrealDB instance. It means that you do not have to explicitly call this method. In some contexts, calling this method beforehand can improve performance by avoiding cold starts.

### Example usage

```csharp
await db.Connect();
```

<br />

## `.Use()` {#use}

Depending on the complexity of your use case, you can switch to a specific namespace and database using the `.Use()` method. This is particularly useful if you want to switch to a different setup after connecting.

```csharp title="Method Syntax"
await db.Use(ns, db)
```

### Arguments

<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="col" scope="row" data-label="Arguments">
                <code>namespace</code>
                <label label="initially required" />
            </td>
            <td colspan="2" scope="col" scope="row" data-label="Description">
                Switches to a specific namespace.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="col" scope="row" data-label="Arguments">
                <code>database</code>
                <label label="initially required" />
            </td>
            <td colspan="2" scope="col" scope="row" data-label="Description">
                Switches to a specific database.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="col" scope="row" data-label="Arguments">
                <code>cancellationToken</code>
                <label label="optional" />
            </td>
            <td colspan="2" scope="col" scope="row" data-label="Description">
                The cancellationToken enables graceful cancellation of asynchronous operations.
            </td>
        </tr>
    </tbody>
</table>

### Example usage

```csharp
await db.Use("main", "main");
```

<br />

## `.Health()` {#health}

Checks the status of the database server and storage engine.

```csharp title="Method Syntax"
await db.Health()
```

### Arguments

<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="col" scope="row" data-label="Arguments">
                <code>cancellationToken</code>
                <label label="optional" />
            </td>
            <td colspan="2" scope="col" scope="row" data-label="Description">
                The cancellationToken enables graceful cancellation of asynchronous operations.
            </td>
        </tr>
    </tbody>
</table>

### Example usage

```csharp
bool status = await db.Health();
```

<br />

## `.Version()` {#version}

Retrieves the version of the SurrealDB instance.

```csharp title="Method Syntax"
await db.Version()
```

### Arguments

<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="col" scope="row" data-label="Arguments">
                <code>cancellationToken</code>
                <label label="optional" />
            </td>
            <td colspan="2" scope="col" scope="row" data-label="Description">
                The cancellationToken enables graceful cancellation of asynchronous operations.
            </td>
        </tr>
    </tbody>
</table>

### Example usage

```csharp
string version = await db.Version(); // Will return "surrealdb-1.1.1"
```

## Connection strings {#connection-strings}

Connection Strings are an easy way to configure your application to connect to a SurrealDB instance. They are stored in the <code>appsettings.json</code> file and can be used to configure the <code>SurrealDbClient</code>.

It is good practice to:

- set a development Connection String in <code>appsettings.Development.json</code>,
- store your production Connection String in a Secret environment variable, or even better in a Vault.

<table>
  <thead>
    <tr>
      <th scope="col">Keys</th>
      <th colspan="2" scope="col">
        Description
      </th>
      <th scope="col">Aliases</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td scope="row" data-label="Keys">
        <code>Endpoint</code>
        <label label="required" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        The database endpoint to connect to. <br />
        The disctinction between <code>Server</code> and <code>Client</code> can
        help you ensure you only call a distant database (server mode) or a
        local database (client mode).
      </td>
      <td scope="row" data-label="Aliases">
        <code>Server</code>
        <code>Client</code>
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Keys">
        <code>Namespace</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        Switches to a specific namespace.
      </td>
      <td scope="row" data-label="Aliases">
        <code>NS</code>
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Keys">
        <code>Database</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        Switches to a specific database.
      </td>
      <td scope="row" data-label="Aliases">
        <code>DB</code>
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Keys">
        <code>Username</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        Username used to have root access.
      </td>
      <td scope="row" data-label="Aliases">
        <code>User</code>
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Keys">
        <code>Password</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        Password used to have root access.
      </td>
      <td scope="row" data-label="Aliases">
        <code>Pass</code>
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Keys">
        <code>Token</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        Token (JWT) used to have user access.
      </td>
      <td scope="row" data-label="Aliases"></td>
    </tr>
    <tr>
      <td scope="row" data-label="Keys">
        <code>AuthLevel</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        Auth level when connecting to the SurrealDB instance. <br />
        Valid options are <code>Root</code>, <code>Namespace</code> or{" "}
        <code>Database</code>. <br />
        Defaults to <code>Root</code>.
      </td>
      <td scope="row" data-label="Aliases"></td>
    </tr>
  </tbody>
</table>

### Examples

Here are a couple of examples of Connection Strings:

```sh
Server=http://127.0.0.1:8000;Namespace=test;Database=test;Username=root;Password=secret
```

```sh
Endpoint=http://127.0.0.1:8000;NS=test;DB=test;User=root;Pass=secret
```

```sh
Server=ws://127.0.0.1:8000;AuthLevel=Namespace;NS=test;DB=test;User=root;Pass=secret
```

```sh
Client=mem://;Namespace=test;Database=test
```
