3.4 Hiding API Keys with Environment Variables (dotenv) and Pushing Code to GitHub

Поделиться
HTML-код
  • Опубликовано: 27 сен 2024
  • In this lesson, we will address how to can hide an API key using environment variables and open source the code on GitHub.
    💻github.com/Cod...
    🔗 dotenv: www.npmjs.com/...
    🔗 GitHub: github.com/
    🎥 NEXT LESSON: • 3.5 Web Application De...
    🎥 PREVIOUS LESSON: • 3.3 Mapping Database E...
    🎥 FULL COURSE: • Working with Data and ...
    🎥 Git and GitHub: • 1.1: Introduction - Gi...
    🚂 Website: thecodingtrain....
    💖 Patreon: / codingtrain
    🛒 Store: www.designbyhu...
    📚 Books: www.amazon.com...
    🎥 Coding Challenges: • Coding Challenges
    🎥 Intro to Programming: • Start learning here!
    🔗 p5.js: p5js.org
    🔗 Processing: processing.org
    📄 Code of Conduct: github.com/Cod...

Комментарии • 226

  • @janetrajkoski1424
    @janetrajkoski1424 3 года назад +3

    As someone who just started learning to code and my first interview being: "The hell you doing your API key is public", this was a godsend video. Very to the point and clear explanation. Subscribed

  • @aristotleanderson930
    @aristotleanderson930 4 года назад +42

    This is a great tutorial! Thank you for being quick and to the point, as well as informative and helpful!

    • @gustavotomas5214
      @gustavotomas5214 3 года назад

      You probably dont give a damn but does someone know a way to log back into an instagram account?
      I somehow forgot my account password. I love any help you can give me.

    • @casenace5722
      @casenace5722 3 года назад

      @Gustavo Tomas Instablaster :)

    • @gustavotomas5214
      @gustavotomas5214 3 года назад

      @Casen Ace thanks so much for your reply. I found the site thru google and Im waiting for the hacking stuff now.
      Seems to take a while so I will reply here later when my account password hopefully is recovered.

    • @gustavotomas5214
      @gustavotomas5214 3 года назад

      @Casen Ace It did the trick and I finally got access to my account again. I am so happy!
      Thank you so much, you really help me out!

    • @casenace5722
      @casenace5722 3 года назад

      @Gustavo Tomas no problem =)

  • @hj1916
    @hj1916 Год назад +3

    It was never boring! Actually, you made this serious topic so fun like magic. Also, I loved the ending credit of the train. Creative idea!!! Thank you so much!!!!

  • @zeyadahmedsamer3443
    @zeyadahmedsamer3443 2 года назад +2

    no words can describe your awesome explanation

  • @MrGreg557
    @MrGreg557 2 года назад

    What a great series man, I'm grateful everyone can use resources like this. Going from knowing almost 0 JS to deploying my app was very satisfying.
    One update though: if you get this error like me: "npm ERR! Missing script: "start"", add this to the end of your package.json file, before the finishing curly brace:
    ,"scripts": {
    "start": "node index.js"
    },

  • @SaiKumar-vf9lz
    @SaiKumar-vf9lz 4 года назад

    From hiding api keys to adding local source to GitHub ..well summarized things in 10 mins ..great video !

  • @kareng9484
    @kareng9484 2 месяца назад

    Bro, thank you very much, this is exactly what I needed, you're so good at explaining things

  • @Niehde
    @Niehde 5 лет назад +20

    Nice video! I've just recently pushed some API keys to Github. Fortunately they have a service that lets you know when you've screwed up.

  • @smashing_data4292
    @smashing_data4292 8 месяцев назад

    As someone who has done this before on my own projects I can say this is legit. Immediate subscribe.

  • @Rlc201
    @Rlc201 4 года назад +2

    How i didn't know this channel? Awesome tutorial and one of the best teachers that i have ever seen on youtube. Keep up!

  • @aakash-codes
    @aakash-codes Год назад

    I just love the way you teach things! Doesn't even let me feel boring for a single sec! 😊🚀

  • @benSfft
    @benSfft 4 года назад +6

    Thanks a lot ! That's clear and the objectives are so well explained ! I didn't even know that we could code on ecstasy !

  • @gme0ver24
    @gme0ver24 5 лет назад +6

    Useful stuff as always. Waiting for the next video!

  • @bandarabdullah40
    @bandarabdullah40 5 лет назад

    I was suffering to understand the environment variable and its benefits this video the best explanation I have ever seen.

  • @zorro1rr
    @zorro1rr 4 года назад +3

    I'm getting the api key returned in my terminal, but now my data is not being returned in the browser and getting console error: Uncaught ReferenceError: require is not defined. Help please

    • @TheCodingTrain
      @TheCodingTrain  4 года назад

      Our Coding Train Discord is a great place to get help with coding questions ! discord.gg/hPuGy2g
      - The Coding Train Team

  • @SnobbyLion
    @SnobbyLion 11 месяцев назад

    You're so good at explaining things! Thank you!

  • @snehagangurde6266
    @snehagangurde6266 4 года назад +1

    Thank you so much i have been searching one good video for days. finally got this one. this one is quick, understandable

  • @kenfeier5542
    @kenfeier5542 10 месяцев назад

    Great tutorial, but I do want to mention one thing. Environmental variables help you hide your sensitive information for version control purposes. If you build a public website and have environmental variables showing on client side, a user can open the debugger and hover over those variables and see the values of them. Make sure that you use these variables on back-end (server) side if you don't want anyone to see their values.

  • @leerenae313
    @leerenae313 3 года назад +1

    Hey, love your videos! they're so straight to the point Thank you for putting this out for free 🙏

  • @220syedrazamehdirizvi7
    @220syedrazamehdirizvi7 4 месяца назад

    U made it so fun to watch ....❤

  • @drhastings
    @drhastings 3 года назад

    Simple and straight to the point thank you👏🏿

  • @pjguitar15
    @pjguitar15 3 года назад +1

    You're a great teacher!!!

  • @mohammedraqeeb4392
    @mohammedraqeeb4392 2 года назад

    Great explanation

  • @santra528
    @santra528 3 года назад +1

    Using dotenv package and store the API_KEY in .env file does not completely hide the API_KEY. It is fine for GitHub because someone visiting this repo they wont be able to see the API_KEY. But if the project is deployed in sever then anyone can see the API_KEY from the browser when they visit this particular website. The best way to hide the API_KEY is store it in the backend and make the API calls from the backend only. Only send the response data to the frontend. And to add more security you can set up CORS for the API_KEY so even if someone gets access to the API_KEY they wont be able to send request as the request will be rejected and only the request from the domains mention in the CORS will be able to make successful request using this API_KEY.

    • @incandescentbruh
      @incandescentbruh 3 года назад

      No one is dumb enough to put the api key in frontend, also the example code in the video is using express js which is run by node js in backend.

  • @sweickel
    @sweickel 3 года назад

    Thank you! This is exactly the video and explanation I needed!

  • @RayfranRocha
    @RayfranRocha 4 года назад +1

    Spectacular tutorial! Thank you. It's very clear. I wonder if using any browser inspector, can anyone see the API_KEY? Looking inside the code or in the Request to the API message?

  • @coffeymay6775
    @coffeymay6775 4 года назад +1

    It says require is not defined for me in the console. I followed the steps in terminal, have the lastest version of node etc.???

  • @bacanalienigena
    @bacanalienigena Год назад

    It works amazingly, thanks a million!

  • @JanacMeena
    @JanacMeena 2 года назад

    2:00 start
    5:30 Create a sample .env
    Make sure .gitignore has .env listed

  • @noco2605
    @noco2605 2 года назад

    Thank you very much for your explanation 😊

  • @abigiyatadesse2672
    @abigiyatadesse2672 2 года назад

    Thanks so much this is helpful.

  • @dennisgonzales9521
    @dennisgonzales9521 3 года назад

    Very engaging! thank you for the lesson.

  • @nicetomeetugaming7024
    @nicetomeetugaming7024 2 года назад

    Amazing video sir!

  • @adedimolao9094
    @adedimolao9094 4 года назад +2

    can you use .env and gitignore in a vanilla js project?

  • @oyetorostephen5010
    @oyetorostephen5010 4 года назад +2

    THank you! I find this fun to learn

  • @santiagootero3580
    @santiagootero3580 6 месяцев назад

    you are the goat, thanks so much

  • @appiahseth5319
    @appiahseth5319 3 года назад

    Great presentation. Thank you very much

  • @nicetomeetugaming7024
    @nicetomeetugaming7024 2 года назад

    BTW you are amazing teacher!

  • @nicholashendrata
    @nicholashendrata 4 года назад

    THIS SAVED MY LIFE THANK YOU

  • @familyjuega12345
    @familyjuega12345 2 года назад

    Awesome content and explanation, thanks man!

  • @Cam-lm6wo
    @Cam-lm6wo 3 года назад +1

    incredible video, I spend like 1 year in a College trying to use GitHub as they told me without actually know what I was doing. thank you! Just a question... Can I add to the .env file a private key from firebase?

  • @techmarinar
    @techmarinar 3 года назад

    thanx man , this was helpful

  • @hargur4121
    @hargur4121 2 года назад

    Thanks a lot! This has been a lifesaver

  • @bluemagicuk
    @bluemagicuk 5 лет назад +1

    super helpful - thank you!

  • @sharadsharma3176
    @sharadsharma3176 5 лет назад

    eagerly waiting for the next video of this series....
    I want to know more about the available hosting services for node.

  • @danieloliveira9815
    @danieloliveira9815 3 года назад

    Typing those command line commands made me feel like a real programmer 😎 Thank you for this awesome playlist!

  • @aham3687
    @aham3687 5 лет назад

    could continue this series with PWA. This has been popular with web technology in this modern day.

  • @ivankljun2542
    @ivankljun2542 3 года назад

    Very nice, thank you, very educational and entertaining! :)

  • @dreznik
    @dreznik 5 лет назад +1

    sheefmahn brilliant series of videos. any videos where you interact with a Database-as-a-Service? SQL or not

  • @GopalChand
    @GopalChand Год назад

    Hi, how about if i am using Docker and dont want to publish my .env file in git. But my application is require env file. Can you pls help how to use .env file in Docker container or K8

  • @serdarinjo
    @serdarinjo 2 года назад

    Muchas gracias bro 😀!

  • @davidchopin6583
    @davidchopin6583 4 года назад

    So, so useful. Thanks!

  • @ilearncode7365
    @ilearncode7365 2 года назад

    From a “security/ privacy” standpoint, how is this any better than just having a js file with a variable for thr api key in it?

  • @akramsyed3628
    @akramsyed3628 2 года назад

    i am first here and very impresed they way you teach , ke

  • @weixiangng8279
    @weixiangng8279 Месяц назад

    If I were to deploy to vercel how can I retrieve the .env values?

  • @DigitalMonsters
    @DigitalMonsters 5 лет назад +8

    Whats the difference between using dotenv and just throwing them in another javascript file that you export from and then adding that file to the gitignore?
    I could watch the video and most likely get the answer to that question; but in the interest of being one of the earliest comments.... please understand.

    • @ridespirals
      @ridespirals 5 лет назад

      kind of the same thing, but environment variables are more common because you can configure them more easily on other host services like heroku, CI systems like Jenkins, you can change then on a command line, lots of other ways.

    • @BertMaurau
      @BertMaurau 5 лет назад

      Waiting for the actual answer, but like for me, my main reason for using .env files is because it's a "standard" and you can share that file between different languages for example. A single .env file can be read in NodeJS, PHP, Python,... (others?) without changing any syntax and let dotenv handle everything. My second reason would be the "environment" part. It's clear that it is environment-based and the scope for these variables is globally accessible (less an issue in NodeJS probably) within classes, methods, ..

    • @johncerpa3782
      @johncerpa3782 5 лет назад

      Ahh true that

    • @Loppy2345
      @Loppy2345 5 лет назад +1

      For the same reason you wouldn't take a helicopter to do your grocery shopping, it's not the right tool for the job.

  • @gideonaswani2449
    @gideonaswani2449 2 года назад

    Awesome, thanks

  • @iakovryzhichka2832
    @iakovryzhichka2832 10 месяцев назад

    I suppose if you commit env file before .gitignore file(with line.env), you'll see your .env file in the repo anyway.
    To delete it from the history, write this in console:
    git rm -r --cached .env
    git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
    git push --force

  • @LimitlessGamingYoutubeChannel
    @LimitlessGamingYoutubeChannel 3 года назад

    Super helpful video!

  • @willuk7336
    @willuk7336 3 года назад

    Thanks dad xx

  • @devinschlegel1763
    @devinschlegel1763 5 лет назад +10

    his next coding challenge should be solving the heat equation based off of 3blue1brown's video on the same topic

    • @TheCodingTrain
      @TheCodingTrain  5 лет назад

      Please suggest here! github.com/CodingTrain/Rainbow-Topics/issues

  • @kwameagyenim-boateng2968
    @kwameagyenim-boateng2968 3 года назад +1

    console.log(process.env) doesn't include my .env variable

  • @justingolden21
    @justingolden21 2 года назад

    install:
    npm install dotenv
    import:
    require('dotenv').config()
    use:
    process.env

  • @sazaam
    @sazaam 3 года назад

    Simply awesome

  • @instinqueue4689
    @instinqueue4689 2 года назад

    Thanks 👍

  • @shravans7184
    @shravans7184 5 месяцев назад

    Thanks

  • @Vincetroid
    @Vincetroid 4 года назад

    You saved me, suscribed!

  • @abidsiraj5728
    @abidsiraj5728 2 года назад

    tell me how to find that api keys again on GitHub??

  • @abhishektyagi4428
    @abhishektyagi4428 5 лет назад +1

    Sir Could you please make a video explaining the resources you use to learn or enhance your programming skills

  • @ggaa16990
    @ggaa16990 5 месяцев назад

    Bigg thankkk sir

  • @hasi90s
    @hasi90s Год назад

    Whatt if someone catch api key from network tab

  • @pierosabino
    @pierosabino 4 года назад

    Hi, I followed your video to publish code on github using dotenv but it doesn't work

  • @Jb67912
    @Jb67912 4 года назад

    you don't need an access token for the logs js file? It seems the website of mapbox requires one

  • @carlosduque8767
    @carlosduque8767 3 года назад

    Why when you put the node_modules in the .gitignore file you put a / in front of it?

    • @dusanlukic6784
      @dusanlukic6784 2 года назад

      because node modules is a folder and then git will ignore every file in that folder

  • @patrickporter6441
    @patrickporter6441 3 года назад

    I had a question: Can the same effect be accomplished with a config.json and including that in the gitignore?

  • @Viralplace
    @Viralplace 3 года назад

    but this works just for development on github... if I go in production like a jam stack project? it is not hidden right?

  • @Felipecuevas
    @Felipecuevas 4 года назад

    it only work with node servers but if I try to do it with angular have some several problems

  • @piyushtale0001
    @piyushtale0001 3 года назад

    Great Great Great

  • @pepew7102
    @pepew7102 3 года назад

    But your API key is still visible in the sources og the project right ?

  • @cperez7785
    @cperez7785 3 года назад

    Does anyone know how to find your API key? The text folder I created I accidentally deleted and I had to re-generate new APY keys but I do not know where I may find this text file to update it. Any help will appreciate it!

  • @kritikshivanshu
    @kritikshivanshu 4 года назад

    @TheCodingTrain Hi there! I am facing problem in order to upload .env file in my github repo. Since the application doesn't fetching information. What to do???Please help me.

  • @ankurkhandelwal1375
    @ankurkhandelwal1375 3 года назад

    What if index.js and .env are in different folders?

  • @WEBB99
    @WEBB99 5 лет назад +1

    how is this different from just making a json file and storing it there?

    • @TheCodingTrain
      @TheCodingTrain  5 лет назад +1

      Ultimately it's the same, but a lot of web hosting services support environment variables in native ways, see my last video in this series!

  • @li-on1
    @li-on1 5 лет назад

    The dot means add everything of the current directory.
    Otherwise "--all" is required.

  • @cosmos9227
    @cosmos9227 4 года назад

    Sir where I can Find import and export folder maker in android app resource?

  • @TheRealDjinnet
    @TheRealDjinnet 5 лет назад

    for people who tried to find the GitHub project in the video, here is the link to the GitHub repository: github.com/CodingTrain/The-Weather-Here

  • @ashy7198
    @ashy7198 2 года назад

    are .env not rendered to the browser then or something? What is to stop someone from viewing the app in their browser and fetching your API key out of the rendered JS with inspect element?

    • @TheCodingTrain
      @TheCodingTrain  2 года назад +1

      That is correct! The way I've set up this project the .env files are hidden on the server. You can only see them if you have login access to the server where they are held.

    • @BrianKiddDevDesign
      @BrianKiddDevDesign Год назад

      @@TheCodingTrain Excellent. This was a very fun and informative series. Thanks.

  • @gokulkannan1348
    @gokulkannan1348 4 года назад

    can xhr requests be hidden from the console?

  • @nikolatomic4306
    @nikolatomic4306 Год назад

    How to hide api key in vanila js?

  • @catalinhora6489
    @catalinhora6489 3 года назад

    i'm working with custom-env pretty similar, but I need to have two env files, for two environments, how should this be treated? How can i make it work?

  • @michaelroysomera1068
    @michaelroysomera1068 4 года назад

    Is this supported in Angular?

  • @Shinika01
    @Shinika01 5 лет назад

    This will NOT work when a client is connected to your website to use it. As the request is Javascript made, he WILL see the key in the url with the developper tools, Network tab... Am i missing something? i'm not really following that serie..

    • @moonythm
      @moonythm 5 лет назад

      No he wont because the request is made by the server, and in the dev tab u only see the requests made by the client

  • @sipadu
    @sipadu 2 года назад

    Does not work with angular though :(

  • @magebol
    @magebol 4 года назад

    Hi, Great tutorial!. I am new to this. I need to hide api keys but I don´t know how to install the dotenv. I tried to run install command you showed in he video, but I am doing something wrong in the terminal of my mac. Any recommendation anyone? Thanks

  • @montazmeahii6029
    @montazmeahii6029 4 года назад +2

    always sounds to me like you're saying "I'm going to post this in the video's subscription," but I'm sure you're saying "video's description".

  • @NBA6Fan
    @NBA6Fan 3 года назад

    So I do all of this and the key is still visible in console in browser... brutal protection

  • @cholasimmons
    @cholasimmons 4 года назад

    what about for typescript, doesn't anyone use typescript???? :(

  • @BGarda
    @BGarda Год назад

    So in which video does he talk about deploying it to a web server?

    • @TheCodingTrain
      @TheCodingTrain  Год назад

      It's a little out of date, but here you go! thecodingtrain.com/tracks/data-and-apis-in-javascript/data/3-the-weather-here/5-web-application-deployment

  • @papryk6705
    @papryk6705 5 лет назад +1

    when the last episode?

  • @my_food_logs
    @my_food_logs 4 года назад +1

    I use config module try it