dclient/docs/environment.md
Simon Willison 32124a0bf3 Support DATASETTE_URL and DATASETTE_TOKEN environment variables
Allow configuring a base Datasette instance URL and API token via
environment variables, so users can run commands like
`dclient query data "select ..."` without specifying a full URL each time.

DATASETTE_URL is combined with the argument as a path segment.
DATASETTE_TOKEN is used as a lowest-priority fallback for auth.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 22:10:59 -08:00

68 lines
1.7 KiB
Markdown

(environment-variables)=
# Environment variables
`dclient` supports two environment variables for convenient access to a Datasette instance without needing aliases or repeated URLs.
## DATASETTE_URL
Set this to the base URL of your Datasette instance:
```bash
export DATASETTE_URL=https://my-instance.datasette.cloud
```
Then pass just the database name as the first argument to any command:
```bash
dclient query data "select * from my_table limit 10"
```
This is equivalent to:
```bash
dclient query https://my-instance.datasette.cloud/data "select * from my_table limit 10"
```
It works with all commands:
```bash
dclient insert data my_table data.csv --csv
dclient actor data
```
Full URLs and aliases always take priority over `DATASETTE_URL`. If the argument starts with `http://` or `https://`, it is used as-is. If it matches an alias in `aliases.json`, the alias is used.
## DATASETTE_TOKEN
Set this to an API token:
```bash
export DATASETTE_TOKEN=dstok_abc123
```
The token will be used automatically for any request that doesn't have a more specific token configured.
The precedence order for tokens is:
1. `--token` CLI flag (highest priority)
2. Stored token from `auth.json` (matched by URL prefix)
3. `DATASETTE_TOKEN` environment variable (lowest priority)
## Using both together
These variables work well together for quick access to a single instance:
```bash
export DATASETTE_URL=https://my-instance.datasette.cloud
export DATASETTE_TOKEN=dstok_abc123
# Query the "data" database
dclient query data "select * from my_table"
# Insert into the "data" database
cat records.json | dclient insert data my_table - --json
# Check your actor identity
dclient actor data
```