How to Hide API Keys in Python: An Environment Variables Example

Поділитися
Вставка
  • Опубліковано 11 гру 2024

КОМЕНТАРІ • 13

  • @BillYovino
    @BillYovino Рік тому +2

    Thanks for this. I've seen other references to env variables but your instructions are clear and concise.

    • @PuppyCoding
      @PuppyCoding  Рік тому +2

      You're welcome Bill. Very happy to hear that it was useful.

  • @CuriousJayDiscover
    @CuriousJayDiscover Рік тому +3

    So useful and beginner- friendly, thanks for sharing.

    • @CuriousJayDiscover
      @CuriousJayDiscover Рік тому +2

      I have tried your method of setting .env, it worked well. Thanks again. 👍

    • @PuppyCoding
      @PuppyCoding  Рік тому +1

      @@CuriousJayDiscover Oh, I'm really happy to hear that. Glad it worked and thanks for the kind words!

  • @pinkflower03
    @pinkflower03 11 місяців тому +1

    Sir, was dotenv depends on the name of your parent folder?

  • @joydeepsarkar3092
    @joydeepsarkar3092 10 місяців тому

    Very helpful!

  • @deepali4149
    @deepali4149 Рік тому +3

    Hi, if I have my code deployed on a server from a repository, will hiding my api key using this method so that it is not visible in the repo affect anything?

    • @PuppyCoding
      @PuppyCoding  Рік тому +2

      That's a good question. Firstly, if you're uploading your code to GitHub, make sure you add ".env" to your ".gitignore" file to prevent it being accidentally committed to your repo. (You probably know that already but I'm mentioning it just in case.)
      Regarding deploying your code to a server, don't use a ".env" file. Instead, set environment variables directly on the server using your hosting platform's built-in method (I've listed some common ones below). If your host doesn't allow that, you can set environment variables using the command line (export API_KEY="my-api-key") or in a shell startup file. Hope that helps.
      - AWS EC2: You can set environment variables in the user data script when launching an instance, or use AWS Systems Manager Parameter Store or AWS Secrets Manager for more sensitive data.
      - AWS Lambda: You can set environment variables in the AWS Lambda console, AWS CLI, or AWS SDKs.
      - Heroku: You can use the Heroku CLI or the Heroku dashboard to set config vars.
      - Docker: You can set environment variables using the `-e` flag when starting a container, or by including the `ENV` command in your Dockerfile.
      - Kubernetes: You can set environment variables in your pod's YAML configuration file.

    • @deepali4149
      @deepali4149 Рік тому +2

      @@PuppyCoding Thank you for the response!

    • @PuppyCoding
      @PuppyCoding  Рік тому +1

      @@deepali4149 You're welcome!

    • @PuppyCoding
      @PuppyCoding  Рік тому +3

      @@deepali4149 Hi again. Just letting you know that I made a quick video to answer your question in a bit more detail: ua-cam.com/video/WhLyrmp1AY4/v-deo.html

    • @deepali4149
      @deepali4149 Рік тому +2

      @@PuppyCoding thanks… I’ll check it out :)