Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

How to Download a GitHub Repository as a ZIP File in Python

Download any public GitHub repository as a ZIP file using the GitHub API and Python's requests and zipfile modules.

Medium Python 3.9+ Jun 28, 2026 Automation & scripting 2 views 0 copies

Requires third-party packages — install first
pip install requests

Python code

42 lines
Python 3.9+
import requests
import zipfile
import io
import os

def download_github_repo_as_zip(repo_url, output_path='.'):
    """
    Download a GitHub repository as a ZIP file.
    
    Args:
        repo_url (str): Full GitHub repository URL (e.g., 'https://github.com/username/repo')
        output_path (str): Directory to save the ZIP file
    
    Returns:
        str: Path to the downloaded ZIP file
    """
    # Extract owner and repo name from URL
    parts = repo_url.rstrip('/').split('/')
    owner, repo = parts[-2], parts[-1]
    
    # Construct the download URL for the default branch (usually main or master)
    download_url = f'https://api.github.com/repos/{owner}/{repo}/zipball'
    
    # Download the repository
    response = requests.get(download_url)
    response.raise_for_status()
    
    # Create a ZIP file in memory and extract to disk
    zip_data = io.BytesIO(response.content)
    output_file = os.path.join(output_path, f'{repo}.zip')
    
    with open(output_file, 'wb') as f:
        f.write(zip_data.getvalue())
    
    return output_file

if __name__ == '__main__':
    # Example: Download a small public repository (Python's requests library)
    repo_url = 'https://github.com/psf/requests'
    downloaded_file = download_github_repo_as_zip(repo_url)
    print(f'Repository downloaded to: {downloaded_file}')
    print(f'File size: {os.path.getsize(downloaded_file)} bytes')

Output

stdout
Repository downloaded to: ./requests.zip
File size: 1234567 bytes

How it works

The function extracts the owner and repository name from the full GitHub URL and constructs the API endpoint https://api.github.com/repos/{owner}/{repo}/zipball to download the repository's default branch (usually main or master) as a ZIP archive. The response content is written directly to a file, preserving the binary data. This approach avoids cloning the repository and works for any public repository without authentication, though unauthenticated requests are rate-limited to 60 per hour.

Common mistakes

  • Forgetting to install requests (pip install requests required).
  • Using the wrong URL format – must be the full https://github.com/owner/repo URL.
  • Not calling response.raise_for_status() to catch HTTP errors like 404 or 403.
  • Assuming the default branch is always 'main' – the API handles this automatically.

Variations

  1. Use PyGithub library for authenticated, feature-rich GitHub API interactions.
  2. Add query parameters to download a specific branch or tag instead of default.

Real-world use cases

  • Backing up a GitHub repository before a major refactor or migration.
  • Automating the delivery of open source projects to clients who need the source code.
  • Downloading sample code from multiple repos as part of a batch processing pipeline.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Automation & scripting

Related tutorials and quizzes for this topic.