Skip to content

serpapi/serpapi-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SerpApi Go Library

serpapi-go

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.

Installation

Go 1.10 is required.

go get -u github.com/serpapi/serpapi-golang

Simple Usage

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.

Advanced Usage

Search API

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.

Documentations

Location API

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.

Search Archive API

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. :)

Account API

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.

Basic examples in Go

Search bing

 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
  }
}  

Search baidu

 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
  }
}  

Search yahoo

 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
  }
}  

Search youtube

 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
  }
}  

Search walmart

 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
  }
}  

Search ebay

 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
  }
}  

Search naver

 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
  }
}  

Search home depot

 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
  }
}  

Search apple app store

 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
  }
}  

Search duckduckgo

 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
  }
}  

Search google

 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
  }
}  

Search google scholar

 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
  }
}  

Search google autocomplete

 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
  }
}  

Search google product

 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
  }
}  

Search 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_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
  }
}  

Search google events

 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
  }
}  

Search google local services

 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
  }
}  

Search google maps

 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
  }
}  

Search google jobs

 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
  }
}  

Search google play

 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
  }
}  

Advanced search API usage

Highly scalable batching

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")
}

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.

Supported Go version.

Go versions validated by Github Actions:

Change logs

  • [2023-09-01] 1.0.0 Full API support

Developer Guide

Key goals

  • 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

Inspirations

This project source code and coding style was inspired by Go itself. This programming language native provides all the recommendation to build awesome software.

Code quality expectations

  • 0 lint offense: make lint
  • 100% tests passing: make test
  • 100% code coverage: make test

Developer Guide

Design : UML diagram

Class diagram

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
Loading

search() : Sequence diagram

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
Loading

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!

Continuous integration

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!

License

MIT License.

About

SerpApi client implementation in Golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published