Command line interface

Access to the GitHub API from the command line

You can use GhApi via the command line, and can access nearly everything in the GitHub API.

There are three commands provided. For most people, ghapi will be the easiest to use. All three commands take the following parameters:

If you have an environment variable GITHUB_TOKEN defined, then it will be used for the token if --token is not passed. If neither is provided, then the commands will not be able to acces any authentication functionality.

We’ll now look at each of the three commands in turn.

The ghapi command

To get started with the ghapi command, first find the name of the operation you wish to perform. If you’ve used the Python API, then you already know the operation names - they’re whatever you type in Python after “api.”. To find the name of the operation you need, search the full API reference which contains information about every endpoint in the entire GitHub API. For instance, if you want to work with GitHub Issues, then searching for “issues” on the full reference page will take you to this section.

The first operation listed there is issues.list, shown with a link to the official GitHub documentation, and a list of parameters that the command accepts. You’ll see the official docs list some parameters, such as “accept”, which aren’t listed in the GhApi reference - that’s because GhApi automatically set some parameters for you.

You’ll also see that the parameter list on the official docs includes a column marked “in”, which can be “header”, “query”, “path”, or “body”. The ghapi command handles that distinction for you, so you can ignore it for now.

To use ghapi, pass the method name (exactly the same as you’d use in the Python API) as the first parameter, followed by any positional parameters required, and then keyword arguments with “--” before each parameter name.

For instance, git.get_ref takes three parameters: owner, repo, and ref. If we wish to pass the first two as positional parameters, and the last as a named argument, then we’d call:

! ghapi git.get_ref fastai ghapi-test --ref heads/master
- ref: refs/heads/master
- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy
- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master
- object: 
  - sha: f086d17cc9ec8e99f466c613cab44f98abad1db6
  - type: commit
  - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/f086d17cc9ec8e99f466c613cab44f98abad1db6

(NB: The “!” before the examples such as the one above is only needed when calling the commands from Jupyer Notebook. In your terminal, do not include the “!” prefix.)

If you pass just --help after the operation name, you’ll see a full list of all parameters accepted, and a link to the official GitHub documentation.

! ghapi git.get_ref --help
git.get_ref(owner, repo, ref)
https://docs.github.com/rest/reference/git#get-a-reference

Helper methods are also supported, e.g:

! ghapi create_release --help
(tag_name, branch='master', name=None, body='', draft=False, prerelease=False, files=None)

The ghpath command

If you find the endpoint you want in the GitHub docs, rather than the GhApi reference, you’ll see a path and a verb instead of an operation name. For example, here’s the GitHub documentation for git.getref. The white text on blue background section shows that the verb required is “GET”. Next to that is the path, which is listed as “/repos/{owner}/{repo}/git/ref/{ref}”.

To call an endpoint when you have this information from the GitHub docs, use the ghpath command. The arguments are exactly the same as ghapi, except that instead of an operation name, you provide the path (which you can paste directly from the GitHub docs) and verb.

Just like with ghapi, ghpath handles header, query, path and body parameters for you automatically.

For instance, the previous git.get_ref command using ghpath is:

! ghpath '/repos/{owner}/{repo}/git/ref/{ref}' get fastai ghapi-test --ref heads/master
- ref: refs/heads/master
- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy
- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master
- object: 
  - sha: f086d17cc9ec8e99f466c613cab44f98abad1db6
  - type: commit
  - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/f086d17cc9ec8e99f466c613cab44f98abad1db6

The ghraw command

Sometimes, you just want to call an exact path directly, handling the path and query parameters yourself. The ghraw command provides that functionality. Like with ghpath, you pass the path and verb as the first two arguments, but now you have to fill in all the path parameters yourself, plus construct the query string yourself (if one is required). If you need to pass body parameters, use the --data parameter, which requires that you encode the request body appropriately (which is JSON-encoded for most GitHub endpoints).

For instance, the git.git_ref example show in the previous two sections does not have any header, body, or query parameters, so we need only insert the path parameters, which results in this call:

! ghraw /repos/fastai/ghapi-test/git/ref/heads/master get
- ref: refs/heads/master
- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy
- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master
- object: 
  - sha: f086d17cc9ec8e99f466c613cab44f98abad1db6
  - type: commit
  - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/f086d17cc9ec8e99f466c613cab44f98abad1db6

Shell completions

Shell completions are provided for ghapi behind the scenes by the completion-ghapi command (which is implemented with the completion_ghapi function). You probably won’t need to call this yourself - it will be called for you as part by your shell.

! completion-ghapi git
git
! completion-ghapi gi
gists git gitignore
! completion-ghapi git.
git.create_blob git.create_commit git.create_ref git.create_tag git.create_tree git.delete_ref git.get_blob git.get_commit git.get_ref git.get_tag git.get_tree git.list_matching_refs git.name git.update_ref git.verbs
! completion-ghapi git.g
git.get_blob git.get_commit git.get_ref git.get_tag git.get_tree

Tab completion

You can enable tab completion for ghapi by placing the following command at the end of your ~/.bashrc or ~/.zshrc file:

eval "$(completion-ghapi --install)"