Skip to content

Querying GitHub API and gh CLI Output With fx ​

The GitHub REST API and the gh CLI both return JSON. Pipe it into fx to browse it interactively or to extract, filter, and sort fields with short JavaScript expressions. If you don't have it yet, install fx.

View GitHub API JSON in the Terminal ​

Pipe any gh api call into fx to open the response in the interactive viewer:

bash
gh api repos/antonmedv/fx | fx

Collapse and expand nodes with the arrow keys, press / to search, or @ to fuzzy-search paths. To just pretty-print the response without opening the viewer, pass .:

bash
gh api repos/antonmedv/fx | fx .

Extract Fields From a GitHub API Response ​

Get a single field with .field:

bash
gh api repos/antonmedv/fx | fx '.stargazers_count'
20145

Build a smaller object from the fields you care about, including nested ones:

bash
gh api repos/antonmedv/fx | fx '({name: x.full_name, stars: x.stargazers_count, forks: x.forks_count, license: x.license.spdx_id})'
json
{
  "name": "antonmedv/fx",
  "stars": 20145,
  "forks": 459,
  "license": "MIT"
}

Print an array one item per line with list:

bash
gh api repos/antonmedv/fx | fx '.topics' list
cli
json
terminal
tui

Parse gh CLI JSON Output ​

Most gh commands accept --json with a list of fields. Map over the result with @:

bash
gh pr list --json number,title,author,isDraft,labels | fx '@.title'
json
[
  "Add TOML output support",
  "Bump golang.org/x/term from 0.24.0 to 0.25.0",
  "WIP: fuzzy search improvements",
  "Fix crash on empty input with --slurp"
]

Filter Pull Requests by Author, Draft, or Label ​

Use ? to filter. This drops bot PRs and drafts, then formats each PR as a line:

bash
gh pr list --json number,title,author,isDraft | fx '?!x.author.is_bot && !x.isDraft' '@`#${x.number} ${x.title} (@${x.author.login})`' list
#412 Add TOML output support (@antonmedv)
#405 Fix crash on empty input with --slurp (@mkim)

Find PRs with a specific label:

bash
gh pr list --json number,labels | fx '?.labels.some(l => l.name === "bug")' '@.number'
json
[
  405
]

List a User's GitHub Repos Sorted by Stars ​

Fetch repos from the REST API with curl, skip forks, and sort by stargazers_count in descending order with sortBy:

bash
curl -s 'https://api.github.com/users/antonmedv/repos?per_page=100' \
  | fx '?!x.fork' 'sortBy(r => -r.stargazers_count)' '@({name: x.name, stars: x.stargazers_count})'
json
[
  { "name": "fx", "stars": 20145 },
  { "name": "expr", "stars": 6812 },
  { "name": "walk", "stars": 3290 },
  { "name": "finder", "stars": 1405 }
]

For a tab-separated table:

bash
curl -s 'https://api.github.com/users/antonmedv/repos?per_page=100' \
  | fx '?!x.fork' 'sortBy(r => -r.stargazers_count)' '@`${x.stargazers_count}\t${x.full_name}`' list
20145	antonmedv/fx
6812	antonmedv/expr
3290	antonmedv/walk
1405	antonmedv/finder

Count Open Issues by Label ​

Flatten all label names with .[], group them with groupBy, and replace each group with its size:

bash
gh issue list --state open --json number,title,labels \
  | fx '.[].labels[].name' 'groupBy(x => x)' 'x => Object.fromEntries(Object.entries(x).map(([k, v]) => [k, v.length]))'
json
{
  "bug": 3,
  "tui": 2,
  "enhancement": 2
}

Issues with no labels at all:

bash
gh issue list --state open --json number,labels | fx '?.labels.length === 0' '@.number'
json
[
  410
]

Find a Release Download URL for Your Platform ​

The latest release lists its files under assets. Pick one by name:

bash
gh api repos/antonmedv/fx/releases/latest | fx '.assets.find(a => a.name.includes("linux_arm64")).browser_download_url'
https://github.com/antonmedv/fx/releases/download/39.2.0/fx_linux_arm64

Strings are printed without quotes, so you can pass the result straight to curl -LO "$(...)". To list every macOS build:

bash
gh api repos/antonmedv/fx/releases/latest | fx '.assets' '?.name.includes("darwin")' '@.browser_download_url' list
https://github.com/antonmedv/fx/releases/download/39.2.0/fx_darwin_amd64
https://github.com/antonmedv/fx/releases/download/39.2.0/fx_darwin_arm64

Process Paginated gh api --paginate Output ​

gh api --paginate prints one JSON array per page, one after another. fx reads concatenated JSON as a stream, so each expression runs once per page:

bash
gh api --paginate users/antonmedv/repos | fx '.length'
2
2
1

To work with all pages as one list, use --slurp to collect the pages into an array, then flatten it:

bash
gh api --paginate users/antonmedv/repos | fx --slurp flatten 'sortBy(r => -r.stargazers_count)' '@.name'
json
[
  "fx",
  "expr",
  "walk",
  "finder",
  "gitmoji"
]

Total stars across every page:

bash
gh api --paginate users/antonmedv/repos | fx --slurp flatten '.reduce((sum, r) => sum + r.stargazers_count, 0)'
31655

TIP

Without --slurp, sorting and grouping only apply within a single page. Slurp whenever the result depends on the whole list.

See Also ​