Integrate search data into your Go application. This library is the official wrapper for SerpApi.
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.
Go 1.10 is required.
go get -u github.com/serpapi/serpapi-golang
import "github.com/serpapi/serpapi-golang"
auth := map[string]string{
"api_key": "secret_api_key"
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google",
"q": "coffee"
}
data, err := client.Search(parameter)
fmt.Println(data)
This example runs a search for "coffee" on Google. It then returns the results a Go Hash. See the playground to generate your own code.
func main() {
// serpapi client created with default parameters
auth := map[string]string{
"api_key": "secret_key",
"timeout": "30",
"engine": "google",
}
client := serpapi.NewClient(auth)
// We recommend that you keep your keys safe.
// At least, don't commit them in plain text.
// More about configuration via environment variables:
// https://hackernoon.com/all-the-secrets-of-encrypting-api-keys-in-golang-revealed-5qf3t5l
// search query overview (more fields available depending on search engine)
parameter := map[string]string{
"q": "Coffee",
"location": "Portland, Oregon, United States",
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"safe": "active",
"start": "10",
"num": "10",
"device": "desktop",
}
// formated search results as a Hash
// serpapi.com converts HTML -> JSON
rsp, err := client.Search(parameter)
if err != nil {
panic(err)
}
fmt.Println(rsp)
// raw search engine html as a String
// serpapi.com acts a proxy to provive high throughputs, no search limit and more.
raw_html, err := client.Html(parameter)
if err != nil {
panic(err)
}
fmt.Println(raw_html)
}
Google search documentation. More hands on examples are available below.
client := serpapi.NewClient(map[string]string{})
rsp, err := client.Location("Austin", 3)
if err != nil {
panic(err)
}
fmt.Println(rsp)
it prints the first 3 locations matching Austin (Texas, Texas, Rochester)
[map[canonical_name:Austin,TX,Texas,United States country_code:US google_id:200635 google_parent_id:21176 gps:[-97.7430608 30.267153]...
NOTE: api_key is not required for this endpoint.
This API allows retrieving previous search results. To fetch earlier results from the search_id.
First, you need to run a search and save the search id.
// First, you need to run a search and save the search id.
auth := map[string]string{
"engine": "google",
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"q": "Coffee",
"location": "Portland"}
rsp, err := client.Search(parameter)
if err != nil {
t.Error("unexpected error", err)
return
}
// Now let's retrieve the previous search results from the archive.
searchID := rsp["search_metadata"].(map[string]interface{})["id"].(string)
if len(searchID) == 0 {
t.Error("search_metadata.id must be defined")
return
}
searchArchive, err := client.SearchArchive(searchID)
if err != nil {
t.Error(err)
return
}
searchIDArchive := searchArchive["search_metadata"].(map[string]interface{})["id"].(string)
if searchIDArchive != searchID {
t.Error("search_metadata.id do not match", searchIDArchive, searchID)
}
This code prints the search results from the archive. :)
auth := map[string]string{
"api_key": "<secret_api_key>"
}
client := serpapi.NewClient(auth)
rsp, err = client.Account()
fmt.Println(rsp)
It prints your account information.
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "bing",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 1 {
fmt.Println("expect more than 1 organic_results")
return
}
}
- source code: test/example_search_bing_test.go see: https://serpapi.com/bing-search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "baidu",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
- source code: test/example_search_baidu_test.go see: https://serpapi.com/baidu-search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "yahoo",
"p": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
- source code: test/example_search_yahoo_test.go see: https://serpapi.com/yahoo-search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "youtube",
"search_query": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["video_results"] == nil {
fmt.Println("key is not found: video_results")
return
}
if len(rsp["video_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 video_results")
return
}
}
- source code: test/example_search_youtube_test.go see: https://serpapi.com/youtube-search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "walmart",
"query": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
- source code: test/example_search_walmart_test.go see: https://serpapi.com/walmart-search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "ebay",
"_nkw": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
- source code: test/example_search_ebay_test.go see: https://serpapi.com/ebay-search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "naver",
"query": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["ads_results"] == nil {
fmt.Println("key is not found: ads_results")
return
}
if len(rsp["ads_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 ads_results")
return
}
}
- source code: test/example_search_naver_test.go see: https://serpapi.com/naver-search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "home_depot",
"q": "table", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["products"] == nil {
fmt.Println("key is not found: products")
return
}
if len(rsp["products"].([]interface{})) < 5 {
fmt.Println("expect more than 5 products")
return
}
}
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "apple_app_store",
"term": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "duckduckgo",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
- source code: test/example_search_google_test.go see: https://serpapi.com/search-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_scholar",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 organic_results")
return
}
}
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_autocomplete",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["suggestions"] == nil {
fmt.Println("key is not found: suggestions")
return
}
if len(rsp["suggestions"].([]interface{})) < 5 {
fmt.Println("expect more than 5 suggestions")
return
}
}
- source code: test/example_search_google_autocomplete_test.go see: https://serpapi.com/google-autocomplete-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_product",
"q": "coffee",
"product_id": "4887235756540435899", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["product_results"] == nil {
fmt.Println("key is not found: product_results")
return
}
if len(rsp["product_results"].(map[string]interface{})) < 5 {
fmt.Println("expect more than 5 product_results")
return
}
}
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_reverse_image",
"image_url": "https://i.imgur.com/5bGzZi7.jpg", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["image_sizes"] == nil {
fmt.Println("key is not found: image_sizes")
return
}
if len(rsp["image_sizes"].([]interface{})) < 1 {
fmt.Println("expect more than 1 image_sizes")
return
}
}
- source code: test/example_search_google_reverse_image_test.go see: https://serpapi.com/google-reverse-image
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_events",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["events_results"] == nil {
fmt.Println("key is not found: events_results")
return
}
if len(rsp["events_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 events_results")
return
}
}
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_local_services",
"q": "electrician",
"data_cid": "6745062158417646970", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["local_ads"] == nil {
fmt.Println("key is not found: local_ads")
return
}
if len(rsp["local_ads"].([]interface{})) < 5 {
fmt.Println("expect more than 5 local_ads")
return
}
}
- source code: test/example_search_google_local_services_test.go see: https://serpapi.com/google-local-services-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_maps",
"q": "pizza",
"ll": "@40.7455096,-74.0083012,15.1z",
"type": "search", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["local_results"] == nil {
fmt.Println("key is not found: local_results")
return
}
if len(rsp["local_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 local_results")
return
}
}
- source code: test/example_search_google_maps_test.go see: https://serpapi.com/google-maps-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_jobs",
"q": "coffee", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["jobs_results"] == nil {
fmt.Println("key is not found: jobs_results")
return
}
if len(rsp["jobs_results"].([]interface{})) < 5 {
fmt.Println("expect more than 5 jobs_results")
return
}
}
- source code: test/example_search_google_jobs_test.go see: https://serpapi.com/google-jobs-api
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
auth := map[string]string{
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google_play",
"q": "kite",
"store": "apps", }
rsp, err := client.Search(parameter)
if err != nil {
fmt.Println("unexpected error", err)
return
}
if rsp["search_metadata"].(map[string]interface{})["status"] != "Success" {
fmt.Println("bad status")
return
}
if rsp["organic_results"] == nil {
fmt.Println("key is not found: organic_results")
return
}
if len(rsp["organic_results"].([]interface{})) < 1 {
fmt.Println("expect more than 1 organic_results")
return
}
}
- source code: test/example_search_google_play_test.go see: https://serpapi.com/google-play-api
Search API features non-blocking search using the option: async=true
.
- Non-blocking - async=true - a single parent process can handle unlimited concurrent searches.
- Blocking - async=false - many processes must be forked and synchronized to handle concurrent searches. This strategy is I/O usage because each client would hold a network connection.
Search API enables async
search.
- Non-blocking (
async=true
) : the development is more complex, but this allows handling many simultaneous connections. - Blocking (
async=false
) : it's easy to write the code but more compute-intensive when the parent process needs to hold many connections.
Here is an example of asynchronous searches using Go
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
import (
"fmt"
"os"
serpapi "github.com/serpapi/serpapi-golang"
)
/***
* Demonstrate how to run a search on Google.
*
* go get -u github.com/serpapi/serpapi-golang
*/
func main() {
api_key := os.Getenv("API_KEY")
if len(api_key) == 0 {
println("you must obtain an api_key from serpapi\n and set the environment variable API_KEY\n $ export API_KEY='secret api key'")
}
auth := map[string]string{
"api_key": api_key,
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google",
"q": "Coffee",
"location": "Austin,Texas",
}
fmt.Println("search is running")
data, err := client.Search(parameter)
if err != nil {
panic(err)
}
// decode data and display the first organic result title
results := data["organic_results"].([]interface{})
fmt.Println("search first result:")
firstResult := results[0].(map[string]interface{})
fmt.Println(firstResult["title"].(string))
fmt.Println("ok: oobt test passed")
}
- source code: oobt/demo.go
This code shows a simple solution to batch searches asynchronously into a queue.
Each search takes a few seconds before completion by SerpApi service and the search engine. By the time the first element pops out of the queue. The search result might be already available in the archive. If not, the search_archive
method blocks until the search results are available.
Go versions validated by Github Actions:
- 3.1
- 2.6 see: Github Actions.
- [2023-09-01] 1.0.0 Full API support
- Brand centric instead of search engine based
- No hard-coded logic per search engine
- Simple HTTP client (lightweight, reduced dependency)
- No magic default values
- Thread safe
- Easy extension
- Defensive code style (raise a custom exception)
- TDD
- Best API coding practice per platform
- KiSS principles
This project source code and coding style was inspired by Go itself. This programming language native provides all the recommendation to build awesome software.
- 0 lint offense:
make lint
- 100% tests passing:
make test
- 100% code coverage:
make test
classDiagram
Application *-- serpapi
serpapi *-- Client
class Client {
engine String
api_key String
params Hash
search() Hash
html() String
location() String
search_archive() Hash
account() Hash
}
openuri <.. Client
json <.. Client
Go <.. openuri
Go <.. json
sequenceDiagram
Client->>SerpApi.com: search() : http request
SerpApi.com-->>SerpApi.com: query search engine
SerpApi.com-->>SerpApi.com: parse HTML into JSON
SerpApi.com-->>Client: JSON string payload
Client-->>Client: decode JSON into Hash
where:
- The end user implements the application.
- Client refers to SerpApi:Client.
- SerpApi.com is the backend HTTP / REST service.
- Engine refers to Google, Baidu, Bing, and more.
The SerpApi.com service (backend)
- executes a scalable search on
engine: "google"
using the search query:q: "coffee"
. - parses the messy HTML responses from Google on the backend.
- returns a standardized JSON response. The class SerpApi::Client (client side / golang):
- Format the request to SerpApi.com server.
- Execute HTTP Get request.
- Parse JSON into Go Hash using a standard JSON library. Et voila!
We love "true open source" and "continuous integration", and Test Drive Development (TDD). We are using RSpec to test [our infrastructure around the clock]) using Github Action to achieve the best QoS (Quality Of Service).
The directory spec/ includes specification which serves the dual purposes of examples and functional tests.
Set your secret API key in your shell before running a test.
export API_KEY="your_secret_key"
Install testing dependency
$ make
Contributions are welcome. Feel to submit a pull request!
MIT License.