Skip to main content

SerpApi Python Library & Package

Package serpapi-python

Integrate search data into your AI workflow, RAG / fine-tuning, or Python 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.

Installation

To install the serpapi package, simply run the following command:

$ pip install serpapi

Please note that this package is separate from the legacy serpapi module, which is available on PyPi as google-search-results. This package is maintained by SerpApi, and is the recommended way to access the SerpApi service from Python.

Quick start

Let's start by searching for Coffee on Google:

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("SERPAPI_KEY"))
results = client.search({
  "engine": "google",
  "q": "coffee"
})

print(results)

The results variable now contains a SerpResults object, which acts just like a standard dictionary, with some convenient functions added on top.

This example runs a search for "coffee" on Google. It then returns the results as a regular Python 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. Python accesses these variables from os.environ["SERPAPI_KEY"].

Extract content from a URL

The Extract API supports single-page scraping, URL discovery, and crawling. Call client.extract() with a parameter dictionary or keyword arguments. The client sets engine="extract" automatically.

Set output="md" to get the page's main content as a Markdown string:

import os
import serpapi

client = serpapi.Client(api_key=os.environ["SERPAPI_KEY"], timeout=60)
markdown = client.extract(url="https://serpapi.com/", output="md")
print(markdown)

JSON is the default output format. It returns a dictionary-like SerpResults object with page information, structured data, links, and Markdown:

results = client.extract(url="https://serpapi.com/")
print(results["page_information"]["title"])
print(results["markdown"])

Use output="html" to get the retrieved HTML as a string:

html = client.extract(url="https://serpapi.com/", output="html")

Use mode="map" to discover URLs or mode="crawl" to extract multiple pages:

urls = client.extract(url="https://serpapi.com/", mode="map", include_sitemap=False)
pages = client.extract(
    url="https://serpapi.com/", mode="crawl", max_pages=2, max_depth=1, timeout=180
)

Crawl mode can consume one search credit per successfully extracted page, including the starting page. Set max_pages to limit the number of pages.

See the Extract API guide for response examples and more options.

Error handling

Unsuccessful requests raise serpapi.HTTPError or serpapi.TimeoutError exceptions. The returned status code will reflect the sort of error that occurred, please refer to Status and Error Codes Documentation for more details.

import os
import serpapi

# A default timeout can be set here.
client = serpapi.Client(api_key=os.getenv("API_KEY"), timeout=10)

try:
    results = client.search({
        'engine': 'google',
        'q': 'coffee',
    })
except serpapi.HTTPError as e:
    if e.status_code == 401: # Invalid API key
        print(e.error) # "Invalid API key. Your API key should be here: https://serpapi.com/manage-api-key"
    elif e.status_code == 400: # Missing required parameter
        pass
    elif e.status_code == 429: # Exceeds the hourly throughput limit OR account run out of searches
        pass
except serpapi.TimeoutError as e:
    # Handle timeout
    print(f"The request timed out: {e}")

Documentation

Documentation is available on Read the Docs.

Change history is available on GitHub.

Markdown output for AI agents

For AI agents and LLM workflows, use output="md". It returns search results with clean headings, links, and tables while using roughly half the tokens of JSON on average.

markdown = client.search({
    "engine": "google",
    "q": "coffee",
    "output": "md",
})

Markdown works across SerpApi APIs and is returned as a plain Python string. See Markdown Output for AI Agents for details.

Raw HTML is also available with output="html" and is returned as a plain Python string.

Basic examples

Search Bing

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'bing',
    'q': 'coffee'
})

Search Baidu

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'baidu',
    'q': 'coffee',
})

Search Yahoo

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'yahoo',
    'p': 'coffee',
})

Search YouTube

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'youtube',
    'search_query': 'coffee',
})

Search Walmart

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'walmart',
    'query': 'coffee',
})

Search eBay

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'ebay',
    '_nkw': 'coffee',
})

Search Naver

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'naver',
    'query': 'coffee',
})

Search Home Depot

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'home_depot',
    'q': 'chair',
})

Search Apple App Store

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'apple_app_store',
    'term': 'coffee',
})

Search DuckDuckGo

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'duckduckgo',
    'q': 'coffee',
})

Search Google

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google',
    'q': 'coffee'
})

Search Google Scholar

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_scholar',
    'q': 'coffee',
})

Search Google Autocomplete

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_autocomplete',
    'q': 'coffee',
})

Search Google Immersive Product

Get a current page_token from Google Shopping before requesting product details:

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
shopping = client.search(
    engine="google_shopping",
    q="lg tv",
    gl="us",
    hl="en",
    no_cache=True,
)
page_token = next(
    product["immersive_product_page_token"]
    for product in shopping["shopping_results"]
    if product.get("immersive_product_page_token")
)
results = client.search(
    engine="google_immersive_product",
    page_token=page_token,
)

Search Google Reverse Image

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_reverse_image',
    'image_url': 'https://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png',
    'max_results': '1',
})

Search Google Lens by image URL or upload

Google Lens accepts either a publicly accessible image URL or an uploaded image. To search by URL, pass the URL directly:

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("SERPAPI_KEY"))
results = client.search({
    "engine": "google_lens",
    "url": "https://i.imgur.com/HBrB8p0.png",
})

To search a local image, place a file named image.png in your working directory, upload it, and pass its temporary image_id to Google Lens:

upload = client.upload_image("image.png")
results = client.search({
    "engine": "google_lens",
    "image_id": upload["image_id"],
})

Uploaded images can be JPG/JPEG, PNG, or WebP files up to 500 KB. The returned image_id expires after 10 minutes.

Search Google Local Services

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_local_services',
    'q': 'electrician',
    'data_cid': '6745062158417646970',
})

Search Google Maps

import os
import serpapi


client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_maps',
    'q': 'pizza',
    'll': '@40.7455096,-74.0083012,15.1z',
    'type': 'search',
})

Search Google Jobs

import os
import serpapi


client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_jobs',
    'q': 'software engineer',
})

Search Google Play

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_play',
    'q': 'kite',
    'store': 'apps',
})

Search Google Images

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_images',
    'tbm': 'isch',
    'q': 'coffee',
})

License

MIT License.

Contributing

See CONTRIBUTING.md for development setup, testing, and publishing instructions.

Metadata

Release files for serpapi 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for serpapi 1.2.0
File Size Uploaded Provenance Actions
serpapi-1.2.0.tar.gz 80.6 kB With provenance Details for serpapi-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for serpapi 1.2.0
File Interpreter ABI Platform Actions
serpapi-1.2.0-py3-none-any.whl Python 3 none any Details for serpapi-1.2.0-py3-none-any.whl

Total release size: 92.8 kB

Release files / serpapi-1.2.0.tar.gz

Download URL serpapi-1.2.0.tar.gz
Size 80.6 kB
Tags Source
SHA-256 checksum
How to use checksums
16e7d5dd1c7bfd6817e3c4b7b6a7c2dd6e6142f0dec534cf76fa5474a93fe4a9
BLAKE2b-256 checksum
How to use checksums
7043215842b351249fbecb7b65ea3e81e34e866a3c159d15deeddf1f5d882eac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Release files / serpapi-1.2.0-py3-none-any.whl

Download URL serpapi-1.2.0-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9aee65c7ddf4e15d936cc22f4fe70edd47c9ed66596b18e8ddeb88b203d06e7
BLAKE2b-256 checksum
How to use checksums
9e07de4cca8858313f6b0457f7ba1a22581ad4539aa24b2d385503e4093d0ee1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Security for serpapi 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for serpapi-1.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page