cli/cli
Raw API access (gh api)
Active contributors: Mislav, Kynan, Corey Johnson
Purpose
gh api lets users (and scripts) call any GitHub REST or GraphQL endpoint. It handles auth, host selection, pagination, JSON decoding, request body construction, and field-level filtering with --jq or --template. It is essentially curl plus everything gh already knows about your account.
Directory layout
pkg/cmd/api/
api.go # NewCmdApi: ~14 KB, the whole command lives here
fields.go # parses --field/-F (typed values) and --raw-field/-f
http.go # request construction, hostname resolution
pagination.go # follows Link headers and GraphQL pageInfo
pretty_print.go # color JSON output for TTYUnlike most commands, gh api is intentionally a single Cobra command without subcommands; verbs are encoded in flags (-X, --method).
Key abstractions
| Symbol | File | Role |
|---|---|---|
NewCmdApi |
pkg/cmd/api/api.go |
Sets up flags, wires the apiRun function. |
processFields |
pkg/cmd/api/fields.go |
Parses -f key=value (string), -F key=value (typed), and reads stdin. |
httpRequest |
pkg/cmd/api/http.go |
Builds the final http.Request, including hostname swap for graphql. |
paginate |
pkg/cmd/api/pagination.go |
Implements --paginate for both REST (Link: rel="next") and GraphQL (pageInfo.endCursor). |
prettyPrintJSON |
pkg/cmd/api/pretty_print.go |
Colour-printing through pkg/jsoncolor. |
How it works
gh api graphql -f query='...' -f variables='...' resolves to:
- The smart base repo resolver picks a host (so
--hostnameis rarely needed inside a clone). - The
queryfield is plain JSON, the variables field is parsed viafields.go. httpRequestsetsAuthorization,User-Agent, and the GraphQL features header. Hostname becomesapi.<host>for REST,api.<host>/graphqlfor GraphQL.- The response body is streamed through the pretty-printer (TTY) or the
--jq/--templatefilter. - With
--paginate, the loop continues until there is no next cursor; multiple JSON arrays are concatenated when feasible.
gh api -X DELETE repos/{owner}/{repo}/issues/comments/123 works the same way; placeholder substitution uses the resolved repo for {owner} and {repo}.
Integration points
- Uses the same
cmdutil.Factory.HttpClientas every other command, so debug logging viaGH_DEBUG=apiworks identically. - Output filtering uses
pkg/jsoncolorand the embedded jq viaitchyny/gojq. - Errors with scope hints come back through
api.HTTPError.ScopesSuggestionto surface "trygh auth refresh -s ..." messages.
Entry points for modification
- New flag: extend
NewCmdApiand add a row to the table-driven test inapi_test.go. - New paginator behaviour: edit
pagination.go(be careful:--paginate --jqinteraction is exercised by tests inapi_test.go). - New placeholder: extend the substitution logic in
api.go({owner},{repo},{branch}are the existing ones).
Key source files
| File | Purpose |
|---|---|
pkg/cmd/api/api.go |
Command definition. |
pkg/cmd/api/fields.go |
Field parsing. |
pkg/cmd/api/http.go |
Request construction. |
pkg/cmd/api/pagination.go |
Paginate REST + GraphQL. |
pkg/cmd/api/pretty_print.go |
Coloured JSON output. |
Related pages
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.