// libSQL error reference

401 Unauthorized: 401 Unauthorized: bad or missing auth token

The server rejected the auth token: it is missing, malformed, expired, or was created for a different database.

Fehler
401 Unauthorized
Kategorie
Authentication and tokens
Fehlermeldung
Server returned HTTP status 401 Unauthorized

Was es bedeutet

Turso and sqld check a JWT auth token on every remote request. A 401 means the check failed before any SQL ran. Turso's API docs list 401 as Invalid or expired auth token; a missing token fails the same way. The three usual shapes are: no token at all in the URL or header, a token that was created for another database, or a token whose expiry has passed. A 401 with a token you believe is valid is almost always expiry or a mismatch between token and database.

Häufige Ursachen

  • No auth token was attached to the connection URL or the Authorization header.
  • The token was created for a different database. Turso tokens are database-scoped.
  • The token expired. By default turso db tokens create tokens never expire, so an expired token means it was minted with --expiration and the date passed, or a platform token with a built-in expiry aged out.
  • The token string was truncated or wrapped somewhere, for example in an env file or a CI secret.

Find the cause

Decode the token payload and read the `exp` claim. If `exp` is in the past, the token expired. Compare the database name in the token's audience or in your URL with `turso db show <name> --url`. A 401 on every request with a valid, unexpired, matching token points at the token never reaching the server, so check the client's URL scheme: `libsql://` URLs need the token in the URL or a client option, plain HTTPS platform API calls need an `Authorization: Bearer` header.

Wie du es behebst

  1. Create a fresh token for the database you connect to and check the expiry:

    turso db tokens create mydb --expiration 7d
  2. Confirm the URL and token point at the same database. The URL is in the database's connect string:

    turso db show mydb --url
  3. Decode the token to inspect its claims and expiry. It is a JWT, so the payload is base64:

    echo '<token>' | cut -d. -f2 | base64 -d 2>/dev/null; echo
  4. For local testing without the cloud, run sqld yourself and skip the platform token path entirely:

    turso dev --db-file scratch.db

Other ways to handle it

For throwaway work, turso dev runs a local sqld with no auth, or a plain file URL file:local.db skips the server entirely. For CI, mint a short-lived token at job start instead of storing a long-lived one.

Sicher reproduzieren

Local: `turso dev --db-file scratch.db` starts a server with auth disabled, so 401 cannot be reproduced against it. To reproduce the real thing you need a Turso Cloud database: create one with `turso db create repro401`, then connect with a deliberately wrong token string and the client returns SERVER_ERROR with status 401. Note this needs a real cloud database and token; there is no honest offline repro.

Fragen

How do I check when my Turso auth token expires?

Decode the JWT payload: echo '<token>' | cut -d. -f2 | base64 -d 2>/dev/null und lies den exp claim (seconds since epoch). Tokens from turso db tokens create have no expiry unless the token was minted with --expiration, so an exp in the past means someone set one deliberately.

Where does the auth token go in a libSQL connection?

Either in the URL as ?authToken=<token> or through the client's authToken option (createClient({ url, authToken }) in @libsql/client). Plain HTTPS calls to the platform API take an Authorization: Bearer header instead.

Why does my token work in turso db shell but fail in my app?

The shell authenticates with your CLI login, not the database token your app uses. Compare turso db show mydb --url with the URL in your app config and mint a fresh database token with turso db tokens create mydb for the app.

Zugehörige Fehler

More authentication and tokens errors

Der Code und der Meldungstext stammen aus der libSQL / Turso documentation. Durchstöbere die vollständige libSQL error list oder Jeden Datenbankfehler durchsuchen.

// weniger Fehler, schnellere Korrekturen

Work on libSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for libSQL and the other databases you use.

Text und Daten auf dieser Seite: CC BY 4.0, Quelle: SQLly (sqlly.app). Code und SQL-Snippets: MIT. Lizenzdetails Zitierte Fehlermeldungstexte gehören dem jeweiligen Datenbankhersteller.