mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem The CLI reads `supabase/config.jsonc` (and `config.json`) since v3.1.0, but every docs page still shows `config.toml` only. There's also no page explaining the formats, how to switch, the `$schema` / `#:schema` declaration, or editor setup, and no reference pages for the new `supabase config convert` and `supabase config validate` commands. Linear: CLI-2871 ## Solution - **Dual-format examples from one source.** A new directive (`features/directives/ConfigFormats.ts`) picks up any ```` ```toml ```` fence with `name=supabase/config.toml` and renders it as `supabase/config.toml` / `supabase/config.jsonc` tabs. The JSONC is generated at build time with `convertCliConfigTomlToJsonc` from `@supabase/config`, the same converter `supabase config convert` uses, so authors only write TOML and the JSONC matches what the CLI would produce (comments included). Inside `<$CodeTabs>` the JSONC is added as an extra tab. - **The format choice sticks.** `ConfigCodeTabs` keeps the selected format in `localStorage`, so choosing JSONC switches every config example on the page and on later pages. - **Config reference.** The `usage` examples on `/guides/local-development/cli/config` use the same tabs (they were highlighted as Python before), and the intro mentions `config.jsonc`. The `remotes.<branch_name>` example now uses `[remotes.staging]`, since `<branch_name>` isn't a valid TOML key. - **New guide, "Config file formats"** (`/guides/local-development/cli/config-formats`): which file wins, minimum CLI version, `convert` and `validate`, schema declaration and versioning, and editor setup (VS Code trusted domains, Even Better TOML, Tombi, SchemaStore). - **CLI reference.** Adds `supabase-config-convert` and `supabase-config-validate` to the CLI reference nav. `cli_v1_commands.yaml` is the generated v3.2.0 reference, identical to the release bot's update in #53463, so the two PRs don't conflict. - **Content.** About 60 existing config examples across the guides, including the Branching configuration page, are tagged `name=supabase/config.toml`. Branching reads `config.json` and `config.jsonc` as well, and the Branching configuration page now says so. Non-Supabase TOML (`pyproject.toml`, `bunfig.toml`) and the v2-era `[inbucket]` before/after snippet stay TOML-only. - **Format-neutral prose.** Sentences that described the config as `config.toml` now say "your config file", naming `supabase/config.toml` and `supabase/config.jsonc` once per page. TOML section syntax in prose (`max_frequency` under `[auth.email]`) is written as key paths (`auth.email.max_frequency`), which read the same in both formats. Headings that named `config.toml` keep their old anchors. Sentences about what `supabase init` creates still say `config.toml`. - **Markdown export for agents.** `internals/generate-guides-markdown.ts` runs the same transform, so the `.md` pages served to AI agents include the JSONC example after each TOML one. - A test converts every tagged example under `content/`, so a broken TOML example fails tests instead of the build. New dependencies for `apps/docs`: `@supabase/config` (^1.8.0) and `effect` (the same `effect` version Studio pins). `config convert` and `config validate` ship in CLI v3.2.0, which is now stable. ## Review instructions 1. Open `/docs/guides/local-development/seeding-your-database` on the preview. Switch an example to `supabase/config.jsonc` and check the other examples on the page switch too. 2. Open `/docs/guides/local-development/cli/config` and check the usage examples open on JSONC. 3. Read `/docs/guides/local-development/cli/config-formats`. 4. Open `/docs/reference/cli/supabase-config-convert` and `/docs/reference/cli/supabase-config-validate`. ## Self-review Ran the `review-the-docs` local self-review (content, pipeline, and component checks) over the 54 changed pages. - No blockers. Fixed the nits it found: leftover TOML section syntax in prose (diff engines, declarative schemas, Branching, the common settings table on Local and hosted projects), a few long or ungrammatical sentences, and the conversion follow-up steps on Config file formats, now a list. - Links, custom anchors, nav wiring, and frontmatter check out. - The `.md` export on the preview shows each TOML example followed by its JSONC version. - `test-the-docs` deferred: the changed fences are config snippets, and `ConfigFormats.test.ts` already converts every tagged example. No procedural steps changed apart from the `config convert` and `config validate` commands on the new page. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
235 lines
10 KiB
Plaintext
235 lines
10 KiB
Plaintext
---
|
|
id: 'functions-secrets'
|
|
title: 'Environment variables'
|
|
description: 'Set, read, and deploy Edge Function secrets.'
|
|
subtitle: 'Store API keys and other secrets where your Edge Functions can read them.'
|
|
tocVideo: 'EQ5h3NSWMbk'
|
|
---
|
|
|
|
Local development and production load secrets differently, so set them in both.
|
|
|
|
- [Local secrets](#local-secrets) and [Production secrets](#production-secrets) have the steps for each environment.
|
|
- [Accessing environment variables](#accessing-environment-variables) shows how to read a secret from your function code. [When your function can't read a secret](#when-your-function-cant-read-a-secret) covers the local case where nothing arrives.
|
|
- [Reference](#reference) explains which file feeds which runtime, and lists the variables Supabase injects for you.
|
|
|
|
## Local secrets
|
|
|
|
Locally, Edge Functions read secrets from `supabase/functions/.env`. The local stack loads that file on `supabase start`.
|
|
|
|
1. Create `supabase/functions/.env` and add each secret with the value you want the function to read. A `.env.example` template isn't enough, because the runtime reads the values rather than the variable names.
|
|
|
|
```bash
|
|
# supabase/functions/.env
|
|
STRIPE_SECRET_KEY=sk_test_...
|
|
```
|
|
|
|
2. Add the file to your `.gitignore`, along with every other env file you create. A `.env` file committed to Git exposes every secret in it to anyone who can read the repository.
|
|
|
|
```bash
|
|
# .gitignore
|
|
supabase/functions/.env
|
|
.env.local
|
|
```
|
|
|
|
3. Create the function, then replace its contents to read the secret and report whether it arrived. Return the result of the check rather than the value, so the response never carries the secret.
|
|
|
|
```bash
|
|
supabase functions new hello-world
|
|
```
|
|
|
|
```ts
|
|
// supabase/functions/hello-world/index.ts
|
|
Deno.serve(() => {
|
|
const secretKey = Deno.env.get('STRIPE_SECRET_KEY')
|
|
return Response.json({ configured: Boolean(secretKey) })
|
|
})
|
|
```
|
|
|
|
4. Start the local stack.
|
|
|
|
```bash
|
|
supabase start
|
|
```
|
|
|
|
5. Call the function. When `configured` comes back `true`, the runtime handed the secret to your function.
|
|
|
|
```bash
|
|
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello-world' \
|
|
--header 'apikey: <SUPABASE_PUBLISHABLE_KEY>'
|
|
```
|
|
|
|
Your function now reads the secret from your local environment.
|
|
|
|
---
|
|
|
|
## Production secrets
|
|
|
|
Set secrets for your production Edge Functions in the Supabase Dashboard or with the [Supabase CLI](/docs/guides/local-development/cli/getting-started).
|
|
|
|
Creating or deleting a production secret requires the Owner or Administrator role. Developers can view secrets but not change them. See [Access control](/docs/guides/platform/access-control#edge-config-permissions) for the full matrix.
|
|
|
|
A secret name can't start with `SUPABASE_`. That prefix is reserved for the variables Supabase injects, and both the Dashboard and the Management API reject it.
|
|
|
|
### Using the Dashboard
|
|
|
|
1. Open [Edge Function Secrets](/dashboard/project/_/functions/secrets) in the Dashboard.
|
|
2. Enter the **Key** and **Value** for your secret, then click **Save**.
|
|
|
|
<Image
|
|
alt='The Edge Function Secrets page in the Supabase Dashboard. An Add new secrets card holds a Key field whose placeholder reads "e.g. CLIENT_KEY" and a Value field with a reveal toggle and a remove button, above an Add another button and a Save button.'
|
|
src={{
|
|
light: '/docs/img/edge-functions-secrets--light.jpg',
|
|
dark: '/docs/img/edge-functions-secrets.jpg',
|
|
}}
|
|
width={3757}
|
|
height={1525}
|
|
/>
|
|
|
|
You can paste multiple secrets at once.
|
|
|
|
### Using the CLI
|
|
|
|
1. Create a `.env` file with the secrets you want to deploy, and add it to your `.gitignore` before you commit.
|
|
|
|
```bash
|
|
# .env
|
|
STRIPE_SECRET_KEY=sk_live_...
|
|
```
|
|
|
|
2. Push every secret in the file to your remote project. The command also makes them visible in the Dashboard.
|
|
|
|
```bash
|
|
supabase secrets set --env-file .env
|
|
```
|
|
|
|
`supabase secrets set` also sets production secrets individually, without a `.env` file.
|
|
|
|
```bash
|
|
supabase secrets set STRIPE_SECRET_KEY=sk_live_...
|
|
```
|
|
|
|
List the secrets set on your remote project:
|
|
|
|
```bash
|
|
supabase secrets list
|
|
```
|
|
|
|
<Admonition type="note">
|
|
|
|
Your functions read a new secret immediately, so you don't need to redeploy.
|
|
|
|
</Admonition>
|
|
|
|
Your deployed functions can now read the secret.
|
|
|
|
---
|
|
|
|
## Accessing environment variables
|
|
|
|
Read an environment variable with `Deno.env.get`, passing the name of the variable.
|
|
|
|
```js
|
|
Deno.env.get('NAME_OF_SECRET')
|
|
```
|
|
|
|
### In an Edge Function
|
|
|
|
Inside an Edge Function, the Supabase keys are already in the environment. Read them and pass them to `createClient`:
|
|
|
|
```ts
|
|
import { createClient } from 'npm:@supabase/supabase-js@2'
|
|
|
|
const SUPABASE_PUBLISHABLE_KEYS = JSON.parse(Deno.env.get('SUPABASE_PUBLISHABLE_KEYS')!)
|
|
|
|
// For user-facing operations (respects Row Level Security)
|
|
const supabase = createClient(
|
|
Deno.env.get('SUPABASE_URL')!,
|
|
// To use a different API key, change 'default' to your preferred key name
|
|
SUPABASE_PUBLISHABLE_KEYS['default']
|
|
)
|
|
|
|
const SUPABASE_SECRET_KEYS = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)
|
|
// For admin operations (bypasses Row Level Security)
|
|
const supabaseAdmin = createClient(
|
|
Deno.env.get('SUPABASE_URL')!,
|
|
// To use a different API key, change 'default' to your preferred key name
|
|
SUPABASE_SECRET_KEYS['default']
|
|
)
|
|
```
|
|
|
|
### In a Deno script
|
|
|
|
A Deno script you run yourself, outside `supabase functions serve`, doesn't read `supabase/functions/.env`. Pass the file with `--env-file`, and grant the script access to environment variables with `--allow-env`:
|
|
|
|
```bash
|
|
deno run --allow-env --env-file=supabase/functions/.env script.ts
|
|
```
|
|
|
|
Or set the variable for a single command:
|
|
|
|
```bash
|
|
STRIPE_SECRET_KEY=sk_test_... deno run --allow-env script.ts
|
|
```
|
|
|
|
---
|
|
|
|
## When your function can't read a secret
|
|
|
|
A variable that comes back empty usually means the value never reached the runtime.
|
|
|
|
Restart the stack, or serve the function with the file passed explicitly:
|
|
|
|
```bash
|
|
supabase functions serve hello-world --env-file supabase/functions/.env
|
|
```
|
|
|
|
If the value still doesn't arrive, confirm you edited the file your runtime reads.
|
|
|
|
---
|
|
|
|
## Reference
|
|
|
|
Look up which file feeds which runtime, and which variables Supabase injects for you.
|
|
|
|
### Where local values come from
|
|
|
|
A project can hold more than one file that feeds local environment variables, and they aren't interchangeable:
|
|
|
|
- `supabase/functions/.env` is the one your Edge Functions read, loaded when the stack starts.
|
|
- A file you name yourself, such as `.env.local`, is read only when you pass it to `supabase functions serve` with `--env-file`.
|
|
- A `.env` at the root of your project is the one your config file reads, through its `env()` function. See [Using secrets inside the config file](/docs/guides/local-development/managing-config#using-secrets-inside-configtoml). A variable your function needs also has to be in `supabase/functions/.env`, even when the root file already holds the same value.
|
|
|
|
You can also set local values directly in your config file (`supabase/config.toml` or `supabase/config.jsonc`), under `edge_runtime.secrets`:
|
|
|
|
```toml supabase/config.toml
|
|
[edge_runtime.secrets]
|
|
STRIPE_SECRET_KEY = "env(STRIPE_SECRET_KEY)"
|
|
```
|
|
|
|
### Default secrets
|
|
|
|
Alongside the secrets you set yourself, Edge Functions have access to these by default:
|
|
|
|
| Variable | Description |
|
|
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| `SUPABASE_URL` | The API gateway for your Supabase project. |
|
|
| `SUPABASE_DB_URL` | The URL for your Postgres database. Use it to connect directly to your database. |
|
|
| `SUPABASE_PUBLISHABLE_KEYS` | The `publishable` keys JSON dictionary for your Supabase API. Safe to use in a browser when you have Row Level Security enabled. |
|
|
| `SUPABASE_SECRET_KEYS` | The `secret` keys JSON dictionary for your Supabase API. These keys bypass Row Level Security, so use them in Edge Functions and **never** in a browser. |
|
|
| `SUPABASE_JWKS` | The JSON Web Key Set used to verify user JWTs. Same value served at `https://<project-ref>.supabase.co/auth/v1/.well-known/jwks.json`. |
|
|
|
|
Legacy keys:
|
|
|
|
| Variable | Description |
|
|
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| `SUPABASE_ANON_KEY` | The `anon` key for your Supabase API. Safe to use in a browser when you have Row Level Security enabled. |
|
|
| `SUPABASE_SERVICE_ROLE_KEY` | The `service_role` key for your Supabase API. This key bypasses Row Level Security, so use it in Edge Functions and **never** in a browser. |
|
|
|
|
In a hosted environment, functions also have access to these variables:
|
|
|
|
| Variable | Description |
|
|
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
| `SB_REGION` | The region the function was invoked in. |
|
|
| `SB_EXECUTION_ID` | A UUID for the function instance, or [isolate](/docs/guides/functions/architecture#4-execution-mechanics-fast-and-isolated). |
|
|
| `DENO_DEPLOYMENT_ID` | The version of the function code, formatted as `{project_ref}_{function_id}_{version}`. |
|