<?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>DEV Community: EPrenzlin</title>
    <description>The latest articles on DEV Community by EPrenzlin (@eprenzlin).</description>
    <link>https://dev.to/eprenzlin</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%2F572147%2F6f5cdd87-5e54-402b-a481-4451bcd14edf.png</url>
      <title>DEV Community: EPrenzlin</title>
      <link>https://dev.to/eprenzlin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eprenzlin"/>
    <language>en</language>
    <item>
      <title>.Env, .gitignore, and protecting API keys</title>
      <dc:creator>EPrenzlin</dc:creator>
      <pubDate>Mon, 15 Feb 2021 14:15:03 +0000</pubDate>
      <link>https://dev.to/eprenzlin/env-gitignore-and-protecting-api-keys-2b9l</link>
      <guid>https://dev.to/eprenzlin/env-gitignore-and-protecting-api-keys-2b9l</guid>
      <description>&lt;p&gt;Environment files, .gitignore, api_keys and how to use them took longer than it should have to fully 'click' for me.This blog post will hopefully be a good starting ground for others. &lt;/p&gt;

&lt;p&gt;TL;DR: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;files in .gitignore don't get uploaded onto github &lt;/li&gt;
&lt;li&gt;keep your api_key in the your .env file ; have your .env file included within your .gitignore file &lt;/li&gt;
&lt;li&gt;access your api_key with REACT_APP&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  API Keys
&lt;/h1&gt;

&lt;p&gt;First off, API keys are, or can be, a unique identifier (think like a password) used to authenticate the identity of the developer. Sometimes the api_keys are included within the get/post/delete request. Please note that different platforms use API_keys in different manners. &lt;/p&gt;

&lt;p&gt;As an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch(`https://testing.com/api/v4/webhook/add?token=1234abc`
.then(response =&amp;gt; response.json() 
.then(data =&amp;gt; console.log(data))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above fake snippet, we can see that within the fetch request, we have the "token=1234...", which is an example of how api_keys can be imbedded within the get URL.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;How can we protect this?&lt;/strong&gt;
&lt;/h1&gt;

&lt;h1&gt;
  
  
  .gitignore
&lt;/h1&gt;

&lt;p&gt;Put simply, this is a file dedicated to keep track of what files NOT to upload onto Github, so the whole world wont see the various information contained therein. &lt;/p&gt;

&lt;h1&gt;
  
  
  How to set up?
&lt;/h1&gt;

&lt;p&gt;When we creating our own new repository on Github, we have the option to create a .gitignore file. &lt;/p&gt;

&lt;p&gt;If we didn't initially do this, we could also quite simply&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch .gitignore in our root directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok - we have a basic overview of api_keys, and .gitignore... how does the .env file fit into all this? &lt;/p&gt;

&lt;h1&gt;
  
  
  .ENV
&lt;/h1&gt;

&lt;p&gt;Short for environment variable. Within the context of api_keys and .gitignore files, it is essentially a variable that we don't want other users/ developers to see. &lt;/p&gt;

&lt;p&gt;It is vital that the api_key remains within the .env file and is not known to the outside world. There are countless stories on the internet with developers not securing their api_keys within the .env, and being charged $$ from the api provider. &lt;/p&gt;

&lt;p&gt;Key to note, when creating the .env file, it must be done within the root directory, and not within the src files!&lt;/p&gt;

&lt;h1&gt;
  
  
  How to add .env in our .gitignore file?
&lt;/h1&gt;



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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#within our .env file
REACT_APP_api_key= "your_api_key_here" 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the above code snippet, the REACT_APP must be used, per the REACT docs. &lt;br&gt;
&lt;a href="https://create-react-app.dev/docs/adding-custom-environment-variables/"&gt;https://create-react-app.dev/docs/adding-custom-environment-variables/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we want to access the api_key within our app, such as when making a fetch request to the api_server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#below is how to access to api_key from our .env file. 
Notice how the below matches the variable in the .env file?
process.env.REACT_APP_API_KEY}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, to put this all together in an example fetch request&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#wherever we are making our example fetch requests... 
fetch(`fetch(`https://testing.com/api/v4/webhook/add?${process.env.REACT_APP_API_KEY}`
.then(response =&amp;gt; response.json() 
.then(data =&amp;gt; console.log(data))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;br&gt;
There we have it - a super quick guide on gitignore, .env, and how to incorporate them into your React app. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
