# Via SDK

Connect to an instance from application code, including the credentials each SDK needs and how to create them.

Reach an instance from application code through one of the [SurrealDB SDKs](/docs#sdks).

This page covers what an SDK needs to connect and how to create it. The SDK reference for your language covers the API itself.

To get a snippet with the endpoint already filled in, select **Connect** on the instance in [SurrealDB Studio](https://app.surrealdb.com), then select your language.

![The Connect menu in SurrealDB Studio with the SDK option selected, showing a generated connection snippet containing the instance endpoint.](~/assets/img/image/cloud/open-in-sdk.png)

## What an SDK needs

An SDK needs three things:

- **The endpoint** of the instance, from the **Connect** menu.
- **A namespace and a database**, which tell SurrealDB where the query runs. A prompt to create them appears at the top of the dashboard if the instance has none. See [system structure](/docs/concepts.md#system-structure).
- **Credentials**, unless the connection is anonymous. Studio authenticates with your own session, but application code needs a user or an access method defined on the instance.

## Create credentials

> [!NOTE]
> This step applies when the SDK calls `signin` on connection. Skip it if the application authenticates some other way. See [access methods](/docs/reference/query-language/statements/define/access.md) and [system users](/docs/reference/query-language/statements/define/user.md).

1. Open the **Authentication** panel of the instance in SurrealDB Studio.
2. Select **+** in the **Root Authentication** section.
3. Choose the kind of credential you need.
4. Set the token duration and the session duration.

Two kinds of credential are offered:

- **New system user:** a username, a password, and a role that determines what the user may do. This is what a backend service typically uses.
- **New access method:** a named method whose type determines how clients authenticate through it. This is what record-level and end-user authentication uses.

Keep both durations short enough that a leaked token expires on its own.

![The Root Authentication dialog in SurrealDB Studio, offering a new system user with a username, password, and role, or a new access method with a name and type, each with configurable token and session durations.](~/assets/img/image/cloud/create-root-user.png)

Root credentials reach everything on the instance. For namespace-scoped or database-scoped authentication, and for record-level access, create the namespace and database first. Then define the user or the access method at that level.

## Connect

Every SDK follows the same shape. `connect` takes the endpoint, then you select the namespace and database, then you sign in. The examples below use root credentials.

> [!NOTE]
> With a non-root user, the sign-in call also needs the `access` details for the access method you defined. The [SDK reference](/docs#sdks) for your language shows the exact call.

**Rust**

```rust
use serde::{Deserialize, Serialize};
use surrealdb::engine::any;
use surrealdb::opt::auth::Root;
use tokio;
use chrono::{DateTime, Utc};

#[derive(Serialize, Deserialize)]
struct Project {
	name: String,
	description: String,
	status: String,
	priority: String,
	tags: Vec<String>,
	created_at: DateTime<Utc>,
}

// Open a connection
let db = any::connect("wss://<INSTANCE_ENDPOINT>").await?;

// Select namespace and database
db.use_ns("DEMO namespace").use_db("DEMO database").await?;

// Authenticate
db.signin(Root {
	username: "<username>",
	password: "<password>",
}).await?;

// Create a record
let project = Project {
	name: "SurrealDB Dashboard".to_string(),
	description: "Admin interface for SurrealDB".to_string(),
	status: "in_progress".to_string(),
	priority: "high".to_string(),
	tags: vec!["typescript".to_string(), "react".to_string(), "database".to_string()],
	created_at: Utc::now(),
};

db.create("project").content(project).await?;
```

**JavaScript**

```js
import { Surreal, Table } from "surrealdb";

const db = new Surreal();

// Open a connection and authenticate
await db.connect("wss://<INSTANCE_ENDPOINT>", {
	namespace: "DEMO namespace",
	database: "DEMO database",
	authentication: {
		username: "<username>",
		password: "<password>",
	}
});

// Create record
await db.create(new Table("project"), {
	name: "SurrealDB Dashboard",
	description: "Admin interface for SurrealDB",
	status: "in_progress",
	priority: "high",
	tags: ["typescript", "react", "database"],
	created_at: new Date(),
});

// Select all records in project table
console.log(await db.select(new Table("project")));

await db.close();
```

**Python**

```py
from surrealdb import Surreal
from datetime import datetime, timezone
from surrealdb import RecordID

# Open a connection
with Surreal("wss://<INSTANCE_ENDPOINT>") as db:

	# Select namespace and database
	db.use("DEMO namespace", "DEMO database")

	# Authenticate
	db.signin({
		"username": "<username>",
		"password": "<password>",
	})

	# Create a record
	db.create(RecordID("project", "1"), {
		"name": "SurrealDB Dashboard",
		"description": "Admin interface for SurrealDB",
		"status": "in_progress",
		"priority": "high",
		"tags": ["typescript", "react", "database"],
		"created_at": datetime.now(timezone.utc),
	})

	# Select a specific record
	print(db.select(RecordID("project", "1")))
```

**.NET**

```csharp
using SurrealDb.Net;
using SurrealDb.Net.Models.Auth;

const string TABLE = "project";

using var db = new SurrealDbClient("wss://<INSTANCE_ENDPOINT>/rpc");

// Select namespace and database
await db.Use("DEMO namespace", "DEMO database");

// Authenticate
await db.SignIn(new RootAuth { Username = "<username>", Password = "<password>" });

// Create record
var project = new Project
{
	Name = "SurrealDB Dashboard",
	Description = "Admin interface for SurrealDB",
	Status = "in_progress",
	Priority = "high",
	Tags = new[] { "typescript", "react", "database" },
	CreatedAt = DateTime.UtcNow,
};

await db.Create(TABLE, project);
```

**PHP**

```php
$db = new \Surreal\Surreal();

// Open a connection
$db->connect("wss://<INSTANCE_ENDPOINT>/rpc", [
	"namespace" => "DEMO namespace",
	"database" => "DEMO database",
]);

// Authenticate
$db->signin([
	"username" => "<username>",
	"password" => "<password>",
]);

// Create a record
$db->create("project", [
	"name" => "SurrealDB Dashboard",
	"description" => "Admin interface for SurrealDB",
	"status" => "in_progress",
	"priority" => "high",
	"tags" => ["typescript", "react", "database"],
	"created_at" => new DateTime(),
]);
```

## Next steps

- **[SDK reference](/docs#sdks):** the full API for each language, including live queries and transactions.
- **[Connect via HTTP](/docs/manage/instances/connect/via-http.md):** for languages without an SDK, and the request size limits that apply.
- **[Authentication](/docs/learn/security/authentication/summary.md):** choosing between system users, access methods, and record-level access.
