# Turso Quickstart (TypeScript)

> Get started with Turso and TypeScript in a few simple steps.
> Canonical HTML: https://tursodb.cn/docs/sdk/ts/quickstart/
> Upstream Source (MIT): https://docs.turso.tech/sdk/ts/quickstart
> Language: en

---
In this TypeScript quickstart we will learn how to:

* Install the Turso package
* Connect to a local or remote database
* Execute a query using SQL
* Sync changes to the cloud

## Recommended: @tursodatabase/database (Local / Embedded)

`@tursodatabase/database` is the recommended package for local and embedded use cases (Node.js, Electron, mobile, IoT). It is built on the Turso Database engine — a ground-up rewrite of SQLite with concurrent writes (MVCC) and async I/O.

**1. Install**

```bash
npm install @tursodatabase/database
```

**2. Connect**

```ts
import { connect } from "@tursodatabase/database";

const db = await connect("app.db");
```

**3. Execute**

```ts
const createTable = db.prepare(`
  CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT NOT NULL
  )
`);
await createTable.run();

const insertUser = db.prepare("INSERT INTO users (username) VALUES (?)");
await insertUser.run("alice");

const stmt = db.prepare("SELECT * FROM users");
const users = await stmt.all();
console.log(users);
```

**4. Sync (push and pull)**

If you need to sync your local database with Turso Cloud, use [`@tursodatabase/sync`](/docs/sync/usage/):

```bash
npm install @tursodatabase/sync
```

```ts
import { connect } from "@tursodatabase/sync";

const db = await connect({
  path: "./app.db",
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
});

await db.exec("INSERT INTO users (username) VALUES ('bob')");

// Push local writes to Turso Cloud
await db.push();

// Pull remote changes to local database
const changed = await db.pull();
```

All reads and writes happen against the local database file — fast, offline-capable. `push()` sends your changes to the cloud. `pull()` brings remote changes down. See [Turso Sync](/docs/sync/usage/) for details on conflict resolution, checkpointing, and more.

## Remote Turso database: @tursodatabase/serverless

`@tursodatabase/serverless` is the package for applications that connect to a remote [Turso database](/docs/tursodb/quickstart/) on Turso Cloud over the network — including Node.js servers, Docker containers, serverless functions, and edge runtimes (Cloudflare Workers, Vercel Edge, Deno Deploy). It uses only `fetch` — zero native dependencies, works everywhere.

**1. Retrieve database credentials**

You will need an existing Turso database to continue. If you don't have one, create one with `turso db create --tursodb` (see the [quickstart](/docs/quickstart/)).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=turso://[databaseName]-[organizationSlug].turso.io
TURSO_AUTH_TOKEN=
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

You will want to store these as environment variables.

</div></div>


**2. Install**

```bash
npm install @tursodatabase/serverless
```

**3. Connect to your database**

```ts
import { connect } from "@tursodatabase/serverless";

const conn = connect({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
});
```

For compatibility with the `@libsql/client` API, use the compat module:

```ts
import { createClient } from "@tursodatabase/serverless/compat";

const client = createClient({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
});
```

**4. Execute a query using SQL**

```ts
const stmt = await conn.prepare("SELECT * FROM users");
const rows = await stmt.all();
```

If you need to use placeholders for values, you can do that:

```ts
const stmt = await conn.prepare("SELECT * FROM users WHERE id = ?");
const row = await stmt.get([1]);
```

## Remote libSQL database: @libsql/client

`@libsql/client` is the package for applications that connect to a remote [libSQL](/docs/libsql/) database on Turso Cloud. It is also the package to use for ORM integration (Drizzle, Prisma). See the [reference](/docs/sdk/ts/reference/#libsqlclient) for full documentation.

**1. Install**

Begin by installing the `@libsql/client` dependency in your project:



```bash npm
npm install @libsql/client
```

```bash pnpm
pnpm install @libsql/client
```

```bash yarn
yarn add @libsql/client
```

```bash deno
deno add npm:@libsql/client
```

**2. Connect**

```ts Node.js
import { createClient } from "@libsql/client";

export const turso = createClient({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
});
```

```ts Edge Runtimes
import { createClient } from "@libsql/client/web";

export const turso = createClient({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
});
```

```ts Deno
import { createClient } from "https://esm.sh/@libsql/client@0.6.0/web";

export const turso = createClient({
  url: Deno.env.get("TURSO_DATABASE_URL"),
  authToken: Deno.env.get("TURSO_AUTH_TOKEN"),
});
```

**3. Execute**

```ts
await turso.execute("SELECT * FROM users");
```

**4. Sync**

If you need to sync your local database with a remote Turso Cloud database (local reads and writes with push/pull to the cloud), use [Turso Sync](/docs/sync/usage/). Turso Sync is built on the Turso Database engine and provides local-first sync with explicit `push()` and `pull()` operations.
