# Command line interface


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

You can use [`GhApi`](https://ghapi.fast.ai/core.html#ghapi) via the
command line, and can access nearly everything in the [GitHub
API](https://docs.github.com/rest).

There are three commands provided. For most people,
[`ghapi`](https://ghapi.fast.ai/cli.html#ghapi) will be the easiest to
use. All three commands take the following parameters:

- `--help`: list all parameters to this command and endpoint
- `--headers`: A list of extra headers to pass, JSON-encoded
- `--token`: A GitHub authentation token
- `--debug`: Print requests before sending them

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`](https://ghapi.fast.ai/cli.html#ghapi) command

To get started with the [`ghapi`](https://ghapi.fast.ai/cli.html#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](https://ghapi.fast.ai/fullapi.html) 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](https://ghapi.fast.ai/fullapi.html#issues).

The first operation listed there is
[issues.list](https://docs.github.com/rest/reference/issues#list-issues-assigned-to-the-authenticated-user),
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`](https://ghapi.fast.ai/cli.html#ghapi) command
handles that distinction for you, so you can ignore it for now.

To use [`ghapi`](https://ghapi.fast.ai/cli.html#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](https://ghapi.fast.ai/fullapi.html#git)
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:

``` python
! 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.

``` python
! 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:

``` python
! ghapi create_release --help
```

    (tag_name, branch='master', name=None, body='', draft=False, prerelease=False, files=None)

## The [`ghpath`](https://ghapi.fast.ai/cli.html#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](https://docs.github.com/rest/reference/git#get-a-reference).
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`](https://ghapi.fast.ai/cli.html#ghpath) command. The
arguments are exactly the same as
[`ghapi`](https://ghapi.fast.ai/cli.html#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`](https://ghapi.fast.ai/cli.html#ghapi),
[`ghpath`](https://ghapi.fast.ai/cli.html#ghpath) handles header, query,
path and body parameters for you automatically.

For instance, the previous `git.get_ref` command using
[`ghpath`](https://ghapi.fast.ai/cli.html#ghpath) is:

``` python
! 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`](https://ghapi.fast.ai/cli.html#ghraw) command

Sometimes, you just want to call an exact path directly, handling the
path and query parameters yourself. The
[`ghraw`](https://ghapi.fast.ai/cli.html#ghraw) command provides that
functionality. Like with
[`ghpath`](https://ghapi.fast.ai/cli.html#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:

``` python
! 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`](https://ghapi.fast.ai/cli.html#ghapi) behind the scenes by the
`completion-ghapi` command (which is implemented with the
[`completion_ghapi`](https://ghapi.fast.ai/cli.html#completion_ghapi)
function). You probably won’t need to call this yourself - it will be
called for you as part by your shell.

``` python
! completion-ghapi git
```

    git

``` python
! completion-ghapi gi
```

    gists git gitignore

``` python
! 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

``` python
! 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`](https://ghapi.fast.ai/cli.html#ghapi) by placing the following
command at the end of your `~/.bashrc` or `~/.zshrc` file:

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