Skip to content

How to hide my API key from my web application #151885

Discussion options

You must be logged in to vote

Hi, @grahamj89

To keep your API key secure while still keeping your repository public, you can follow these steps:

  1. Use Environment Variables:

    • Store your API key in environment variables. In Python, you can use the os module to access these variables. This prevents the key from being hardcoded into your application code.
    • Example:
      import os
      api_key = os.getenv('API_KEY')
  2. Add .env File:

    • You can create a .env file in your project root and store the API key there. Use the python-dotenv package to load environment variables from this file.
    • In .env:
      API_KEY=your_api_key_here
      
    • Install python-dotenv:
      pip install python-dotenv
    • Load the .env variables in your app:
      from dotenv import load_d…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@grahamj89
Comment options

Answer selected by grahamj89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Security Build security into your GitHub workflow with features to keep your codebase secure Question
2 participants