šŸæ 3 min. read

Hide Your API Keys

Monica Powell

How to Hide Your API Keys in PythonĀ šŸ”‘

Protect your applicationā€™s API Keys while committing toĀ Git.

black-and-white-code-programming-tech-79290

If you plan on programming any applications and storing your code in a public GitHub repository then it is important that you protect your API keys šŸ”‘ by ensuring that they are not searchable or otherwise publicly accessible.

Whatā€™s anĀ API?

An application programming interface (API) is a structured set of instructions for building applications. If you want to leverage data from services such as Twitter, The New York Times, Slack, Spotify, etc. then you should read their APIs to figure out how to structure your queries to receive data from their service or to post on their service.

Example APIs:

What are APIĀ keys?

API keys allow developers to access APIs and are unique keys associated with that particular developer and/or application. Just like you shouldnā€™t share your passwords you should never share your API keys. It is important to protect your API keys so that people do not take any actions as you which could result in your API key being revoked due to somebody else exceeding rate limits or abusing/violating an APIs terms of service. A rate limit is when an application limits the number of API calls that a specific application or user can make during a specified period of time.

How do I protect my API keys onĀ Github?

Hereā€™s how to hide API keys in Python from GitHub using config.py to store your sensitive API keys and tokens in a separate file from your main script. I used similar code when accessing the Twitter Search API for my blackgirlmagic twitter bot.

Create 3 Files in Your Application

config.py

This file will store your API keys. You just need to update the portion in the strings with your API keys, depending on the service you may or may not need all four types of API keys. These in particular are required to create a Twitter application.

1api_key = "YOUR_KEY"
2api_secret = "YOUR_SECRET"
3access_token = "YOUR_ACCESS_TOKEN"
4token_secret = "YOUR_TOKEN_SECRET"

main_script.py

This file will store your main script that needs to access the API keys. This file can be named whatever you like.

1import config
2
3from twython import Twython, TwythonError
4
5# create a Twython object by passing the necessary secret passwords
6twitter = Twython(config.api_key, config.api_secret, config.access_token, config.token_secret)

.gitignore

AĀ .gitignore file tells GitHub to ignore the noted files, directories or files that end in specific extensions when committing files to GitHub. This step is crucial to ensure that your config.py file does not end up viewable on GitHub! Hereā€™s a collection of usefulĀ .gitignore templates.

github/gitignore
_A collection of usefulĀ .gitignore templates_github.com

1config.py
2__pycache__
3.ipynb_checkpoints

Feel free to reach out below with any comments or questions that you have. I would love to know how you hide your API keys when creating applications in Python or any other languages.

This article was published on January 26, 2017.


Don't be a stranger! šŸ‘‹šŸ¾

Thanks for reading "Hide Your API Keys". Join my mailing list to be the first to receive my newest web development content, my thoughts on the web and learn about exclusive opportunities.

    Ā 

    I wonā€™t send you spam. Unsubscribe at any time.