$ ls ./menu

© 2025 ESSA MAMDANI

cd ../blog
2 min read

Kaggle API, How to use it

> Kaggle API and discusses how to use it, along with addressing the broader question of accessing datasets via APIs.

Audio version coming soon
Kaggle API, How to use it
Verified by Essa Mamdani

This article explores the Kaggle API and discusses how to use it, along with addressing the broader question of accessing datasets via APIs.

Kaggle API

The official Kaggle API is available on GitHub: https://github.com/Kaggle/kaggle-api. It's a command-line tool implemented in Python 3, offering access to various Kaggle functionalities.

Installation

  1. Ensure you have Python 3 and pip installed.
  2. Run pip install kaggle. (On Mac/Linux, pip install --user kaggle is recommended.)

API Credentials

  1. Create a Kaggle account at https://www.kaggle.com.
  2. Go to your account settings (https://www.kaggle.com/<username>/account) and create an API token. This downloads kaggle.json containing your credentials.
  3. Place kaggle.json in:
    • Linux: $XDG_CONFIG_HOME/kaggle/kaggle.json (defaults to ~/.config/kaggle/kaggle.json) or ~/.kaggle/kaggle.json
    • Windows: C:\\Users\\&lt;Windows-username&gt;\\.kaggle\\kaggle.json
    • Other: ~/.kaggle/kaggle.json
  4. For security, use chmod 600 ~/.config/kaggle/kaggle.json on Unix-based systems. Alternatively, set environment variables KAGGLE_USERNAME and KAGGLE_KEY.

Using the Kaggle API from Python

The Kaggle API's documentation primarily focuses on command-line usage. However, you can integrate it into Python scripts. Here's how, based on Stack Overflow examples:

python
1from kaggle.api.kaggle_api_extended import KaggleApi
2api = KaggleApi()
3api.authenticate()
4
5# Download all files of a dataset
6api.dataset_download_files('avenn98/world-of-warcraft-demographics')
7
8# Download a single file
9api.dataset_download_file('avenn98/world-of-warcraft-demographics', 'WoW Demographics.csv')
10
11# Download all files for a competition
12api.competition_download_files('titanic')
13
14# Download a single file for a competition
15api.competition_download_file('titanic', 'gender_submission.csv')
16
17# Submit to a competition
18api.competition_submit('gender_submission.csv', 'API Submission', 'titanic')
19
20# Retrieve Leaderboard
21leaderboard = api.competition_view_leaderboard('titanic')

Remember to replace placeholders like dataset and competition names with your actual values. A more detailed explanation with various use cases can be found in this blog post: https://technowhisp.com/kaggle-api-python-documentation/.

Troubleshooting

The provided Stack Overflow example (https://stackoverflow.com/questions/55934733/documentation-for-kaggle-api-within-python) highlights a UnicodeDecodeError. This often stems from incorrect file encoding handling. Ensure your file is correctly encoded (e.g., UTF-8). The error might also indicate problems with the filename itself.

Other Dataset APIs

The Kaggle product feedback thread (https://www.kaggle.com/product-feedback/45093) shows a request for more comprehensive APIs, similar to OpenML. Currently, the Kaggle API's scope is limited, primarily focusing on competitions, datasets, and kernels. The availability of broader APIs for user kernels, competition information, and dataset details is not explicitly confirmed.