<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Hamznabil</title>
    <description>The latest articles on Forem by Hamznabil (@hamznabil).</description>
    <link>https://forem.com/hamznabil</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2102282%2Ff05bc65f-054e-45de-ae05-5458955b6bcf.png</url>
      <title>Forem: Hamznabil</title>
      <link>https://forem.com/hamznabil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hamznabil"/>
    <language>en</language>
    <item>
      <title>Secure API Key Handling in Python Projects</title>
      <dc:creator>Hamznabil</dc:creator>
      <pubDate>Fri, 20 Sep 2024 13:53:32 +0000</pubDate>
      <link>https://forem.com/hamznabil/secure-api-key-handling-in-python-projects-1kg7</link>
      <guid>https://forem.com/hamznabil/secure-api-key-handling-in-python-projects-1kg7</guid>
      <description>&lt;p&gt;A guide to properly managing API keys and environment variables in Python projects&lt;/p&gt;

&lt;h2&gt;
  
  
  📜 Introduction
&lt;/h2&gt;

&lt;p&gt;When working with APIs in Python, you often need to use API keys or other sensitive credentials. It's crucial to &lt;strong&gt;manage these keys securely&lt;/strong&gt; to avoid leaking sensitive information or accidentally committing them to your Git repository.&lt;/p&gt;

&lt;p&gt;For a complete demonstration, check out my GitHub repository &lt;a href="https://github.com/Hamza-nabil/Secure-API-Key-Handling" rel="noopener noreferrer"&gt;Secure-API-Key-Handling&lt;/a&gt;, It features a Streamlit chat app that securely manages API keys using &lt;code&gt;.env&lt;/code&gt; files and the &lt;code&gt;python-dotenv&lt;/code&gt; package while interacting with the Gemini Generative AI model.&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 Getting Started
&lt;/h2&gt;

&lt;p&gt;Follow these steps to set up your project for secure API key handling:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install Dependencies
&lt;/h3&gt;

&lt;p&gt;You'll need the &lt;code&gt;python-dotenv&lt;/code&gt; package to load environment variables from a &lt;code&gt;.env&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Set Up a &lt;code&gt;.env&lt;/code&gt; File
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in your project root, where you'll store your API key and other environment-specific variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# .env&lt;/span&gt;
&lt;span class="nv"&gt;API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your_api_key_here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; This .env file should never be committed to your repository. We’ll configure .gitignore to ensure that.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Add &lt;code&gt;.env&lt;/code&gt; to &lt;code&gt;.gitignore&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Add the following line to your &lt;code&gt;.gitignore&lt;/code&gt; file to ensure that &lt;code&gt;.env&lt;/code&gt; doesn't get pushed to Git:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .gitignore
.env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Provide a &lt;code&gt;.env.example&lt;/code&gt; File
&lt;/h3&gt;

&lt;p&gt;For other developers working on your project, include a &lt;code&gt;.env.example&lt;/code&gt; file as a template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .env.example
API_KEY=your_api_key_here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file will not contain sensitive data, but it gives an example of the variables required to run the project. Other developers can copy this file to .env and add their own credentials.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp&lt;/span&gt; .env.example .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🛑 Common Mistakes to Avoid
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hardcoding API Keys&lt;/strong&gt;: Never hardcode sensitive information directly in your Python code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# BAD EXAMPLE: Never do this
&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hardcoded_api_key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Committing .env files&lt;/strong&gt; : Ensure that .env is always included in .gitignore to avoid accidentally pushing it to version control.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pushing Virtual Environments&lt;/strong&gt;: Always exclude virtual environments (like venv) from Git:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .gitignore
venv/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📚 Resources:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Hamza-nabil/Secure-API-Key-Handling" rel="noopener noreferrer"&gt;Secure API Key Handling GitHub Repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.streamlit.io/8-tips-for-securely-using-api-keys/" rel="noopener noreferrer"&gt;8 Tips for Securely Using API Keys&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>api</category>
      <category>security</category>
    </item>
  </channel>
</rss>
