SearchAPI Ruby Gem
A Ruby client for the SearchAPI.io API, providing access to Google Search, YouTube, Instagram, TikTok, and more.
Installation
Add to your Gemfile:
gem 'searchapi'Or install directly:
gem install searchapiConfiguration
require 'searchapi'
SearchAPI.api_key ENV['SEARCH_API_KEY']Available APIs
| API | Method | Description |
|---|---|---|
| Google Search | SearchAPI.google(query) |
Full Google search with all features |
| Google Light | SearchAPI.google_light(query) |
Fast, lightweight Google search |
| Google News | SearchAPI.news(query) |
Google News articles |
| Google News Portal | SearchAPI.news_portal(query) |
Google News Portal with topics |
| Google News Light | SearchAPI.news_light(query) |
Fast, lightweight news search |
| Google Images | SearchAPI.images(query) |
Google Image search |
| Google Local | SearchAPI.local(query) |
Local business search |
| Google Scholar | SearchAPI.scholar(query) |
Academic papers and citations |
| Google Finance | SearchAPI.finance(symbol) |
Stock quotes and financial data |
| Google Flights | SearchAPI.flights(options) |
Flight search and booking |
| YouTube | SearchAPI.youtube(query) |
YouTube video search |
SearchAPI.instagram(username) |
Instagram profile data | |
| TikTok | SearchAPI.tiktok(username) |
TikTok profile data |
Quick Examples
Google Search
response = SearchAPI.google('ruby programming')
response.result.organic_results.each do |result|
puts "#{ result.title } - #{ result.link }"
endGoogle News
response = SearchAPI.news('technology', time_period: :last_week)
response.result.each do |article|
puts "#{ article.title } (#{ article.source })"
endGoogle Images
response = SearchAPI.images('sunset', size: :large, color: :orange)
response.result.each do |image|
puts "#{ image.title } - #{ image.original.link }"
endGoogle Finance
response = SearchAPI.finance('AAPL:NASDAQ')
puts "#{ response.result.summary.title }: $#{ response.result.summary.price }"Google Flights
options = SearchAPI::GoogleFlightsOptions.build do
departure_id 'JFK'
arrival_id 'LAX'
outbound_date '2026-06-15'
flight_type :one_way
end
response = SearchAPI.flights(options)
puts "Cheapest: $#{ response.result.cheapest&.price }"YouTube Search
response = SearchAPI.youtube('ruby tutorial')
response.result.videos.each do |video|
puts "#{ video.title } by #{ video.channel.title }"
endSocial Media Profiles
# Instagram
response = SearchAPI.instagram('instagram')
puts "#{ response.result.profile.username }: #{ response.result.followers } followers"
# TikTok
response = SearchAPI.tiktok('tiktok')
puts "#{ response.result.profile.name }: #{ response.result.hearts } hearts"Using Options
Each API supports options via a block or hash:
# Block syntax
options = SearchAPI::GoogleSearchOptions.build do
device :mobile
gl 'us'
hl 'en'
time_period :last_week
end
response = SearchAPI.google('news', options)
# Hash syntax
response = SearchAPI.google('news', { device: :mobile, gl: 'us' })Options are case-insensitive and normalized automatically:
# These are equivalent
SearchAPI::GoogleFinanceOptions.build { window :'1d' } # => '1D'
SearchAPI::GoogleFinanceOptions.build { window :'1D' } # => '1D'
SearchAPI::GoogleFinanceOptions.build { window :MAX } # => 'MAX'Response Structure
All responses return a Faraday response with a result accessor:
response = SearchAPI.google('test')
response.success? # HTTP success
response.result.success? # API success (no error field)
response.result.search_metadata
response.result.search_parameters
response.result.organic_resultsResults are enumerable where it makes sense:
response = SearchAPI.google('ruby')
response.result.each { |r| puts r.title } # Iterates organic_results
response.result.first # First organic result
response.result.count # Number of organic resultsDocumentation
See the /readme directory for detailed documentation on each API endpoint.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/EndlessInternational/searchapi.
License
The gem is available as open source under the terms of the MIT License.