A simple Java application that fetches Google search autocomplete suggestions using SerpApi and Google Gson.
- A JDK (tested with Java 26)
- A SerpApi account and API key (sign up here)
Add the SerpApi Java library and Google Gson to build.gradle. See the
SerpApi Java library docs for the
current dependency line.
The app reads your key from an environment variable named SERPAPI_KEY.
Never hard-code the key or commit it to Git.
Linux/macOS
export SERPAPI_KEY="your_api_key_here"Windows (PowerShell)
$env:SERPAPI_KEY="your_api_key_here"Windows (Command Prompt)
set SERPAPI_KEY=your_api_key_here./gradlew runEach run makes one SerpApi search, which counts as part of your plan's quota.
Example output:
Autocomplete suggestions:
* software engineers are using ai
* software engineers are using linux
ScrapeAutocomplete sends the query "software engineers are using " to
SerpApi's Google Autocomplete engine and prints each suggestion. To try a
different phrase, change the "q" value in findTools().
./gradlew testThe test calls the real API, so SERPAPI_KEY must be set and it uses one search.