Use Google Colab Secrets to Store Kaggle API Key
A new way to add API keys to Colab
Earlier this year Colab introduced a new feature called “Secrets” which comes in handy for storing API keys.
Downloading Kaggle edata from Colab easy — just one line of code
!kaggle competitions download -c [datasetname]
The tricky part is to add your Kaggle API to allow you access for download.
Currently this is how most people typically do to add Kaggle API to Colab and you will have to repeat this process in each notebook:
!pip install -q kaggle
from google.colab import files
files.upload() # choose the kaggle.json file that you downloaded
!mkdir ~/.kaggle
!cp kaggle.json ~/.kaggle/ # make directory named kaggle and copy kaggle.json file there.
!chmod 600 ~/.kaggle/kaggle.json # change the permissions of the file.
This blog post provides a brief instruction of how to easily add your Kaggle API using Colab’s Secrets feature, which can be used in all your Colab notebooks.
Step 1: Get your Kaggle API key
- Go to your Kaggle account settings page.
- Under the “API” section, click “Create New API Token”.
- This will download a
kaggle.json
file containing your username and key.