Skip to main content
In this quickstart we will learn how to:
  • Retrieve database credentials
  • Connect to a remote Turso database
  • Execute a query using SQL
1

Retrieve database credentials

You will need an existing database to continue. If you don’t have one, create one.Get the database URL:
turso db show --url <database-name>
Get the database authentication token:
turso db tokens create <database-name>
Assign credentials to the environment variables inside .env.
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
You will want to store these as environment variables.
2

Install

npm install @tursodatabase/serverless
3

Connect to your database

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:
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

const stmt = conn.prepare("SELECT * FROM users");
const rows = await stmt.all();
If you need to use placeholders for values, you can do that:
const stmt = conn.prepare("SELECT * FROM users WHERE id = ?");
const row = await stmt.get([1]);