mirror of
https://github.com/supabase/supabase.git
synced 2026-10-14 21:59:16 +03:00
* Update Environment Variables docs * Update Dependencies docs * Add Function Configuration d ocs * Add custom Configuration section in Navigation * Add Error Handling section * Add new HTTP Methods section * Update Deno 2 Guide * Update nav * Rm deno2 nav * Update Deploy section * Update CI/CD section * Update Logging * Update Testing * Update Local Debugging * Update Troubleshooting * Update Nav * Update CLI Quickstart * Update Dashboard Quickstart * Update Development Environment * Update Nav * Fix links * Update apps/docs/content/guides/functions/quickstart-dashboard.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/development-environment.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/development-environment.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/quickstart.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Use $CodeSample for multi-root example * Use tocVideo instead of video embed * Rm typo * Switch Quickstart ordering * Rephrase Docker requirement for deployment * Add Project Structure section * style: fix docs lint issues * Update Regional Invocations * Update Error Handing * Fix linting * Fixes * Remove duplicate sections * Update Auth integration * Update Storage * Update Database/Postgres * Update Nav * Update Background Tasks * Update Ephemeral Storage * Update Websockets * Update Routing * Update wasm * Update AI models * Fix linting issues * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/storage-caching.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/websockets.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/websockets.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Rm unnecessary lines * Rm wrong example * Fix typo * Rm deno2 * Move CICD to deployment and rename routing * Fix whitespace * rm unnecessary spelling --------- Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
144 lines
4.0 KiB
Plaintext
144 lines
4.0 KiB
Plaintext
---
|
||
id: 'functions-secrets'
|
||
title: 'Environment Variables'
|
||
description: 'Managing secrets and environment variables.'
|
||
subtitle: 'Manage sensitive data securely across environments.'
|
||
---
|
||
|
||
## Default secrets
|
||
|
||
Edge Functions have access to these secrets by default:
|
||
|
||
- `SUPABASE_URL`: The API gateway for your Supabase project
|
||
- `SUPABASE_ANON_KEY`: The `anon` key for your Supabase API. This is 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 is safe to use in Edge Functions, but it should NEVER be used in a browser. This key will bypass Row Level Security
|
||
- `SUPABASE_DB_URL`: The URL for your Postgres database. You can use this to connect directly to your database
|
||
|
||
---
|
||
|
||
## Accessing environment variables
|
||
|
||
You can access environment variables using Deno's built-in handler, and passing it the name of the environment variable you’d like to access.
|
||
|
||
```js
|
||
Deno.env.get('NAME_OF_SECRET')
|
||
```
|
||
|
||
For example, in a function:
|
||
|
||
```js
|
||
import { createClient } from 'npm:@supabase/supabase-js@2'
|
||
|
||
// For user-facing operations (respects RLS)
|
||
const supabase = createClient(
|
||
Deno.env.get('SUPABASE_URL')!,
|
||
Deno.env.get('SUPABASE_ANON_KEY')!
|
||
)
|
||
|
||
// For admin operations (bypasses RLS)
|
||
const supabaseAdmin = createClient(
|
||
Deno.env.get('SUPABASE_URL')!,
|
||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
|
||
)
|
||
```
|
||
|
||
---
|
||
|
||
### Local secrets
|
||
|
||
In development, you can load environment variables in two ways:
|
||
|
||
1. Through an `.env` file placed at `supabase/functions/.env`, which is automatically loaded on `supabase start`
|
||
2. Through the `--env-file` option for `supabase functions serve`. This allows you to use custom file names like `.env.local` to distinguish between different environments.
|
||
|
||
```bash
|
||
supabase functions serve --env-file .env.local
|
||
```
|
||
|
||
<Admonition type="caution">
|
||
|
||
Never check your `.env` files into Git! Instead, add the path to this file to your `.gitignore`.
|
||
|
||
</Admonition>
|
||
|
||
We can automatically access the secrets in our Edge Functions through Deno’s handler
|
||
|
||
```tsx
|
||
const secretKey = Deno.env.get('STRIPE_SECRET_KEY')
|
||
```
|
||
|
||
Now we can invoke our function locally. If you're using the default `.env` file at `supabase/functions/.env`, it's automatically loaded:
|
||
|
||
```bash
|
||
supabase functions serve hello-world
|
||
```
|
||
|
||
Or you can specify a custom `.env` file with the `--env-file` flag:
|
||
|
||
```bash
|
||
supabase functions serve hello-world --env-file .env.local
|
||
```
|
||
|
||
This is useful for managing different environments (development, staging, etc.).
|
||
|
||
---
|
||
|
||
### Production secrets
|
||
|
||
You will also need to set secrets for your production Edge Functions. You can do this via the Dashboard or using the CLI.
|
||
|
||
**Using the Dashboard**:
|
||
|
||
1. Visit [Edge Function Secrets Management](https://supabase.com/dashboard/project/_/settings/functions) page in your Dashboard.
|
||
2. Add the Key and Value for your secret and press Save
|
||
|
||
<Image
|
||
alt="Edge Functions Secrets Management"
|
||
src={{
|
||
light: '/docs/img/edge-functions-secrets--light.jpg',
|
||
dark: '/docs/img/edge-functions-secrets.jpg',
|
||
}}
|
||
/>
|
||
|
||
Note that you can paste multiple secrets at a time.
|
||
|
||
**Using the CLI**
|
||
|
||
You can create a `.env` file to help deploy your secrets to production
|
||
|
||
```bash
|
||
# .env
|
||
STRIPE_SECRET_KEY=sk_live_...
|
||
```
|
||
|
||
<Admonition type="caution">
|
||
|
||
Never check your `.env` files into Git! Instead, add the path to this file to your `.gitignore`.
|
||
|
||
</Admonition>
|
||
|
||
You can push all the secrets from the `.env` file to your remote project using `supabase secrets set`. This makes the environment visible in the dashboard as well.
|
||
|
||
```bash
|
||
supabase secrets set --env-file .env
|
||
```
|
||
|
||
Alternatively, this command also allows you to set production secrets individually rather than storing them in a `.env` file.
|
||
|
||
```bash
|
||
supabase secrets set STRIPE_SECRET_KEY=sk_live_...
|
||
```
|
||
|
||
To see all the secrets which you have set remotely, you can use `supabase secrets list`
|
||
|
||
```bash
|
||
supabase secrets list
|
||
```
|
||
|
||
<Admonition type="note">
|
||
|
||
You don't need to re-deploy after setting your secrets. They're available immediately in your
|
||
functions.
|
||
|
||
</Admonition>
|