-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREADME.md.erb
More file actions
615 lines (449 loc) · 22.6 KB
/
Copy pathREADME.md.erb
File metadata and controls
615 lines (449 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# SerpApi Java Library
[](https://github.com/serpapi/serpapi-java/actions/workflows/gradle.yml)
[](https://jitpack.io/#serpapi/serpapi-java)
Integrate search data into your AI workflow, RAG, fine-tuning, or Java application using this official [SerpApi Java SDK](https://serpapi.com/integrations/java).
[SerpApi](https://serpapi.com/) supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and many more.
## Installation
Installation of the serpapi-java package is done using Maven / Gradle.
Any new project should include these lines in a `build.gradle` file.
```gradle
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
implementation 'com.github.serpapi:serpapi-java:1.2.0'
}
```
To list all available versions see:
https://jitpack.io/api/builds/com.github.serpapi/serpapi-java
or you can download the jar file from https://github.com/serpapi/serpapi-java/releases.
_Note: JitPack builds Maven artifacts from GitHub releases and tags._
## Quickstart Tutorial
[Create a SerpApi account](https://serpapi.com/dashboard) to get your API key, then store it in an environment variable:
```export SERPAPI_KEY="your_api_key"```
To try the library quickly, use the demo project:
```bash
git clone https://github.com/serpapi/serpapi-java.git
cd serpapi-java/demo
make all SERPAPI_KEY='<your private key>'
```
Use quotes if your key contains shell-special characters.
The `serpapi-java` package is already installed inside the `build.gradle` file of this cloned `serpapi-java` repository.
So, in this tutorial, no extra setup is needed.
For future projects please refer to the above provided 'Installation' section.
`demo/src/main/java/demo/App.java`:
```java
class App {
public static void main(String[] args) {
String apiKey = System.getenv("SERPAPI_KEY");
String location = "Austin,Texas";
String engine = "google";
System.out.println("find the first coffee shop in " + location + " using " + engine);
Map<String, String> auth = new HashMap<>();
auth.put("engine", engine);
auth.put("api_key", apiKey);
SerpApi serpapi = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "Coffee");
parameter.put("location", location);
try {
JsonObject data = serpapi.search(parameter);
JsonArray organic = data.getAsJsonArray("organic_results");
JsonObject first = organic.get(0).getAsJsonObject();
System.out.println("First result: " + first.get("title").getAsString() + " (search near " + location + ")");
} catch (SerpApiException e) {
System.out.println("SerpApi request failed.");
e.printStackTrace();
System.exit(1);
}
}
}
```
## Features
- Asynchronous searches for submitting non-blocking jobs and retrieving completed results from the Search Archive API
- Search results stored as a [Gson](https://github.com/google/gson) for JSON and returns responses as Gson `JsonObject` / `JsonArray` with `search`, token-efficient Markdown with `md`, or raw search-engine HTML with `html`
- SDK methods for the [Location API](https://serpapi.com/locations-api), [Search Archive API](https://serpapi.com/search-archive-api), and [Account API](https://serpapi.com/account-api)
## Response Formats
Use `search` for structured results decoded into a `Gson JsonObject / JsonArray`:
```results = client.search(parameter);```
Use `md` for a token-efficient Markdown String optimized for LLMs and AI agents:
```markdown = client.markdown(parameter);```
Use `html` when you need the raw search-engine response:
```rawHtml = client.html(parameter);```
Learn more about [SerpApi Markdown output](https://serpapi.com/markdown-output).
> A couple of notes:
> - `client` is a `SerpApi` instance you create with your API key, e.g. `SerpApi client = new SerpApi(auth);` where `auth` is a `Map<String, String>` containing `"api_key"` (and typically `"engine"`).
> - `parameter` is a `Map<String, String>` with your search parameters (`q`, `location`, etc.).
> - `client.markdown(parameter)` is new as of `serpapi-java` **1.2.0** - make sure your `build.gradle` dependency is at least that version
## Requirements
This library uses [Gson](https://github.com/google/gson) for JSON and returns responses as Gson `JsonObject` / `JsonArray`.
**This repository** is built and tested with **JDK 21** and the **Gradle wrapper** (`./gradlew`, currently Gradle 9.1.0). Use the wrapper so you do not need a separate Gradle install.
**Consumers** of the JitPack artifact should run a JVM whose version is at least the **bytecode level** of the release you depend on (releases from this branch target **Java 21**).
## Configuration
Set defaults when creating a client, then override search parameters in individual calls:
```java
Map<String, String> auth = new HashMap<>();
auth.put("api_key", System.getenv("SERPAPI_KEY"));
auth.put("engine", "google");
auth.put("hl", "en");
auth.put("gl", "us");
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
parameter.put("gl", "gb");
parameter.put("async", "false");
try {
JsonObject results = client.search(parameter);
} catch (SerpApiException e) {
e.printStackTrace();
}
```
| Field | Default | Description |
| :-------- | :-----: | :------------------------------------------------------------------------------------------------ |
| `api_key` | None | Your SerpApi API key. Use an environment variable rather than committing it to source control. |
| `engine` | None | The search engine used by default, such as google or google_maps. |
| `async` | false | Submits searches without waiting for them to complete. It can be set on the client or per search. |
Search-engine-specific parameters can also be supplied when creating the client or calling `search`. Parameters passed to search override client defaults.
### Search Asynchronous
Search API features non-blocking search using the option: `async=true`.
- Non-blocking - `async=true` - a single thread can submit many searches without waiting for each one to complete, then collects results later from the Search Archive API.
- Blocking - `async=false` - each search call blocks the calling thread until results are ready. To run searches concurrently, you'd need multiple threads (i.e. through an `ExecutorService`), each holding its own connection open for the duration of its search. This is more I/O-intensive, since concurrency requires as many held-open connections as concurrent searches.
Here is an example simulation of asynchronous searches using Java:
```java
String apiKey = System.getenv("SERPAPI_KEY");
Map<String, String> auth = new HashMap<>();
auth.put("engine", "google");
auth.put("api_key", apiKey);
auth.put("async", "true");
SerpApi client = new SerpApi(auth);
Queue<String> ids = new LinkedList<>();
for (String company : new String[]{"meta", "amazon", "apple", "netflix", "google"}) {
Map<String, String> parameter = new HashMap<>();
parameter.put("q", company);
JsonObject result = client.search(parameter);
ids.add(result.getAsJsonObject("search_metadata").get("id").getAsString());
}
System.out.println("waiting 10s for searches to complete...");
// for production use cases, continuous polling instead of waiting is necessary
Thread.sleep(10000);
while (!ids.isEmpty()) {
JsonObject archived = client.searchArchive(ids.poll());
String status = archived.getAsJsonObject("search_metadata").get("status").getAsString();
String company = archived.getAsJsonObject("search_parameters").get("q").getAsString();
System.out.println(company + ": " + status);
}
```
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
Each search may take a few seconds to complete. By the time the first element pops out of the queue, the search results might already be available in the archive.
If not, the `searchArchive` method blocks until the search results are available.
## Examples
Here are some examples for some of our most popular APIs. You can find the full list of supported engines and parameters in our [documentation](https://serpapi.com/search-engine-apis).
### Google Shopping
Scrape Google Shopping results with product names, prices, ratings, and merchant information.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "google_shopping";
Map<String, String> auth = new HashMap<>();
auth.put("engine", engine);
auth.put("api_key", apiKey);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "Macbook M4");
try {
JsonObject data = client.search(parameter);
JsonArray results = data.getAsJsonArray("shopping_results");
if (results != null && results.size() > 0) {
JsonObject first = results.get(0).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(first));
}
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/GoogleShoppingTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/GoogleShoppingTest.java)
[See documentation](https://serpapi.com/google-shopping-api)
#### Google Shopping Light
A [light variant](https://serpapi.com/google-shopping-light-api) engine called `google_shopping_light` is also available for faster, lower-cost shopping searches.
### Google Images
Scrape Google Images search results, including image URLs, thumbnails, titles, and source pages.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "google_images";
Map<String, String> auth = new HashMap<>();
auth.put("engine", engine);
auth.put("api_key", apiKey);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
try {
JsonObject data = client.search(parameter);
JsonArray results = data.getAsJsonArray("images_results");
if (results != null && results.size() > 0) {
JsonObject first = results.get(0).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(first));
}
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/GoogleImagesTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/GoogleImagesTest.java)
[See documentation](https://serpapi.com/images-results)
#### Google Images Light
A [light variant](https://serpapi.com/google-images-light-api) engine called `google_images_light` is also available for faster, lower-cost image searches.
### Google Lens
Scrape results from the Google Lens page when performing an image search. The results related to the image could contain visual matches and other data.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "google_lens";
Map<String, String> auth = new HashMap<>();
auth.put("engine", engine);
auth.put("api_key", apiKey);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("url", "https://i.imgur.com/your_image.png");
try {
JsonObject data = client.search(parameter);
JsonArray visualMatches = data.getAsJsonArray("visual_matches");
System.out.println(visualMatches);
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/GoogleLensTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/GoogleLensTest.java)
[See Google Lens image upload documentation](https://serpapi.com/google-lens-upload-an-image)
### Google Trends
Track search interest over time and compare the popularity of search terms.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "google_trends";
Map<String, String> auth = new HashMap<>();
auth.put("engine", engine);
auth.put("api_key", apiKey);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
parameter.put("data_type", "TIMESERIES");
try {
JsonObject data = client.search(parameter);
JsonObject interestOverTime = data.getAsJsonObject("interest_over_time");
if (interestOverTime != null) {
JsonArray timelineData = interestOverTime.getAsJsonArray("timeline_data");
if (timelineData != null && timelineData.size() > 0) {
JsonObject first = timelineData.get(0).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(first));
}
}
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/GoogleTrendsTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/GoogleTrendsTest.java)
[See documentation](https://serpapi.com/google-trends-api)
### Google Flights
Search flight routes, schedules, prices, and booking options.
_Note: The `google_flights` engine does not use `q`. Specify route and date parameters such as `departure_id`, `arrival_id`, `outbound_date`, and `return_date`._
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "google_flights";
Map<String, String> auth = new HashMap<>();
auth.put("engine", engine);
auth.put("api_key", apiKey);
SerpApi client = new SerpApi(auth);
String outboundDate = LocalDate.now().plusDays(7).toString();
String returnDate = LocalDate.now().plusDays(14).toString();
Map<String, String> parameter = new HashMap<>();
parameter.put("departure_id", "LAX");
parameter.put("arrival_id", "AUS");
parameter.put("outbound_date", outboundDate);
parameter.put("return_date", returnDate);
try {
JsonObject data = client.search(parameter);
JsonArray bestFlights = data.getAsJsonArray("best_flights");
//JsonArray otherFlights = data.getAsJsonArray("other_flights");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(bestFlights));
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/GoogleFlightsTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/GoogleFlightsTest.java)
[See documentation](https://serpapi.com/google-flights-api)
### Google AI Mode API
The Google AI Mode API returns AI-generated answers with structured text blocks, references, images, products, and more.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "google_ai_mode";
Map<String, String> auth = new HashMap<>();
auth.put("engine", engine);
auth.put("api_key", apiKey);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "best coffee maker");
try {
String markdownData = client.markdown(parameter);
System.out.println(markdownData);
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/GoogleAIModeTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/GoogleAIModeTest.java)
[See documentation](https://serpapi.com/google-ai-mode-api)
### Bing Search
Scrape Bing web search results, including organic results, ads, related searches, and more.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "bing";
Map<String, String> auth = new HashMap<>();
auth.put("api_key", apiKey);
auth.put("engine", engine);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
try {
JsonObject results = client.search(parameter);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(results));
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/BingTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/BingTest.java)
[See documentation](https://serpapi.com/bing-search-api)
### DuckDuckGo Search
Scrape DuckDuckGo search results, including organic results, ads, knowledge graphs, and related searches.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "duckduckgo";
Map<String, String> auth = new HashMap<>();
auth.put("api_key", apiKey);
auth.put("engine", engine);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
try {
JsonObject results = client.search(parameter);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(results));
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/DuckduckgoTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/DuckduckgoTest.java)
[See documentation](https://serpapi.com/duckduckgo-search-api)
### Baidu Search
Scrape Baidu search results, including organic results, answer boxes, and related searches.
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "baidu";
Map<String, String> auth = new HashMap<>();
auth.put("api_key", apiKey);
auth.put("engine", engine);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
try {
JsonObject results = client.search(parameter);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(results));
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/BaiduTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/BaiduTest.java)
[See documentation](https://serpapi.com/baidu-search-api)
### Amazon Search
Scrape Amazon product search results, including product names, prices, ratings, reviews, and availability.
_Note: The `amazon` engine uses the `k` parameter for a keyword search, not `q`._
```java
String apiKey = System.getenv("SERPAPI_KEY");
String engine = "amazon";
Map<String, String> auth = new HashMap<>();
auth.put("api_key", apiKey);
auth.put("engine", engine);
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("k", "coffee");
parameter.put("amazon_domain", "amazon.com");
try {
JsonObject data = client.search(parameter);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(data));
} catch (SerpApiException e) {
e.printStackTrace();
System.exit(1);
}
```
Source code: [src/test/java/serpapi/example/AmazonSearchTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/example/AmazonSearchTest.java)
[See documentation](https://serpapi.com/amazon-search-api)
## Migration from google-search-results-java
If you are upgrading from the legacy [`google-search-results-java`](https://github.com/serpapi/google-search-results-java) library, here is a summary of what changed.
### Dependency
```gradle
// before
implementation 'com.github.serpapi:google-search-results-java:2.0.0'
// after
implementation 'com.github.serpapi:serpapi-java:1.2.0'
```
### Class and method renames
| Old (`google-search-results-java`) | New (`serpapi-java`) |
| ---------------------------------- | ------------------------------------------------------------------------------------ |
| `GoogleSearch` | `SerpApi` |
| `SerpApiSearch` | `SerpApi` |
| `client.getJson()` | `client.search(parameter)` |
| `client.getHtml()` | `client.html(parameter)` |
| — | `client.markdown(parameter)` — new in 1.2.0 |
| `client.getSearchArchive(id)` | `client.searchArchive(id)` |
| `client.getAccount()` | `client.account()` |
| `client.getLocation(parameter)` | `client.location(parameter)` |
| `SerpApiSearchException` | `SerpApiException` |
### Example
```java
// before
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
parameter.put("api_key", "your_api_key");
GoogleSearch search = new GoogleSearch(parameter);
JsonObject results = search.getJson();
// after
Map<String, String> auth = new HashMap<>();
auth.put("api_key", "your_api_key");
SerpApi client = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
parameter.put("engine", "google");
JsonObject results = client.search(parameter);
```
## Documentation
SerpApi supports Google Search, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, Apple App Store, and many other APIs. Browse the [SerpApi](https://serpapi.com/search-api) documentation to find supported APIs and parameters, or use the [Playground](https://serpapi.com/playground) to build a request and generate code.
Additional SDK resources:
- [Java SDK integration page](https://serpapi.com/integrations/java)
- [Java package](https://github.com/serpapi/serpapi-java)
- [SerpApi status](https://serpapi.com/status)
## TLS / HTTPS and older JVMs
### Symptom
`javax.net.ssl.SSLHandshakeException`
### Cause
SerpApi is served over **HTTPS (TLS)**. Very old JRE/JDK builds may lack the TLS versions or cipher suites required to connect.
### Solution
Use a **current JDK** (this project is tested on **JDK 21**). On macOS you can select an installed JDK, for example:
```sh
/usr/libexec/java_home -V
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
java -version
```
On Windows, install a current JDK from your vendor and point `JAVA_HOME` at it.
## Contributing
Contributions are welcome. Make sure to read our [contributing guide](https://github.com/serpapi/serpapi-java/blob/master/CONTRIBUTING.md)
© 2026 [SerpApi](https://serpapi.com/)