Outsource Laravel Development Partner - $3200/Month | Bacancy

Add Real Time Search to Your Laravel App in Minutes with SerpApi

Last updated on by

Add Real Time Search to Your Laravel App in Minutes with SerpApi image

Instantly access real-time search data from Google, Amazon, and more — directly inside your app with SerpApi: Web Search API. SerpApi helps developers scrape search engine results with a simple API.

Here is what you can do with SerpApi:

  • Build AI agents that retrieve fresh web answers.
  • Monitor trends, news, or product prices in real time.
  • Power your chatbot or research app with live search data.
  • Track keyword rankings and SERP changes across multiple search engines.
  • Combine search results from Google, Bing, DuckDuckGo, and others in a simple API call.
  • And much more

Using the Laravel HTTP client we've shared below, you can quickly search Google and get back structured JSON results for many search services offered by Google, Bing, Walmart, and more:

use App\Facades\SerpApi;
 
SerpApi::search('coffee', ['location' => 'Phoenix, AZ']);
 
// Search Bing instead
SerpApi::search('coffee', ['location' => 'Phoenix, AZ', 'engine' => 'bing']);
 
// Google News
SerpApi::search('Laravel', ['location' => 'Phoenix, AZ', 'engine' => 'google_news']);

Results return search metadata, local maps, local results, organic results, related searches, and everything you might see on the screen when performing a search. The documentation contains details parameter examples as well as how they map to the search page using a helpful graphic in the API Examples section.

Defining an HTTP Client in Laravel

Here's a simple service you can use in your Laravel app to start using SerpApi:

<?php
 
namespace App;
 
use Illuminate\Container\Attributes\Config;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
 
class SerpApi
{
public function __construct(
#[Config('services.serpapi.api_key')]
#[\SensitiveParameter]
private string $apiKey
) {}
 
public function search(string $query, array $options = []): array
{
$params = collect($options)->merge(['q' => $query])->filter()->toArray();
 
return $this->client()
->withQueryParameters($params)->asJson()
->get('/search')->json();
}
 
public function client(): PendingRequest
{
return Http::withQueryParameters([
'api_key' => $this->apiKey,
])->baseUrl('https://serpapi.com/');
}
}

Next, sign up for SerpApi and configure your api key in services.serpapi.api_key in the config/services.php file:

SERPAPI_API_KEY="<your key>"

Using the service and API key, you can resolve this service from the container and define it as a service in the container if you'd like. Using Laravel's configuration attribute, it will automatically use the defined service config API key:

$api = app(\App\SerpApi::class);
 
$api->search('coffee', ['location' => 'Phoenix, AZ']);

You can also define a Facade if you'd like to use the service in that way by creating SerpApi.php in an app/Facades directory:

<?php
 
namespace App\Facades;
 
use Illuminate\Support\Facades\Facade;
 
class SerpApi extends Facade
{
protected static function getFacadeAccessor()
{
return \App\SerpApi::class;
}
}

Start building with real-time search data today — it’s easier than you think.

Try the Web Search API Now

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and more

Visit CodeRabbit
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
A new beta of Laravel Wayfinder just dropped image

A new beta of Laravel Wayfinder just dropped

Read article
Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026 image

Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026

Read article
Laravel Altitude - Opinionated Claude Code agents and commands for TALL stack development image

Laravel Altitude - Opinionated Claude Code agents and commands for TALL stack development

Read article
JSON:API Resource in Laravel 12.45 image

JSON:API Resource in Laravel 12.45

Read article
Caching With MongoDB for Faster Laravel Apps image

Caching With MongoDB for Faster Laravel Apps

Read article
Laravel 12.44 Adds HTTP Client afterResponse() Callbacks image

Laravel 12.44 Adds HTTP Client afterResponse() Callbacks

Read article