Fastapi: [QUESTION] - How do you manage enviroment variables, and secrets in fastAPI

Created on 9 Mar 2020  路  4Comments  路  Source: tiangolo/fastapi

Just wanted to get any good practice how you deal with enviroment variables, and secrets like database informations(or anything else that is considered as secret).
I am new to this, and I want to get some advice from developers with experience on this case.
And please if you give me some help, that could be great(if possible) with a small explaination(source code).

Thank to this awesome community in advance.

question

Most helpful comment

took me a while to understand how to manage that properly, here is my way of managing them:

  • create a .env file in a /settings folder (git ignore this file), where I put all my variables stored in a format my_variable = "value"
  • use pydantic as said above which relies on python-dotenv, specifying my .env file and a class "Settings" with all environment variables which I can use everywhere needed by importing settings
  • in my cicd (i'm using gitlab), I create my environment variables with an "APP_" prefix for all the variables which I'd like to have accessible in my app, and I use the scope from gitlab to determine whether the values are for staging or production branch
  • during my cicd, I generate a .env file which exports all my env variables stored within gitlab and available on my project and then keep only the one with "APP_" prefix, removing the prefix. This gives this (I need also to replace "declare -x " because it's added during the export due to the image i'm using, so it may depend on your case):
    export | grep APP_ | sed -e 's/APP_//g' | sed -e 's/declare -x //g' > ./app/settings/.env

and voil脿, nothing complicated for me

P.S. I'm not Facebook nor Google so it might be not top of the top but for my needs this is very easy to manage and secure enough.

Hope it helps

All 4 comments

I'd like to point you in a couple of directions:

Is this a good start for you?

Also, take a look at dynaconf and decouple

took me a while to understand how to manage that properly, here is my way of managing them:

  • create a .env file in a /settings folder (git ignore this file), where I put all my variables stored in a format my_variable = "value"
  • use pydantic as said above which relies on python-dotenv, specifying my .env file and a class "Settings" with all environment variables which I can use everywhere needed by importing settings
  • in my cicd (i'm using gitlab), I create my environment variables with an "APP_" prefix for all the variables which I'd like to have accessible in my app, and I use the scope from gitlab to determine whether the values are for staging or production branch
  • during my cicd, I generate a .env file which exports all my env variables stored within gitlab and available on my project and then keep only the one with "APP_" prefix, removing the prefix. This gives this (I need also to replace "declare -x " because it's added during the export due to the image i'm using, so it may depend on your case):
    export | grep APP_ | sed -e 's/APP_//g' | sed -e 's/declare -x //g' > ./app/settings/.env

and voil脿, nothing complicated for me

P.S. I'm not Facebook nor Google so it might be not top of the top but for my needs this is very easy to manage and secure enough.

Hope it helps

Thanks for the help here everyone! :cake:

Thanks @vjanz for closing the issue.

You might also want to check the new docs for settings and env vars: https://fastapi.tiangolo.com/advanced/settings/

Was this page helpful?
0 / 5 - 0 ratings