GitHub API: Fetch all folders and Files in Single GET Request

Doctor Blue picture Doctor Blue · Feb 6, 2013 · Viewed 8.6k times · Source

In the GitHub API, I can issue a GET request of

https://api.github.com/repos/owner/repo/git/trees/master?recursive=1

to recursively fetch all of the trees of a repository. In addition to giving me all of the directories, it gives me URLs that I can use to download individual files:

[...]
{
    "mode": "100644",
    "type": "blob",
    "sha": "abc1234",
    "path": "path/to/file.txt",
    "size": 104361,
    "url": "https://api.github.com/repos/owner/repo/git/blobs/abc1234"
},
[...]

While the recursive=1 piece prevents me from having to make a new GET request for every directory in the repository, I still have to make an individual call for every file. I've looked through the GitHub API Docs, but they don't present a way to do this. It's very likely that there simply isn't a way to fetch all files and folders in a single request, but I wanted to ask here to verify that I have no other option.

Answer

Ivan Zuzak picture Ivan Zuzak · Feb 6, 2013

The only way to do it with a single request is to get the current contents as an archive: https://docs.github.com/en/rest/reference/repos#download-a-repository-archive

Actually, it's 2 requests since the initial response is a 302 redirect.