SerpBase
Back to Blog

Google Search From the Command Line: curl and jq With a Search API

Google results in the terminal with curl and jq: titles and links as TSV, a status check with an exit code, and a keyword file turned into CSV.

December 4, 2026
By SerpBase Team3 min read

Short answer: one curl POST to a search API plus jq gives you Google results in the terminal as plain text, TSV or CSV. Put your key in an environment variable, send the query as JSON to /google/search, and let jq pick the fields. The commands below were run against the answer we recorded for "trail running shoes" on 4 December 2026 (8 organic results).

Set the key once

export SERPBASE_API_KEY='YOUR_API_KEY'

One search, titles and links

curl -s -X POST 'https://api.serpbase.dev/google/search' \
  -H 'Content-Type: application/json' \
  -H "X-API-Key: $SERPBASE_API_KEY" \
  -d '{"q":"trail running shoes","gl":"us","hl":"en","device":"desktop"}' \
  | jq -r '.organic[] | "\(.rank)\t\(.title)\t\(.link)"'

Output (first lines):

1	Best Trail Running Shoes	https://rundreamachieve.com/running-shoe-buying-guides/best-trail-running-shoes/
2	Best Road To Trail Running Shoes | From Hoka to Altra	https://www.steadyfoot.com/best-road-to-trail-running-shoes/
3	7 Best Trail Running Shoes in 2027 – Grip Better, Run Stronger!	https://www.youtube.com/watch?v=7eDTdOaiF0M

Fail loudly when the search fails

The API answers HTTP 200 even for a failed search; the result is in the status field (0 means success). jq -e turns that into an exit code your script can test:

curl -s -X POST 'https://api.serpbase.dev/google/search' \
  -H 'Content-Type: application/json' \
  -H "X-API-Key: $SERPBASE_API_KEY" \
  -d '{"q":"trail running shoes"}' \
  | jq -e 'if .status == 0
           then "ok, \(.organic | length) results, \(.credits_charged) credit"
           else error("failed: \(.status) \(.error)") end'

On our recorded answer this printed "ok, 8 results, 1 credit" and exited 0; on a failed answer it prints jq: error ... failed: 1502 upstream service error and exits 5.

A shell function, and a keyword file to CSV

Building the JSON with jq -n --arg keeps quotes and non-English characters in the query safe:

gs() {
  jq -n --arg q "$1" '{q: $q, gl: "us", hl: "en", device: "desktop"}' \
    | curl -s -X POST 'https://api.serpbase.dev/google/search' \
        -H 'Content-Type: application/json' \
        -H "X-API-Key: $SERPBASE_API_KEY" \
        -d @-
}

# related searches for one query
gs "trail running shoes" | jq -r '.related_searches[]'

# every keyword in keywords.txt -> results.csv
while IFS= read -r kw; do
  gs "$kw" | jq -r --arg kw "$kw" \
    'select(.status == 0) | .organic[] | [$kw, .rank, .title, .link] | @csv'
  sleep 1
done < keywords.txt > results.csv

With two keywords in the file this wrote 16 CSV rows in our run. The related-searches line printed:

how long do trail running shoes typically last
how to choose trail running shoes
what are the key features of trail running shoes

Useful jq one-liners

jq -r '.organic[].link'                      # just the links
jq -r '.organic[].link | split("/")[2]'      # domains that rank
jq -r '.people_also_ask[]?.question'         # People Also Ask questions, if shown
jq -r '.ai_overview.summary // empty'        # the AI Overview text, if shown
jq '.credits_charged'                        # what the call cost

Things that trip people up

  • Use -s with curl. Without it the progress meter is mixed into the pipe output on some terminals.
  • Sections are optional. people_also_ask, ai_overview and others exist only when Google shows them; []? and // empty keep jq from failing.
  • Do not paste the key into the command. With the environment variable it stays out of your shell history and scripts.
  • Pause between calls in loops. sleep 1 keeps a simple loop well inside the per-second limit (rate limits).
  • Some links are Google redirect links (https://www.google.com/goto?...): 5 of the 8 in this answer. The free link resolver returns the real address.

Cost and docs

A Google search is 1 credit, $0.25–$0.50 per 1,000 depending on the pack. Failed requests are not charged, and new accounts get 500 free credits.