Set Up Kaggle API

Set Up Kaggle API

Kaggle is a platform that offers machine learning competitions, allows making submissions, download public datasets, and more. Kaggle has a large community of data scientists and machine learning engineers. We can use Kaggle website to manually perform desired actions. However, Kaggle offers an API that can be used to automate tasks such download the dataset.

Install Kaggle API

Kaggle provides an official API client for Python. It can be installed using pip package manager from the command line:

pip install kaggle

Get API token

To interact with Kaggle API, we need an API token. Follow these steps to create token:

  1. Sign in to Kaggle and go to account page https://www.kaggle.com/<username>/account
  2. Scroll down to the API section and click "Create New API Token".
Create Kaggle API token

A file called kaggle.json will be downloaded. This file contains the username and key required for API authentication.

{"username":"YOUR_USERNAME","key":"YOUR_KEY"}

By default, Kaggle API client checks the KAGGLE_USERNAME and KAGGLE_KEY environment variables to authenticate the user with the API. These variables can be populated with values from kaggle.json.

If environment variables not set then API client checks kaggle.json configuration file which can placed at ~/.kaggle/kaggle.json on Linux or Mac, and at C:\Users\<USERNAME>\.kaggle\kaggle.json on Windows.

Testing Kaggle API

The following code authenticates the user and prints a list of competitions.

import os

os.environ['KAGGLE_USERNAME'] = 'YOUR_USERNAME'
os.environ['KAGGLE_KEY'] = 'YOUR_KEY'

from kaggle.api.kaggle_api_extended import KaggleApi

api = KaggleApi()
api.authenticate()

competitions = api.competitions_list()
print(competitions)

Uninstall Kaggle API

If you want to completely remove Kaggle API client, run the following command:

pip uninstall kaggle

Leave a Comment

Cancel reply

Your email address will not be published.