0

I've been trying to expand my knowledge of using API keys. I've been able to use a hidden API key with Ruby, now I'm testing out using them in JS. I built a very simple Javascript/jQuery app that uses a API key to tell the weather. I would like to push my code to GitHub. How can I hide this key? Is it as simple as putting the API key in a .gitignore file? Do I need to give any additional commands? This is a simple one page application.

Leia_Organa
  • 1,894
  • 7
  • 28
  • 48
  • You need to put it in a ignored file, by the way, if your will run in any client (example browsers) you shouldn't store any API KEY. Use them only in servers – Hitmands Apr 05 '16 at 05:24

1 Answers1

1

Put it in a file, lets say you call it config.js

Put the name of that file in your .gitignore

Push to github, check to see if that file is there

It will not be there because that is the purpose of the .gitignore file!

From git docs:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details.

Link to docs: https://git-scm.com/docs/gitignore

Good Luck!

PS .gitignore files ARE pushed to github see this pic so you don't want to put anything secret in there!

enter image description here

omarjmh
  • 13,632
  • 6
  • 34
  • 42
  • So I can just put something like : var weather_api_key = *api key here* in the config.js file? and it will still link up to my main js file which contains the API call? – Leia_Organa Apr 05 '16 at 05:18
  • in the example above, you would literally type 'config.js' (without quotes) and thats all, you put all the api keys in the config.js file though. – omarjmh Apr 05 '16 at 05:20
  • now thats a different question that depends a few other things, luckly its been discussed A LOT here is a archived post about it: http://stackoverflow.com/questions/950087/include-a-javascript-file-in-another-javascript-file – omarjmh Apr 05 '16 at 05:23