Sub topic 9
Calling the API directly with gh api
This chapter assumes you have authenticated gh (see the authentication chapter), know the --json and --jq flags from the previous chapter, and understand what a REST endpoint is. Most tasks have a dedicated gh command. When one doesn't, gh api lets you call any GitHub REST or GraphQL endpoint with your existing login.
What gh api does
gh api sends an authenticated HTTP request and prints the response body. It adds your token, sets the right headers, and handles pagination for you. You pass an endpoint path, and gh supplies the host and credentials.
Two placeholders save typing. Inside a clone, {owner}, {repo}, and {branch} are filled in from the current repository.
gh api repos/{owner}/{repo}/issues --paginate --jq '.[].title'This prints the title of every issue returned. --paginate follows the Link headers until no pages remain, so you get all results and not just the first 30. --jq works as it does with --json: it filters the response with a jq expression.
The request flow
Choosing the HTTP method and sending data
gh api uses GET by default. Once you add a parameter with -f or -F, the default becomes POST. Use --method (or -X) to override that.
-f key=valuesends a string field.-F key=valuesends a typed field. It convertstrue,false,null, and integers, and it reads a file when the value starts with@.
gh api repos/{owner}/{repo}/issues \
-f title="Crash on empty config" \
-f body="Run the tool with an empty config file." \
--jq '.html_url'For a GET request, fields become query parameters instead of a body. Use --method GET to make that explicit:
gh api --method GET search/issues \
-f q='repo:cli/cli is:open label:bug' \
-F per_page=5 \
--jq '.items[] | "\(.number)\t\(.title)"'Calling GraphQL
For GraphQL, send your query to the special graphql endpoint with -f query=.... Variables go in with -F so that numbers keep their type.
gh api graphql -F count=3 -f query='
query($count: Int!) {
viewer {
login
repositories(first: $count, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes { nameWithOwner }
}
}
}' --jq '.data.viewer.repositories.nodes[].nameWithOwner'The query declares $count as a required integer, and -F count=3 supplies it as a number. With -f, it would arrive as the string "3" and the query would fail validation.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `HTTP 404: Not Found` on a repository you can see | Token lacks access, or the path is wrong | Check `gh auth status`, then the endpoint spelling |
| `HTTP 403` with a rate-limit message | Too many requests | Wait, or authenticate, and reduce calls |
| Only 30 results printed | No `--paginate` | Add `--paginate` or `-F per_page=100` |
| GET request returns a validation error | Fields were sent as a body | Add `--method GET` |
| GraphQL `Int!` variable rejected | Sent with `-f` as a string | Use `-F` |
| Placeholders printed literally | Not inside a repository | Write the owner and repo explicitly |
When to use gh api
Prefer a dedicated command, such as gh pr list, when one exists. Its output is stable and its flags are documented. Reach for gh api when you need a field or endpoint that no command exposes, or when you want one GraphQL query in place of several calls.
Exercise
You want the titles of all open issues labeled bug in the current repository. You run gh api repos/{owner}/{repo}/issues --jq '.[].title' and get only 30 titles, including some without the label. Which changes fix both problems?
Show answer
Add --paginate to follow all pages, and pass the filter as query parameters, for example gh api --method GET repos/{owner}/{repo}/issues -f labels=bug -f state=open --paginate --jq '.[].title'. Note that this endpoint also returns pull requests, so you may need to filter those out with select(.pull_request == null) in the jq expression.
Exercise
A GraphQL call using -f first=5 fails with a type error for an Int! variable. What is the fix?
Show answer
Use -F first=5. The capital -F sends the value as an integer, while -f sends the string "5".