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

Source: https://sqlly.app/errors/libsql/unauthorized-401
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Error: `401 Unauthorized`
- Category: Authentication and tokens

Error message:

```text
Server returned HTTP status 401 Unauthorized
```

## What it means

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.

## Common causes

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

## How to fix it

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

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

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

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

   ```sql
   turso dev --db-file scratch.db
   ```

## Reproduce it safely

```sql
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 SQLly helps

SQLly stores the token with the connection, so you can paste the exact `libsql://` URL and token from your app config into the desktop client: if SQLly gets the same 401, the token or URL is wrong, and if it connects, your app is sending something different.

## Questions

### How do I check when my Turso auth token expires?

Decode the JWT payload: `echo '<token>' | cut -d. -f2 | base64 -d 2>/dev/null` and read the `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.

## Related errors

- [`auth token expired`](https://sqlly.app/errors/libsql/token-expired): Auth token expired
- [`403 Forbidden`](https://sqlly.app/errors/libsql/forbidden-403): 403 Forbidden: no permission for this resource
- [`429 Too Many Requests`](https://sqlly.app/errors/libsql/rate-limited-429): request rate limit hit

The code and message text are from the [libSQL / Turso documentation](https://docs.turso.tech/).
