Connection works in mjs, not in edge function
I have a js connection setup to the database that works great for me when testing the script, but when I put it into an edge function it fails. The URL and Key are the same in both cases.
What does the function expect that's different from what you can pass in a .mjs script? I'm a javascript newb so there's probably some fundamental difference I'm not aware of. The error is in the screenshot along with the connection script from both places (the one with "try" is from the edge function), and pasted here: "TypeError: error sending request for url (http://127.0.0.1:54321/rest/v1/user_apps?id=eq.2&select=*): client error (Connect): tcp connect error: Connection refused (os error 111): Connection refused (os error 111)\n
at async mainFetch (ext:deno_fetch/26_fetch.js:191:12)\n
at async fetch (ext:deno_fetch/26_fetch.js:465:11)\n
at async postData (file:///var/tmp/sb-compile-edge-runtime/hexnode_app_search/index.ts:33:27)\n
at async Object.handler (file:///var/tmp/sb-compile-edge-runtime/hexnode_app_search/index.ts:74:22)\n
at async mapped (ext:runtime/http.js:231:18)"
What does the function expect that's different from what you can pass in a .mjs script? I'm a javascript newb so there's probably some fundamental difference I'm not aware of. The error is in the screenshot along with the connection script from both places (the one with "try" is from the edge function), and pasted here: "TypeError: error sending request for url (http://127.0.0.1:54321/rest/v1/user_apps?id=eq.2&select=*): client error (Connect): tcp connect error: Connection refused (os error 111): Connection refused (os error 111)\n
at async mainFetch (ext:deno_fetch/26_fetch.js:191:12)\n
at async fetch (ext:deno_fetch/26_fetch.js:465:11)\n
at async postData (file:///var/tmp/sb-compile-edge-runtime/hexnode_app_search/index.ts:33:27)\n
at async Object.handler (file:///var/tmp/sb-compile-edge-runtime/hexnode_app_search/index.ts:74:22)\n
at async mapped (ext:runtime/http.js:231:18)"



35 Replies
could you try in your edge function (just momentarily) to create the client using hardcoded url/anon key similar to the image on the left. This should isolate the issue to either an issue with pulling in environment variables or something else going on in edge functions
instead of environment variables
Are you testing the edge function locally or have you deployed it?
A deployed edge function isn't going to work with 127.0.0..1; in which case, the refused connection would make sense.
The red squiggly type error in the one screenshot might just be indicating that the env var may be undefined, but the function expects a string. You would need to hover over it to see. Might just need to put
|| "" after both of them. I was thinking there are times when only the first will throw the type error, not the second; but if you fix the url one, then the key one will show an error. I could be wrong, just going off memory.Correct, 127.0.01 won't work inside deployed function as it will loop back to container itself. You can use
SUPABASE_URL env var. Its available by default in both local and hosted environment. ANON_KEY and SERVICE_ROLE_KEY is also available
Ah yes, good call on the built-in env; I spaced that.
@ihm40 yes I tried that. Same error.
@j4 here's a screenshot. It seems to be building the url fine (see error screenshot in the first post). Yes I'm testing locally, on the local postgres database hosted by docker.
@inder The SUPABASE_URL variable returns this: "supabaseUrl":"http://kong:8000". If I use that it doesn't return an error but also doesn't interact with the database. When I try using this same address manually in a separate script (not an edge function) I get the error "fetch failed"
@inder The SUPABASE_URL variable returns this: "supabaseUrl":"http://kong:8000". If I use that it doesn't return an error but also doesn't interact with the database. When I try using this same address manually in a separate script (not an edge function) I get the error "fetch failed"

Yes
http://kong:8000 is correct url inside the container. When not running inside container, you still need to use 127.0.0.1 or localhost
What do you mean by not interacting with db? Requests are not being made?
show the function codeI'll modify it to make a simple query and nothing else. Just a sec.
Ok that worked. Interesting. Thanks @inder
Here's the code, and the call and response from the version I was running before, but with the SUPABASE_URL in use. It's not giving an error, but not writing or returning any data. That same exact query statement with the same input data (I think) worked outside an Edge Function (but pointed at the 127... database url).
But the curl request you make is returning the data with correct supabaseUrl and the error is null
Instead of hardcoding urls, rely on env vars.
For example: when you run using deno outside the container
and inside container its already present, so you won't have to make any changes in code
Aha! It was a stupid json parsing error. I had two consecutive levels to parse named the same thing.
@inder the environment variables are working great inside the container.
Outside the container, are you saying use this (screenshot)?
If I do that I get this error:
const SUPABASE_URL='http://127.0.0.1:54321' deno [fileName];
^^^^
SyntaxError: Unexpected identifier 'deno'
Wrong screenshot.

No, I meant keep the code same but when testing outside container run the command I provided above. The command above will pass the SUPABASE_URL as env variable to the script so you won't have to keep dangling comments in your code
This command has to be run in your terminal and will pass SUPABASE_URL as env var
Similarly you can pass anon key as env var from terminal
Are you saying I should add them as local environment variables?
The command gives me this:
$ npx SUPABASE_URL='http://127.0.0.1:54321' deno [fileName]
*npm error code ENOENT
npm error syscall open
npm error path C:\Git\Supabase\supabase\Test Scripts\SUPABASE_URL=http:\127.0.0.1:54321\package.json
npm error errno -4058
npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open 'C:\Git\Supabase\supabase\Test Scripts\SUPABASE_URL=http:\127.0.0.1:54321\package.json'
npm error enoent This is related to npm not being able to find a file.
npm error enoent
npm error A complete log of this run can be found in: C:\Users\JeremiahRobinson\AppData\Local\npm-cache_logs\2025-09-27T16_24_19_914Z-debug-0.log *
But I do have a local .env folder here. Does it need to be moved outside the functions directory?

Do you have deno installed locally?
and in what shell are you running the commands?
I didn't, but I just installed it.
Right now in bash. I also have a js and a powershell.
bash is fine.
You can define
SUPABASE_URL and SUPABASE_ANON_KEY inside .env file as well.
If you run script outside container and have specified env vars in .env file, then you need to run this command (no need for npx)
When you run the below command, you're basically providing env vars from the command line directly instead of reading .env file
But if you have many env vars, then .env file makes senseI don't have a lot right now, but it seems like having a file with them would be nice. Here's what I get when I try that.

It doesn't recognize deno unless I preface with npx.
If you installed deno while this shell was already open, then you will have to restart the shell. Also you need to add the function file name at the end
For example, you have hello world function, first cd into functions directory
I thought you meant to run that "deno run --env-file" and point it at my .env file itself. I tried that in a new shell with the same result.
I just moved the .env file outside the functions directory because I want to use it in scripts that aren't Edge Functions.
Are you saying I should point at each script and run that command?
How did you install deno? downloading it from its website or simply using npx ?
npx
then you'll need to npx
I thought you installed it by downloading its binary

--env-file automatically reads .env file. You can pass file names to it when your env filenames are differentOk. So I should be able to run a script and pull the local filenames automatically? I"ll try that.
For example:
I specified env file inside folder

You'll need to prefix the command with npx as you've installed it that way
Ok. I'm not sure what main.ts is. I don't have one of those but just a bunch of random scripts. Here's what I get when I point at one of them and run that command.

- main.ts just refers to the filename. This will be the script path you want to run.
- Change your SUPABASE_URL to using env var
- You don't need to import Deno. its available globally
Ah ok. You're saying that's how I need to call the file each time. Not just once to define where the local environment variable file is. Ok that works.
If you want to run an individual script without running it inside container then yes you need to run this command
You can add a script inside package.json for convenience
But I'd suggest you run it inside container because there are time and memory limits for functions in hosted environment and those limits are replicated locally
So you'll have better idea how your script will behave on supabase
I really appreciate your help. This got me a lot further along.