Integrate search data into your AI workflow, RAG / fine-tuning, or Ruby application using this official wrapper for SerpApi.
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.
Query a vast range of data at scale, including web search results, flight schedules, stock market data, news headlines, and more.
persistent→ Keep socket connection open to save on SSL handshake / reconnection (2x faster). Search at scaleasync→ Support non-blocking job submission. Search Asynchronous- extensive documentation → easy to follow
- real world examples → included throughout
Ruby 2.7 and higher are supported. To achieve an optimal performance, the latest version is recommended. Check 2.7.8 vs 3.4.4 performance comparison.
Other versions, such as Ruby 1.9, Ruby 2.x, and JRuby, are compatible with legacy SerpApi library, which is still supported. To upgrade to the latest library, check our migration guide.
gem'serpapi','~> 1.0','>= 1.0.3'$ gem install serpapirequire'serpapi'client=SerpApi::Client.new(engine: "google",api_key: "<SERPAPI_KEY>")results=client.search(q: "coffee")ppresultsThis example runs a search for "coffee" on Google. It then returns the results as a regular Ruby Hash. See the playground to generate your own code.
The SerpApi key can be obtained from serpapi.com/signup.
Environment variables are a secure, safe, and easy way to manage secrets.
Set export SERPAPI_KEY=<secret_serpapi_key> in your shell.
Ruby accesses these variables from ENV['SERPAPI_KEY'].
This example dives into all the available parameters for the Google search engine. The list of parameters depends on the chosen search engine.
# load gemrequire'serpapi'# serpapi client created with default parametersclient=SerpApi::Client.new(engine: 'google',api_key: ENV['SERPAPI_KEY'],# HTTP client configurationasync: false,# non-blocking HTTP request see: Search Asynchronous (default: false)persistent: true,# leave socket connection open for faster response time see: Search at scale (default: true)timeout: 5,# HTTP timeout in seconds on the client side only. (default: 120s)symbolize_names: true# turn on/off JSON keys to symbols (default: on, more efficient))# search query overview (more fields available depending on search engine)params={# overview of parameter for Google search engine which is one of many search engine supported.# select the search engine (full list: https://serpapi.com/)engine: "google",# actual search queryq: "Coffee",# then adds search engine specific options.# for example: google specific parameters: https://serpapi.com/search-apigoogle_domain: "Google Domain",# example: Portland,Oregon,United States [ * doc: Location API](#Location-API)location: "Location Requested",device: "desktop|mobile|tablet",hl: "Google UI Language",gl: "Google Country",safe: "Safe Search Flag",start: "Pagination Offset",tbm: "nws|isch|shop",tbs: "custom to be client criteria",}# search results as a symbolized Hash (per performance)results=client.search(params)# search results as a raw HTML stringraw_html=client.html(params)This library is well documented, and you can find the following resources:
- Full documentation on SerpApi.com
- Library Github page
- Library GEM page
- Library API documentation
- API health status
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 is 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 Ruby
require'serpapi'company_list=%w[metaamazonapplenetflixgoogle]client=SerpApi::Client.new(engine: 'google',async: true,persistent: true,api_key: ENV['SERPAPI_KEY'])schedule_search=Queue.newresult=nilcompany_list.eachdo |company|
result=client.search(q: company)puts"#{company}: search results found in cache for: #{company}"ifresult[:search_metadata][:status] =~ /Cached/schedule_search.push(result[:search_metadata][:id])endputs"Last search submited at: #{result[:search_metadata][:created_at]}"puts'wait 10s for all requests to be completed 'sleep(10)puts'wait until all searches are cached or success'untilschedule_search.empty?search_id=schedule_search.popsearch_archived=client.search_archive(search_id)company=search_archived[:search_parameters][:q]ifsearch_archived[:search_metadata][:status] =~ /Cached|Success/puts"#{search_archived[:search_parameters][:q]}: search results found in archive for: #{company}"nextendschedule_search.push(search_id)endschedule_search.closeputs'done'- source code: demo/demo_async.rb
This code shows a simple solution to batch searches asynchronously into a queue. Each search may take up to 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 search_archive method blocks until the search results are available.
The provided code snippet is a Ruby spec test case that demonstrates the use of thread pools to execute multiple HTTP requests concurrently.
require'serpapi'require'connection_pool'# create a thread pool of 4 threads with a persistent connection to serpapi.compool=ConnectionPool.new(size: n,timeout: 5)doSerpApi::Client.new(engine: 'google',api_key: ENV['SERPAPI_KEY'],timeout: 30,persistent: true)end# run user thread to search for your favorites coffee typethreads=%w(latteespressocappuccinoamericanomochamacchiatofrappuccinocold_brew).mapdo |query|
Thread.newdopool.with{ |socket| socket.search({q: query}).to_s}endendresponses=threads.map(&:value)The code aims to demonstrate how thread pools can be used to improve performance by executing multiple tasks concurrently. In this case, it makes multiple HTTP requests to an API endpoint using a thread pool of persistent connections.
Note: gem install connection_pool to run this example.
Benefits:
- Improved performance by avoiding the overhead of creating and destroying connections for each request.
- Efficient use of resources by sharing connections among multiple threads.
- Concurrency and parallelism, allowing multiple requests to be processed simultaneously.
benchmark: (demo/demo_thread_pool.rb)
** Benchmark Ruby 3.4.8 vs Ruby 4.0.0 **
benchmark: (demo/demo_thread_pool.rb)
| ruby | runtime (s) | thread | time/thread (s) |
|---|---|---|---|
| 3.4.8 | 0.018644 | 4 | 0.004661 |
| 4.0.0 | 0.017302 | 4 | 0.004326 |
Ruby 4.0.0 shows a slight improvement over Ruby 3.4.8, but the difference is not significant using thread. Ractor could be considered for a more efficient use of resources but it's still in the experimental stage.
Note: in this benchmark, thread == HTTP connections.
require'serpapi'require'pp'client=SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])params={q: 'coffee'}results=client.search(params)unlessresults[:organic_results]puts'no organic results found'exit1endppresults[:organic_results]puts'done'exit0- source code: demo/demo.rb
require'serpapi'client=SerpApi::Client.newlocation_list=client.location(q: "Austin",limit: 3)puts"number of location: #{location_list.size}"pplocation_listit prints the first 3 locations matching Austin (Texas, Texas, Rochester)
[{:id=>"585069bdee19ad271e9bc072",:google_id=>200635,:google_parent_id=>21176,:name=>"Austin, TX",:canonical_name=>"Austin,TX,Texas,United States",:country_code=>"US",:target_type=>"DMA Region",:reach=>5560000,:gps=>[-97.7430608,30.267153],:keys=>["austin","tx","texas","united","states"]}# ...]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.
require'serpapi'client=SerpApi::Client.new(engine: 'google',api_key: ENV['SERPAPI_KEY'])results=client.search(q: "Coffee",location: "Portland")search_id=results[:search_metadata][:id]Now we can retrieve the previous search results from the archive using the search ID (free of charge).
require'serpapi'client=SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])results=client.search_archive(search_id)ppresultsThis code prints the search results from the archive. :)
require'serpapi'client=SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])ppclient.accountIt prints your account information as:
{account_id: "1234567890",api_key: "your_secret_key",account_email: "email@company.com",account_status: "Active",plan_id: "free",plan_name: "Free Plan",plan_monthly_price: 0.0,searches_per_month: 250,plan_searches_left: 250,extra_credits: 0,total_searches_left: 250,this_month_usage: 0,this_hour_searches: 0,last_hour_searches: 0,account_rate_limit_per_hour: 250}require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/search-api- source code: spec/serpapi/client/example/example_search_google_spec.rb see: https://serpapi.com/search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_light',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/google-light-api- source code: spec/serpapi/client/example/example_search_google_light_spec.rb see: https://serpapi.com/google-light-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_scholar',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'biology'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/google-scholar-api- source code: spec/serpapi/client/example/example_search_google_scholar_spec.rb see: https://serpapi.com/google-scholar-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_autocomplete',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:suggestions]# doc: https://serpapi.com/google-autocomplete-api- source code: spec/serpapi/client/example/example_search_google_autocomplete_spec.rb see: https://serpapi.com/google-autocomplete-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_product',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee',product_id: '4887235756540435899'})# print the output of the response in formatted JSONppresults[:product_results]# doc: https://serpapi.com/google-product-api- source code: spec/serpapi/client/example/example_search_google_product_spec.rb see: https://serpapi.com/google-product-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_reverse_image',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({image_url: 'https://i.imgur.com/5bGzZi7.jpg'})# print the output of the response in formatted JSONppresults[:image_sizes]# doc: https://serpapi.com/google-reverse-image- source code: spec/serpapi/client/example/example_search_google_reverse_image_spec.rb see: https://serpapi.com/google-reverse-image
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_events',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:events_results]# doc: https://serpapi.com/google-events-api- source code: spec/serpapi/client/example/example_search_google_events_spec.rb see: https://serpapi.com/google-events-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_local_services',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'electrician',data_cid: '6745062158417646970'})# print the output of the response in formatted JSONppresults[:local_ads]# doc: https://serpapi.com/google-local-services-api- source code: spec/serpapi/client/example/example_search_google_local_services_spec.rb see: https://serpapi.com/google-local-services-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_maps',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'Coffee',ll: '@40.7455096,-74.0083012,14z',type: 'search'})# print the output of the response in formatted JSONppresults[:local_results]# doc: https://serpapi.com/google-maps-api- source code: spec/serpapi/client/example/example_search_google_maps_spec.rb see: https://serpapi.com/google-maps-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_jobs',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:jobs_results]# doc: https://serpapi.com/google-jobs-api- source code: spec/serpapi/client/example/example_search_google_jobs_spec.rb see: https://serpapi.com/google-jobs-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_play',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'kite',store: 'apps'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/google-play-api- source code: spec/serpapi/client/example/example_search_google_play_spec.rb see: https://serpapi.com/google-play-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_images',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({tbm: 'isch',q: 'coffee'})# print the output of the response in formatted JSONppresults[:images_results]# doc: https://serpapi.com/images-results- source code: spec/serpapi/client/example/example_search_google_images_spec.rb see: https://serpapi.com/images-results
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_lens',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({url: 'https://i.imgur.com/HBrB8p0.png'})# print the output of the response in formatted JSONppresults[:visual_matches]# doc: https://serpapi.com/google-lens-api- source code: spec/serpapi/client/example/example_search_google_lens_spec.rb see: https://serpapi.com/google-lens-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_images_light',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'Coffee'})# print the output of the response in formatted JSONppresults[:images_results]# doc: https://serpapi.com/google-images-light-api- source code: spec/serpapi/client/example/example_search_google_images_light_spec.rb see: https://serpapi.com/google-images-light-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_hotels',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'Bali Resorts',check_in_date: '2025-05-26',check_out_date: '2025-05-27',adults: '2',currency: 'USD',gl: 'us',hl: 'en'})# print the output of the response in formatted JSONppresults[:properties]# doc: https://serpapi.com/google-hotels-api- source code: spec/serpapi/client/example/example_search_google_hotels_spec.rb see: https://serpapi.com/google-hotels-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_flights',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({departure_id: 'PEK',arrival_id: 'AUS',outbound_date: '2025-05-26',return_date: '2025-06-01',currency: 'USD',hl: 'en'})# print the output of the response in formatted JSONppresults[:best_flights]# doc: https://serpapi.com/google-flights-api- source code: spec/serpapi/client/example/example_search_google_flights_spec.rb see: https://serpapi.com/google-flights-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_finance',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'GOOG:NASDAQ'})# print the output of the response in formatted JSONppresults[:markets]# doc: https://serpapi.com/google-finance-api- source code: spec/serpapi/client/example/example_search_google_finance_spec.rb see: https://serpapi.com/google-finance-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_ai_overview',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({page_token: 'KIVu-nictZPdjrI4GMeTPdkrWU8cFXV0dBKyKbUgiigy6OgJQawFQapUBpmzvZe9qr2aLYzO6I5vsm-yW0Ip7dPn4__L88efoR8Ff_36i3c87tlzrZamaZVQSkJcdemu5rAscmsbGrLY9X5PkhCLaRkC1VCh6hivs_e1EiaaPA2xIr9r8ixxXqfhEkova0UWlq-jEgnFhJW8UMRRKXsTmyWXiUIJ-2JTJ2jZxnTINvK-8zgJBtEiM4JSEVG0Vw7DW57Qactqdo1PwW_NHv-psiqObMusqpNU7ZM-OFlWFbNWdVxzdtwE_NsBv5YSJMblF5K71vwcgkAqlvk0569vIPXsx0D5pALt0Tbd6yAqUD4jJfxVZYAu0dN8gc6H9MfREVKlyu2WWszcgQx4zCKlD0dGnmJ_wEu6mI5BBfQJHkknc_69LGK8gP5e65BzXTeDDEziu0wH0KitCRdXqK1i_qnXYpZLDV-6ApW7TlzvmoJE585mMs2icNfe4-28-dYBDwVGl31yZNcc9acEefre8kxQ1apS_YLQGFMuZZ7OAPSl_T0cXAD0hZDXTPjDUMp3ehlfAj3fAL2Uu3G55eJyL_isTbLgl7NcPpRLJ5-lLdwWMCDKD-E4FyvHE3CEfTrN0JkAzC8qCliQQ35jiMk5pQ9FFx-6WoU5gmBiqJIKJBW6eRflSYaFMTpXQhDwB8EtQgDMuyJcj-EP9iVwh5nSSA9O3PXh-MWakaC52oRuJREk3dxcmNHd6qeaz_1_uHq8NZMzV3if621rEmkOL62Za4KMnKuhX7XmmesIKAieuSZXXOFPcEXWKG_N71zTgitvTatgm3M1tv_k-l-1ZoEXf3xu-zTZkm_92obr02LIdCKkM_9oyVJMuo2t5Wmx8WBvdsfnfUzJg-2vn6XG4JitSwfRo2l5TTErO_GxnNI4KPtR2YnWMfXXpV0YU1FwWvG7NyOVXlyJvK129AUN6TFI3JPk4MZ4OfLdKNzoShtnpl3RfNxij748svedxMtmmI3e-gc6kgJFVye-qg48j7Rwo71OcbA7dA9-NBe2o2napHMzmuMFQWqr9zSVtJXmKbbej73jI7XHPaymnfBdEIqsmPg6RI_L1URaVmiJuY6N2ZtYb3U3zSen3mjV611h0y3tyDHbi_W_AU9HHA0'})# print the output of the response in formatted JSONppresults[:ai_overview]# doc: https://serpapi.com/google-ai-overview-api- source code: spec/serpapi/client/example/example_search_google_ai_overview_spec.rb see: https://serpapi.com/google-ai-overview-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_news',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'pizza',gl: 'us',hl: 'en'})# print the output of the response in formatted JSONppresults[:news_results]# doc: https://serpapi.com/google-news-api- source code: spec/serpapi/client/example/example_search_google_news_spec.rb see: https://serpapi.com/google-news-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_news_light',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'pizza'})# print the output of the response in formatted JSONppresults[:news_results]# doc: https://serpapi.com/google-news-light-api- source code: spec/serpapi/client/example/example_search_google_news_light_spec.rb see: https://serpapi.com/google-news-light-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_patents',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: '(Coffee)'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/google-patents-api- source code: spec/serpapi/client/example/example_search_google_patents_spec.rb see: https://serpapi.com/google-patents-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_trends',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee,milk,bread,pasta,steak',data_type: 'TIMESERIES'})# print the output of the response in formatted JSONppresults[:interest_over_time]# doc: https://serpapi.com/google-trends-api- source code: spec/serpapi/client/example/example_search_google_trends_spec.rb see: https://serpapi.com/google-trends-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_shopping',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'Macbook M4'})# print the output of the response in formatted JSONppresults[:shopping_results]# doc: https://serpapi.com/google-shopping-api- source code: spec/serpapi/client/example/example_search_google_shopping_spec.rb see: https://serpapi.com/google-shopping-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_immersive_product',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:immersive_product_results]# doc: https://serpapi.com/google-immersive-product-api- source code: spec/serpapi/client/example/example_search_google_immersive_product_spec.rb see: https://serpapi.com/google-immersive-product-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'google_videos',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/google-videos-api- source code: spec/serpapi/client/example/example_search_google_videos_spec.rb see: https://serpapi.com/google-videos-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'amazon',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/amazon-search-api- source code: spec/serpapi/client/example/example_search_amazon_spec.rb see: https://serpapi.com/amazon-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'baidu',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/baidu-search-api- source code: spec/serpapi/client/example/example_search_baidu_spec.rb see: https://serpapi.com/baidu-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'yahoo',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({p: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/yahoo-search-api- source code: spec/serpapi/client/example/example_search_yahoo_spec.rb see: https://serpapi.com/yahoo-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'youtube',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({search_query: 'coffee'})# print the output of the response in formatted JSONppresults[:video_results]# doc: https://serpapi.com/youtube-search-api- source code: spec/serpapi/client/example/example_search_youtube_spec.rb see: https://serpapi.com/youtube-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'walmart',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({query: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/walmart-search-api- source code: spec/serpapi/client/example/example_search_walmart_spec.rb see: https://serpapi.com/walmart-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'ebay',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({_nkw: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/ebay-search-api- source code: spec/serpapi/client/example/example_search_ebay_spec.rb see: https://serpapi.com/ebay-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'naver',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({query: 'coffee'})# print the output of the response in formatted JSONppresults[:ads_results]# doc: https://serpapi.com/naver-search-api- source code: spec/serpapi/client/example/example_search_naver_spec.rb see: https://serpapi.com/naver-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'home_depot',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'table'})# print the output of the response in formatted JSONppresults[:products]# doc: https://serpapi.com/home-depot-search-api- source code: spec/serpapi/client/example/example_search_home_depot_spec.rb see: https://serpapi.com/home-depot-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'apple_app_store',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({term: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/apple-app-store- source code: spec/serpapi/client/example/example_search_apple_app_store_spec.rb see: https://serpapi.com/apple-app-store
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'duckduckgo',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({q: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/duckduckgo-search-api- source code: spec/serpapi/client/example/example_search_duckduckgo_spec.rb see: https://serpapi.com/duckduckgo-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'yandex',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({text: 'coffee'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/yandex-search-api- source code: spec/serpapi/client/example/example_search_yandex_spec.rb see: https://serpapi.com/yandex-search-api
require'serpapi'# initialize the serp api clientclient=SerpApi::Client.new(engine: 'yelp',api_key: ENV['SERPAPI_KEY'])# run a search using serpapi serviceresults=client.search({find_desc: 'Coffee',find_loc: 'New York, NY, USA'})# print the output of the response in formatted JSONppresults[:organic_results]# doc: https://serpapi.com/yelp-search-api- source code: spec/serpapi/client/example/example_search_yelp_spec.rb see: https://serpapi.com/yelp-search-api
| Metric | Ruby 2.7.8 | Ruby 3.4.4 | Ruby 4.0.0 | Improvement (3.4.4 vs 2.7.8) | Improvement (4.0.0 vs 3.4.4) |
|---|---|---|---|---|---|
| SerpApi Non-Persistent | 100.93 req/s | 114.97 req/s | 120.09 req/s | +13.9% | +4.5% |
| SerpApi Persistent | 226.82 req/s | 255.07 req/s | 296.05 req/s | +12.4% | +16.1% |
| HTTP.rb Non-Persistent | 270.62 req/s | 294.01 req/s | 319.81 req/s | +8.6% | +8.8% |
| HTTP.rb Persistent | 347.04 req/s | 570.95 req/s | 456.93 req/s | +64.5% | -20.0% |
- Upgrade to Ruby 3.4.4: Clear performance benefits across all scenarios
- Use Persistent Connections: 2x+ performance improvement in most cases
- HTTP.rb Performance: Particularly benefits from Ruby 3.4.4 with persistent connections
- SerpApi Optimization: Shows consistent ~2.2x improvement with persistent connections regardless of Ruby version
- Ruby 4.0.0 Performance: Shows mixed results with some regressions compared to 3.4.4, particularly for HTTP.rb persistent connections. Ruby 4.0.0 was just released for Christmas 2025, and HTTP.rb has not been optimized for it yet.
The older library (google-search-results-ruby) was performing at 55 req/s on Ruby 2.7.8, which is 2x slower than the current version (serpapi-ruby) on Ruby 3.4.4 or 4.0.0.
Context This benchmark was performed on warmup search results using a MacBook Pro 2025 connected via Wi-Fi 6.0 home network on AT&T fiber from Austin, TX (no network optimization).
If you were already using google-search-results-ruby gem, here are the changes.
# load library
# old way require 'google_search_results'
# new way
require 'serpapi'
# define a search
# old way to describe the search
search = GoogleSearch.new(search_params)
# new way default_parameter = {api_key: "secret_key", engine: "google"}
client = SerpApi::Client.new(default_parameter)
# an instance of the serpapi client is created
# where the default parameters are stored in the client.
# like api_key, engine
# then each subsequent API call can be made with additional parameters.
# override an existing parameter
# old way
search.params[:location] = "Portland,Oregon,United States"
# new way
# just provided the search call with the parameters.
results = client.search({location: "Portland,Oregon,United States", q: "Coffee"})
# search format return as raw html
# old way
html_results = search.get_html
# new way
raw_html = client.html(params)
# where params is Hash containing additional key / value
# search format returns a Hash
# old way
hash_results = search.get_hash
# new way
results = client.search(params)
# where params is the search parameters (override the default search parameters in the constructor). # search as raw JSON format
# old way
json_results = search.get_json
# new way
results = client.search(params)
# The prefix get_ is removed from all other methods.
# Because it's evident that a method returns something.
# old -> new way
search.get_search_archive -> client.search_archive
search.get_account -> client.account
search.get_location -> client.location
Most notable improvements:
- Removing parameters check on the client side. (most of the bugs)
- Reduce logic complexity in our implementation. (faster performance)
- Better documentation.
Ruby 2.7 and higher is supported.
Contributions are welcome. Make sure to read our contributing guide.