mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem When a function runs locally with `supabase start` or `supabase functions serve`, calls to `localhost` or `127.0.0.1` inside it fail with "Connection refused". The Edge Functions development environment page didn't say that the function runs in a Docker container, or how to reach the local Supabase services and servers on the reader's machine. Related to [FDBKIN-6695](https://linear.app/supabase/issue/FDBKIN-6695/document-how-local-edge-functions-can-call-services-on-the-host) Closes [DOCS-1853](https://linear.app/supabase/issue/DOCS-1853/explain-how-a-local-edge-function-reaches-the-local-stack-and-services) ## Solution Adds a "Reach other services from a local function" section to the Edge Functions development environment page. It explains that `localhost` inside a local function means the container, points to `SUPABASE_URL` (and `SUPABASE_DB_URL`) for the local Supabase services, and shows `host.docker.internal:<port>` for a server on the host, including the Linux requirement to listen on `0.0.0.0`. Under `supabase start`, `SUPABASE_DB_URL` uses a `supabase_db_` host that some clients, such as Postgres.js, can't resolve, so the section says to swap that host for `db`. A one-line `.env` example keeps the host URL in a local secret, so deployed code uses a production secret instead. It also drops the claim under `supabase start` that local development is the exact same environment as production, which the new section contradicts. The Postgres tab note on the CLI getting-started page told readers to reach the database from a function through `host.docker.internal`. It now says a local function can't reach the database at that URL, and links to the new section, so both pages give the same advice. On the Drizzle page, the garbled note in the "Connect" step now says the `db.ts` sample swaps the `supabase_db_` host for `db` in a local Edge Function, and links to the new section. The same step now links the **Connect** panel straight to the **Transaction pooler** URI, since the panel has no "Shared pooler" option. The `db.ts` comment now says `prepare: false` disables prepared statements, not prefetch. The page gets a task title and a description that say what it helps the reader do. Smaller fixes: the password instruction is plainer, "connection pooler" is lowercase, a double space is gone, and "Related resources" is in sentence case. The Prisma and Postgres.js quickstarts had the same bare title and "Quickstart" description, so they get task titles and descriptions too, and the three ORM quickstarts now match. The Postgres.js page uses "Query" rather than "Connect to", because "Connect to Postgres with Postgres.js" is over the style guide's 35-character title limit. ## How to verify 1. Open https://docs-git-tedd-docs-1853-supabase.vercel.app/docs/guides/functions/development-environment#reach-other-services-from-a-local-function, https://docs-git-tedd-docs-1853-supabase.vercel.app/docs/guides/local-development/cli/getting-started (the **Postgres** tab under "Access your project's services"), https://docs-git-tedd-docs-1853-supabase.vercel.app/docs/guides/database/drizzle (step 3, "Connect"), https://docs-git-tedd-docs-1853-supabase.vercel.app/docs/guides/database/prisma, and https://docs-git-tedd-docs-1853-supabase.vercel.app/docs/guides/database/postgres-js. 2. The section states that a locally served function runs in a Docker container, so `localhost` and `127.0.0.1` in function code mean the container. 3. Under "Reach the local Supabase services", the section says to use `SUPABASE_URL` for the local Supabase services, notes it is `http://kong:8000` locally, and links to the default secrets table on the secrets page. It says to swap a `supabase_db_` host in `SUPABASE_DB_URL` for `db` for clients that can't resolve it. 4. The section says to reach a server on the host through `host.docker.internal:<port>`, and that on Linux the server must listen on `0.0.0.0`. 5. The example sets `API_URL` in `supabase/functions/.env`. The local secrets and production secrets links open the matching sections of the secrets page. 6. The `supabase start` paragraph no longer claims the local environment is identical to production. 7. The getting-started Postgres tab note no longer mentions `host.docker.internal`, and its link opens the "Reach the local Supabase services" section. 8. On the Drizzle page, step 3 says the `db.ts` sample swaps the `supabase_db_` host for `db` in a local Edge Function, and links to "Reach the local Supabase services". The **Connect** panel link opens the **Direct** tab with **Transaction pooler** selected. The `db.ts` comment mentions prepared statements, and the last heading reads "Related resources". 9. The Drizzle page's title reads "Connect to Postgres with Drizzle", the Prisma page's reads "Connect to Postgres with Prisma", and the Postgres.js page's reads "Query Postgres with Postgres.js". The sidebar keeps its existing labels for all three. ## Checks Ran `pnpm exec oxfmt --check` on the changed files. Typechecked the snippet with `tsc --strict` (with a `Deno` declaration); it was also run with `deno check`, `deno lint`, and served with `supabase functions serve` on Linux, where `localhost` and a `127.0.0.1`-bound host server were refused and `host.docker.internal` to a `0.0.0.0`-bound server returned 200. Under `supabase start`, Postgres.js failed to resolve the `supabase_db_` host in `SUPABASE_DB_URL` and connected with the host set to `db`, and `localhost:54322` from a function was refused. The Drizzle `db.ts` host check was run under `supabase start` with a project ID that contains an underscore: it rewrote the host to `db` and a Drizzle query succeeded. Checked `SUPABASE_URL=http://kong:8000`, `SUPABASE_DB_URL`, and the Linux `host.docker.internal:host-gateway` mapping against `supabase/cli` (`apps/cli/src/shared/functions/serve.ts`, `apps/cli/src/commands/start/start.handler.ts`, `apps/cli/src/commands/start/services/edge-runtime.service.ts`). Checked the **Connect** panel labels and its `connectTab` and `method` query parameters against `apps/studio/components/interfaces/ConnectSheet`, and `prepare: false` against the Postgres.js source. The ORM quickstart titles and descriptions follow the style guide's length limits, and each description matches its page's steps. The navigation sets its own labels, and no nav entry, breadcrumb, or test reads the old titles. ESLint ignores MDX files, and the docs build was not run in the sandbox. ## Follow-ups - `apps/docs/content/guides/database/drizzle.mdx`: the `db.ts` sample is written for Node (`import 'dotenv/config'`, `process.env.DATABASE_URL`), but its `supabase_db_` host check only applies inside a Deno Edge Function under `supabase start`. A reader in either runtime gets a sample that half fits. - `apps/docs/content/guides/database/drizzle.mdx`: the page has no value statement and no next steps. It opens straight into the gerund heading "Connecting with Drizzle" and ends after the connection step. Step 1 installs `drizzle-kit`, but no step uses it, so the reader can't create the `users` table from `schema.ts`. ## Checklist - [x] I have read the [Contributing Guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] I have checked that the change works in the preview deployment ## Feedback for Tedd To ask Tedd for a change to this PR, reply in Tedd's session on [DOCS-1853](https://linear.app/supabase/issue/DOCS-1853/explain-how-a-local-edge-function-reaches-the-local-stack-and-services). Tedd doesn't read comments on this PR. --------- Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> Co-authored-by: alexisintech <98043211+alexisintech@users.noreply.github.com>
142 lines
6.2 KiB
Plaintext
142 lines
6.2 KiB
Plaintext
---
|
||
id: 'development-environment'
|
||
title: 'Development Environment'
|
||
description: 'Get the best Edge Functions experience with the right local developer environment.'
|
||
subtitle: 'Set up your local development environment for Edge Functions.'
|
||
tocVideo: 'EQ5h3NSWMbk'
|
||
---
|
||
|
||
<Admonition type="note">
|
||
|
||
Before getting started, make sure you have the Supabase CLI installed. Check out the [CLI installation guide](/docs/guides/local-development) for installation methods and troubleshooting.
|
||
|
||
</Admonition>
|
||
|
||
---
|
||
|
||
## Step 1: Install Deno CLI
|
||
|
||
The [Supabase CLI](/docs/guides/local-development/cli/getting-started) doesn't use the standard Deno CLI to serve functions locally. Instead, it uses its own Edge Runtime to keep the development and production environment consistent.
|
||
|
||
You can follow the [Deno guide](https://docs.deno.com/runtime/getting_started/setup_your_environment/) for setting up your development environment with your favorite editor/IDE.
|
||
|
||
The benefit of installing Deno separately is that you can use the Deno LSP to improve your editor's autocompletion, type checking, and testing. You can also use Deno's built-in tools such as `deno fmt`, `deno lint`, and `deno test`.
|
||
|
||
After installing, you should have Deno installed and available in your terminal. Verify with `deno --version`
|
||
|
||
---
|
||
|
||
## Step 2: Set up your editor
|
||
|
||
Set up your editor environment for proper TypeScript support, autocompletion, and error detection.
|
||
|
||
### VSCode/Cursor (recommended)
|
||
|
||
1. **Install the Deno extension** from the VSCode marketplace
|
||
2. **Option 1: Auto-generate (easiest)**
|
||
When running `supabase init`, select `y` when prompted "Generate VS Code settings for Deno? [y/N]"
|
||
3. **Option 2: Manual setup**
|
||
|
||
Create a `.vscode/settings.json` in your project root:
|
||
|
||
```json
|
||
{
|
||
"deno.enablePaths": ["./supabase/functions"],
|
||
"deno.importMap": "./supabase/functions/deno.json"
|
||
}
|
||
```
|
||
|
||
This configuration enables the Deno language server only for the `supabase/functions` folder, while using VSCode's built-in JavaScript/TypeScript language server for all other files.
|
||
|
||
---
|
||
|
||
### Multi-root workspaces
|
||
|
||
The standard `.vscode/settings.json` setup works perfectly for projects where your Edge Functions live alongside your main application code. However, you might need multi-root workspaces if your development setup involves:
|
||
|
||
- **Multiple repositories:** Edge Functions in one repo, main app in another
|
||
- **Microservices:** Several services you need to develop in parallel
|
||
|
||
For this development workflow, create `edge-functions.code-workspace`:
|
||
|
||
<$CodeSample
|
||
path="/edge-functions/edge-functions.code-workspace"
|
||
meta="edge-functions.code-workspace"
|
||
language="json"
|
||
/>
|
||
|
||
You can find the complete example on [GitHub](https://github.com/supabase/supabase/tree/master/examples/edge-functions).
|
||
|
||
---
|
||
|
||
## Recommended project structure
|
||
|
||
It's recommended to organize your functions according to the following structure:
|
||
|
||
```bash
|
||
└── supabase
|
||
├── functions
|
||
│ ├── deno.json # Top-level Deno configuration
|
||
│ ├── _shared # Shared code (underscore prefix)
|
||
│ │ ├── supabaseAdmin.ts # Supabase client with SECRET key
|
||
│ │ ├── supabaseClient.ts # Supabase client with PUBLISHABLE key
|
||
│ │ └── cors.ts # Reusable CORS headers
|
||
│ ├── function-one # Use hyphens for function names
|
||
│ │ └── index.ts
|
||
│ └── function-two
|
||
│ └── index.ts
|
||
├── tests
|
||
│ ├── function-one-test.ts
|
||
│ └── function-two-test.ts
|
||
├── migrations
|
||
└── config.toml
|
||
```
|
||
|
||
- **Use "fat functions"**. Develop few, large functions by combining related functionality. This minimizes cold starts.
|
||
- **Name functions with hyphens (`-`)**. This is the most URL-friendly approach
|
||
- **Store shared code in `_shared`**. Store any shared code in a folder prefixed with an underscore (`_`).
|
||
- **Separate tests**. Use a separate folder for [Unit Tests](/docs/guides/functions/unit-test) that includes the name of the function followed by a `-test` suffix.
|
||
|
||
---
|
||
|
||
## Essential CLI commands
|
||
|
||
Get familiar with the most commonly used CLI commands for developing and deploying Edge Functions.
|
||
|
||
### `supabase start`
|
||
|
||
This command starts your entire Supabase stack locally: database, auth, storage, and Edge Functions runtime.
|
||
|
||
### `supabase functions serve [function-name]`
|
||
|
||
Develop a specific function with hot reloading. Your functions run at `http://localhost:54321/functions/v1/[function-name]`. When you save your file, you’ll see the changes instantly without having to wait.
|
||
|
||
Alternatively, use `supabase functions serve` to serve all functions at once. If functions fail to serve locally, see [Issues serving Edge Functions locally](/kb/troubleshooting/issues-serving-edge-functions-locally).
|
||
|
||
### `supabase functions deploy hello-world`
|
||
|
||
Deploy the function when you’re ready
|
||
|
||
---
|
||
|
||
## Reach other services from a local function
|
||
|
||
A local function runs in a Docker container, so `localhost` and `127.0.0.1` in function code mean the container, not your machine. A request to them fails with "Connection refused" or times out.
|
||
|
||
### Reach the local Supabase services
|
||
|
||
Use `SUPABASE_URL` to reach Auth, the Database REST API, Storage, and the other local services. Locally it holds the gateway's internal address, `http://kong:8000`, so don't hardcode `http://127.0.0.1:54321`. For a direct database connection, use `SUPABASE_DB_URL`. Both are [default secrets](/docs/guides/functions/secrets#default-secrets).
|
||
|
||
Under `supabase start`, `SUPABASE_DB_URL` has a host that starts with `supabase_db_`, which some clients, such as Postgres.js, can't resolve. Replace it with `db` for those clients.
|
||
|
||
### Call a server on your machine
|
||
|
||
Use `host.docker.internal` with the server's port, such as `http://host.docker.internal:3000`. On Linux, the server must listen on `0.0.0.0`, because a server bound to `127.0.0.1` refuses requests from the container.
|
||
|
||
Keep the URL in a [local secret](/docs/guides/functions/secrets#local-secrets) so your deployed function doesn't call it, and set the public URL as a [production secret](/docs/guides/functions/secrets#production-secrets):
|
||
|
||
```bash
|
||
# supabase/functions/.env
|
||
API_URL=http://host.docker.internal:3000
|
||
```
|