// 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.

Ошибка
401 Unauthorized
Категория
Authentication and tokens
Сообщение об ошибке
Server returned HTTP status 401 Unauthorized

Что это значит

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.

Распространённые причины

  • 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.

Как исправить

  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.

Безопасное воспроизведение

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.

Вопросы

How do I check when my Turso auth token expires?

Decode the JWT payload: echo '<token>' | cut -d. -f2 | base64 -d 2>/dev/null и читайте 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.

Связанные ошибки

More authentication and tokens errors

Код и текст сообщения взяты из libSQL / Turso documentation. Смотрите весь libSQL error list или поиск по всем ошибкам баз данных.

// меньше ошибок, быстрее исправления

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.

Текст и данные на этой странице: CC BY 4.0, с указанием SQLly (sqlly.app). Код и фрагменты SQL: MIT. Подробнее о лицензии Цитируемый текст сообщений об ошибках принадлежит производителю СУБД.