75

There was a good link here about how the zip/tarball string is created

When I download a zip from github, what is the hex string at the end of the file name represent?

But I'm looking at the GitHub APIv3 and I was curious if I'm missing something.

  1. Is there a way to get the zip/tarball link via an API call?

  2. If not, is there a way I can build that string without using the git binary or library? Meaning, can I use various API calls to pull out th data a need and assemble to URL I need?

I know the second question is a little unreasonable for stackoverflow and this is a bit of a fun project for me, so I would prefer on the second question if you just kind of nudged me in the right direction as opposed to throwing down a code snippet. Or just told me if it was possible.

Community
  • 1
  • 1
mkly
  • 2,253
  • 2
  • 18
  • 23

2 Answers2

125

You can wget your way out of the GitHub repo to get a tar file (archive):

wget --no-check-certificate https://github.com/User/repo/archive/master.tar.gz

# better, if the certificate authorities are present:
wget https://github.com/User/repo/archive/master.tar.gz

will get you a file named 'master' from the user 'User''s repo 'repo'.

The updated V3 API url is:

https://api.github.com/repos/User/repo/:archive_format/:ref
#
# two possibilities for fomat:
https://api.github.com/repos/User/repo/tarball/master
https://api.github.com/repos/User/repo/zipball/master

# from github example:
$curl -L https://api.github.com/repos/octokit/octokit.rb/tarball > octokit.tar.gz

You can then tar xpvf master, getting the full archive. It will create a directory following the naming convention described in the question you mentioned.

No Git binary is needed to get an archive from GitHub, thanks to their download service "Nodeload".


ligemer proposed in an edit the following example:

Edit 2016-08-25 - Shell Example With Wget, Variables, and Untar:

#!/bin/bash -ex

# arguments:
# token = $1
# organization = $2
# repo name = $3
# branch = $4

wget --header="Authorization: token ${1}" --header="Accept:application/vnd.github.v3.raw" -O - https://api.github.com/repos/${2}/${3}/tarball/${4} | tar xz

Call via:

$ scriptName.sh token my-organization site.example master

The command above will download and extract the GitHub folder to the same directory as the script.


Diogo Quintela suggests in the comments:

The following example allow the download, extract and cut the top level directory

curl -L https://api.github.com/repos/octokit/octokit.rb/tarball | tar xz --strip=1
Jon Wolski
  • 2,293
  • 2
  • 19
  • 21
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. I'm currently getting it that way via curl. I was just curious if there was a way to get the download link through the API. I know it isn't specifically useful as much as it's my own curiosity. – mkly Dec 04 '11 at 21:01
  • 1
    @Mike: no API that I know of is needed to get an archive from GitHub. The point is to propose a direct link for you to use. – VonC Dec 04 '11 at 21:34
  • Cool. Thanks. I think I'll mark the answer and move on. Thank you both for your advice. I appreciate it. – mkly Dec 04 '11 at 21:38
  • 4
    What if repository is private? Anyway to get the zipball in this case? – jayarjo Feb 29 '12 at 14:40
  • @jayarjo good question. I am not sure (unless you are the owner of that repo). – VonC Feb 29 '12 at 14:43
  • I am, but I need to request download link from the script. It probably needs to be authorized somehow. – jayarjo Feb 29 '12 at 14:46
  • Not work now,i try your command just get the mast file,do not get tarball file – Mr Lou Apr 09 '12 at 11:18
  • @janwen you always get a file named '`master`'. But try a '`tar xpvf master`, and you will see: it works! That '`master`' file is actually a tarball. – VonC Apr 09 '12 at 11:34
  • @janwen see an example at the very beginning of: https://raw.github.com/VonC/compileEverything/master/.cpl/doc. This is how I download my project tarball. – VonC Apr 09 '12 at 11:35
  • Thank you @Ligemer for the example. – VonC Aug 26 '16 at 05:57
  • The following example allow the download, extract and cut the top level directory `curl -L https://api.github.com/repos/octokit/octokit.rb/tarball | tar xz --strip=1` – Diogo Quintela Oct 31 '16 at 23:32
  • @DiogoQuintela Thank you. I have included your comment in the answer for more visibility. – VonC Oct 31 '16 at 23:36
  • For public repos, without api.github.com, does it not have a access limit? (cause I'm having none. its wierd.. for this expensive operation) – softmarshmallow Jul 04 '22 at 17:22
  • @softmarshmallow I am sure there must be some kind of limit, but I don't know the specifics. – VonC Jul 04 '22 at 17:27
31

The syntax is described in the docs:

GET /repos/:owner/:repo/:archive_format/:ref

The following example URL will point (via a 302 redirect) to a zip archive of master in the hadley/devtools repo:

https://api.github.com/repos/hadley/devtools/zipball/master

(The other option for archive_format is tarball.)

I have no idea since when this API is available.

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
krlmlr
  • 25,056
  • 14
  • 120
  • 217
  • Hey, is there any way to download just one folder? like only the `R` folder in your example. – samayo Nov 19 '14 at 13:54
  • @samaYo: I'm not aware of a way to achieve this via the API, but you might want to try [`git archive --remote=... `](http://git-scm.com/docs/git-archive). I'd be interested to know if this works with GitHub ;-) – krlmlr Nov 19 '14 at 22:34
  • @samayo Alternativ way is to untar the folder you need Here is an example I am using when downloading a github archive where I only wish src folder tar -xzf archive.tar.gz "$(tar -tzf archive.tar.gz | head -1 )src" --strip 1 – Tirsvad Jan 04 '21 at 01:11
  • Updated Github documentation link: https://docs.github.com/en/rest/reference/repos#download-a-repository-archive-zip – blacelle Aug 01 '21 at 21:17
  • @samayo Were you able to figure out a way to download one of the subfolder in the repo? The tarball API does not support the path. – PraveenMak Aug 30 '21 at 22:03
  • @PraveenMak I don't know if I can remember well as this was a long time ago. But I would say no. I didn't find the solution I think. Maybe it's better if you ask a question on it. Good luck – samayo Aug 31 '21 at 04:15