Short answer: Java 11 and later can call a Google search API with the built-in java.net.http.HttpClient — POST a small JSON body to /google/search with your key in the X-API-Key header, then read the answer with Jackson. The class below ran against the API response we recorded on 4 December 2026 for "trail running shoes" and printed its 8 organic results.
The request, first as curl
curl -X POST 'https://api.serpbase.dev/google/search' \
-H 'Content-Type: application/json' \
-H 'X-API-Key: YOUR_API_KEY' \
-d '{"q":"trail running shoes","gl":"us","hl":"en","device":"desktop"}'
The Java class
Needs Java 11+ and com.fasterxml.jackson.core:jackson-databind (we used 2.16.1 on JDK 17). Put your key in the SERPBASE_API_KEY environment variable — YOUR_API_KEY in the curl line above is the same value.
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;
public class GoogleSearch {
private static final String ENDPOINT = "https://api.serpbase.dev/google/search";
private static final ObjectMapper JSON = new ObjectMapper();
private static final HttpClient HTTP = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
static JsonNode search(String query) throws Exception {
String body = JSON.writeValueAsString(Map.of(
"q", query, "gl", "us", "hl", "en", "device", "desktop"));
HttpRequest request = HttpRequest.newBuilder(URI.create(ENDPOINT))
.timeout(Duration.ofSeconds(60))
.header("Content-Type", "application/json")
.header("X-API-Key", System.getenv("SERPBASE_API_KEY"))
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = HTTP.send(request, HttpResponse.BodyHandlers.ofString());
JsonNode result = JSON.readTree(response.body());
// The HTTP code is 200 even when a search fails: check "status" in the body.
if (result.path("status").asInt(-1) != 0) {
throw new IllegalStateException("search failed: status "
+ result.path("status").asText() + " " + result.path("error").asText());
}
return result;
}
public static void main(String[] args) throws Exception {
JsonNode result = search("trail running shoes");
for (JsonNode item : result.path("organic")) {
System.out.printf("%2d %s%n %s%n",
item.path("rank").asInt(),
item.path("title").asText(),
item.path("display_link").asText());
}
System.out.println("credits charged: " + result.path("credits_charged").asInt());
}
}
What it printed
1 Best Trail Running Shoes
https://rundreamachieve.com › best-trail-running-shoes
2 Best Road To Trail Running Shoes | From Hoka to Altra
https://www.steadyfoot.com › best-road-to-trail-running...
3 7 Best Trail Running Shoes in 2027 – Grip Better, Run Stronger!
www.youtube.com
...
8 Trail Shoes | Black Friday Outdoor Gear
https://www.dickssportinggoods.com › trail-shoes-1aaz...
credits charged: 1
The same answer also held 7 shopping_results, 3 related_searches and 10 social_posts; read them the same way with result.path("shopping_results").
The JSON you are reading
{
"status": 0,
"credits_charged": 1,
"query": "trail running shoes",
"organic": [
{
"rank": 1,
"position": 1,
"title": "Best Trail Running Shoes",
"link": "https://rundreamachieve.com/running-shoe-buying-guides/best-trail-running-shoes/",
"display_link": "https://rundreamachieve.com › best-trail-running-shoes"
}
],
"related_searches": ["how long do trail running shoes typically last", "how to choose trail running shoes"]
}
Things that trip people up
- HTTP 200 does not mean the search worked. Failures also arrive with HTTP 200 and a non-zero
statusin the body, soresponse.statusCode()alone tells you nothing. The codes are listed in the error code guide. - Use
path(), notget(). Sections such aspeople_also_askorai_overvieware only present when Google shows them;path()returns an empty node instead ofnull. - Build the body with a JSON library. Concatenating strings breaks on the first query that contains a quote.
- Create one
HttpClientand reuse it. It keeps connections open between calls. - Set a request timeout. A live search takes seconds, not milliseconds; we use 60 seconds here.
- Some links are Google redirect links. In this answer 5 of 8
linkvalues started withhttps://www.google.com/goto?;display_linkstill shows the site, and the free link resolver returns the real address.
Next steps
- More pages: add
"page", 2to the body — see pagination. - Many keywords at once: respect the per-second limit described in rate limits.
- Other languages: Python, Node.js and Go examples.
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.
- Parameters and fields: Google Web Search docs
- Get a key: how to get and send an API key
- Credit packs: pricing