0

I am trying to deploy a FastAPI application on Vercel, but I am having trouble getting environment variables to load. My setup works perfectly fine locally, where I use python-dotenv with load_dotenv() to load the .env file, but on Vercel, the environment variables are not being found at runtime.

Here are the steps I've taken so far:

  1. Added environment variables in the Vercel dashboard under Settings > Environment Variables.
  2. Verified that the app works locally with the same .env file using load_dotenv().
  3. Deployed the app to Vercel, but it fails to find the environment variables.
  4. Tried changing the Vercel Node.js version to 18.x (based on suggestions from other threads), but this caused my application to stop working entirely.

Additional Details:

  1. My application uses Python 3.11 and FastAPI.
  2. My environment variables include SECRET_KEY and DATABASE_URL.

Questions:

  1. Is there a specific configuration required to make Python-based apps load environment variables correctly on Vercel?
  2. Does Vercel require a particular runtime configuration to support Python 3.11 and environment variables together?
  3. Has anyone successfully run a FastAPI app on Vercel with environment variables recently?

Any guidance or examples would be greatly appreciated!

Thank you in advance for your help.

2 Answers 2

0

you need to use os.environ.get("< KEY >") to get the key-value in env. it is also mentioned in Vercel Documentation. it looks like versel add the env pair during the run time.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, you're correct, and I am already doing that. Here's my current setup: ` class Settings(BaseSettings): API_V1_STR: str = "/api/v1" SUPABASE_URL: str = Field(default_factory=lambda: os.environ.get("SUPABASE_URL")) SUPABASE_KEY: str = Field(default_factory=lambda: os.environ.get("SUPABASE_KEY")) ` It seems there might be something else causing the issue, but I haven't identified it yet. Any additional insights would be greatly appreciated.
0

In my case, I misspelled the variable name in the Vercel settings. Try to check it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.