Before you start, make sure you:

1

Install the libSQL SDK

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

2

Configure database credentials

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.

NUXT_TURSO_DATABASE_URL=
NUXT_TURSO_AUTH_TOKEN=
3

Configure variables inside Nuxt's runtime config.

nuxt.config.ts
export default defineNuxtConfig({
  runtimeConfig: {
    turso: {
      databaseUrl: "",
      authToken: "",
    },
  },
});

Make sure that names of the keys in the runtimeConfig object match the names of your environment variables. Read more about this here.

4

Configure libSQL Client.

server/utils/turso.ts
import { createClient } from "@libsql/client";
// You can optionally pass in the event to useRuntimeConfig
// import { H3Event } from "h3";

export function useTurso(/* event: H3Event */) {
  const { turso } = useRuntimeConfig(/* event */);

  return createClient({
    url: turso.databaseUrl,
    authToken: turso.authToken,
  });
}
5

Execute SQL

server/api/items.get.ts
export default defineEventHandler(async (event) => {
  const client = useTurso(/* event */);
  const { rows } = await client.execute("select * from table_name");

  return {
    data: {
      items: rows,
    },
  };
});

Examples