API Reference

Look up the arguments, return values, and exceptions for the functions and classes in serpapi. For examples of how to use them together, see Client Usage.

exception serpapi.APIKeyNotProvided

API key is not provided.

exception serpapi.HTTPConnectionError(original_exception)

Connection Error.

exception serpapi.HTTPError(original_exception)

HTTP Error.

exception serpapi.SearchIDNotProvided

Search ID is not provided.

exception serpapi.SerpApiError

Base class for exceptions in this module.

exception serpapi.TimeoutError(*args: Any, **kwargs: Any)

Timeout Error.

class serpapi.AsyncClient(*, api_key=None, timeout=None)

An asynchronous version of Client for use with await.

It has the same methods, parameters, and request options as Client, and each method is a coroutine. AsyncClient needs Python 3.11 or later and the async extra: pip install "serpapi[async]". It sends requests with httpx2 instead of requests.

async with serpapi.AsyncClient(api_key=os.environ["SERPAPI_KEY"]) as client:
    results = await client.search(engine="google", q="coffee")

Use the client in an async with block, or call await client.aclose() when you are done with it, to close its connections. Use each client in a single event loop. You can send several requests at once with the same client.

Parameters:
  • api_key – The API Key to use for SerpApi.com.

  • timeout – The default timeout to use for requests.

async account(params: dict = None, **kwargs)

Get SerpApi account information. Use it with await.

Parameters:
  • params – Account API parameters.

  • kwargs – Additional account parameters or request options. timeout, proxies, verify, and cert are applied to the underlying HTTP request. stream is accepted but ignored.

Learn more: https://serpapi.com/account-api

async extract(params: dict = None, **kwargs)

Extract content from a public URL using the Extract API. Use it with await.

Accepts the same parameters as Client.extract. Sets engine=extract and returns a AsyncSerpResults object for JSON, or text for HTML and Markdown.

Parameters:
  • params – Optional mapping of Extract API parameters, including the required url. Put timeout here to set the API fetch timeout in milliseconds.

  • kwargs – Additional API parameters or HTTP request options, as in search(). The timeout keyword controls the HTTP client timeout in seconds.

async locations(params: dict = None, **kwargs)

Get a list of supported Google locations. Use it with await.

Parameters:
  • params – Location API parameters such as q and limit.

  • kwargs – Additional location parameters or request options. timeout, proxies, verify, and cert are applied to the underlying HTTP request. stream is accepted but ignored.

Learn more: https://serpapi.com/locations-api

async search(params: dict = None, **kwargs)

Fetch a page of results from SerpApi. Use it with await.

Returns a serpapi.AsyncSerpResults object for JSON responses, or text when output="html" or output="md" is requested. Accepts the same parameters as Client.search.

Parameters:
  • params – Optional mapping of SerpApi search parameters such as engine, q, location, and output.

  • kwargs – Additional SerpApi parameters or request options. timeout, proxies, verify, and cert are applied to the underlying HTTP request. stream is accepted but ignored.

Learn more: https://serpapi.com/search-api

async search_archive(params: dict = None, **kwargs)

Get a result from the SerpApi Search Archive API. Use it with await.

Parameters:
  • params – Archive parameters. Must include search_id. output accepts json (default), html, or md.

  • kwargs – Additional archive parameters or request options. timeout, proxies, verify, and cert are applied to the underlying HTTP request. stream is accepted but ignored.

Learn more: https://serpapi.com/search-archive-api

async upload_image(image, **kwargs)

Upload an image to SerpApi’s Image API. Use it with await.

image can be a filesystem path or an open binary file object. The returned dictionary contains an image_id that can be passed to search() for engines that accept uploaded images, such as Google Lens.

Parameters:
  • image – a path or open binary file object containing a JPG/JPEG, PNG, or WebP image no larger than 500 KB.

  • api_key – the API Key to use for SerpApi.com.

  • ** – any additional multipart form fields to pass to the API.

Learn more: https://serpapi.com/image-api

class serpapi.AsyncSerpResults(data, *, client)

A dictionary-like object that represents the results of a AsyncClient request.

It works like SerpResults, but next_page() is a coroutine and yield_pages() is an asynchronous generator.

async next_page()

Return the next page of results, if any. Use it with await.

async yield_pages(max_pages=1000)

An asynchronous generator that yield s the next n pages of search results, if any.

Use it with async for.

Parameters:

max_pages – limit the number of pages yielded to n.

class serpapi.Client(*, api_key=None, timeout=None)

A class that handles API requests to SerpApi in a user-friendly manner.

Store your API key in an environment variable, then create a client with serpapi.Client(api_key=os.environ["SERPAPI_KEY"]).

Parameters:
  • api_key – The API Key to use for SerpApi.com.

  • timeout – The default timeout to use for requests.

account(params: dict = None, **kwargs)

Get SerpApi account information.

Parameters:
  • params – Account API parameters.

  • kwargs – Additional account parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/account-api

extract(params: dict = None, **kwargs)

Extract content from a public URL using the Extract API.

Accepts a parameter dictionary or keyword arguments, like search(). Sets engine=extract and returns a SerpResults object for JSON, or text for HTML and Markdown.

results = client.extract(url="https://example.com/")
markdown = client.extract(url="https://example.com/", output="md")
results = client.extract({"url": "https://example.com/", "timeout": 30000}, timeout=60)
Parameters:
  • params – Optional mapping of Extract API parameters, including the required url. Put timeout here to set the API fetch timeout in milliseconds.

  • kwargs – Additional API parameters or HTTP request options, as in search(). The timeout keyword controls the HTTP client timeout in seconds.

mode accepts scrape (default), map, or crawl. output accepts json (default), html, or md. JSON includes page content in the top-level markdown field by default, or in each pages entry for crawls. Set include_markdown=False to omit it. For a scraped page, output="md" returns the page Markdown without search metadata.

Crawl mode can consume one search credit per successfully extracted page. Use max_pages to limit the number of pages.

locations(params: dict = None, **kwargs)

Get a list of supported Google locations.

Parameters:
  • params – Location API parameters such as q and limit.

  • kwargs – Additional location parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/locations-api

search(params: dict = None, **kwargs)

Fetch a page of results from SerpApi.

Returns a serpapi.SerpResults object for JSON responses, or text when output="html" or output="md" is requested. Prefer passing SerpApi engine parameters as keyword arguments. A parameter dictionary is also accepted when your code already has parameters in a mapping.

Parameters:
  • params – Optional mapping of SerpApi search parameters such as engine, q, location, and output.

  • kwargs – Additional SerpApi parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/search-api

search_archive(params: dict = None, **kwargs)

Get a result from the SerpApi Search Archive API.

Parameters:
  • params – Archive parameters. Must include search_id. output accepts json (default), html, or md.

  • kwargs – Additional archive parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/search-archive-api

upload_image(image, **kwargs)

Upload an image to SerpApi’s Image API.

image can be a filesystem path or an open binary file object. The returned dictionary contains an image_id that can be passed to search() for engines that accept uploaded images, such as Google Lens.

Parameters:
  • image – a path or open binary file object containing a JPG/JPEG, PNG, or WebP image no larger than 500 KB.

  • api_key – the API Key to use for SerpApi.com.

  • ** – any additional multipart form fields to pass to the API.

Learn more: https://serpapi.com/image-api

class serpapi.SerpResults(data, *, client)

A dictionary-like object that represents the results of a SerpApi request.

An instance of this class is returned if the response is a valid JSON object. It can be used like a dictionary, but also has some additional methods.

next_page()

Return the next page of results, if any.

yield_pages(max_pages=1000)

A generator that yield s the next n pages of search results, if any.

Parameters:

max_pages – limit the number of pages yielded to n.

serpapi.account(params: dict = None, **kwargs)

Get SerpApi account information.

Parameters:
  • params – Account API parameters.

  • kwargs – Additional account parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/account-api

serpapi.extract(params: dict = None, **kwargs)

Extract content from a public URL using the Extract API.

Accepts a parameter dictionary or keyword arguments, like search(). Sets engine=extract and returns a SerpResults object for JSON, or text for HTML and Markdown.

results = client.extract(url="https://example.com/")
markdown = client.extract(url="https://example.com/", output="md")
results = client.extract({"url": "https://example.com/", "timeout": 30000}, timeout=60)
Parameters:
  • params – Optional mapping of Extract API parameters, including the required url. Put timeout here to set the API fetch timeout in milliseconds.

  • kwargs – Additional API parameters or HTTP request options, as in search(). The timeout keyword controls the HTTP client timeout in seconds.

mode accepts scrape (default), map, or crawl. output accepts json (default), html, or md. JSON includes page content in the top-level markdown field by default, or in each pages entry for crawls. Set include_markdown=False to omit it. For a scraped page, output="md" returns the page Markdown without search metadata.

Crawl mode can consume one search credit per successfully extracted page. Use max_pages to limit the number of pages.

serpapi.locations(params: dict = None, **kwargs)

Get a list of supported Google locations.

Parameters:
  • params – Location API parameters such as q and limit.

  • kwargs – Additional location parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/locations-api

serpapi.search(params: dict = None, **kwargs)

Fetch a page of results from SerpApi.

Returns a serpapi.SerpResults object for JSON responses, or text when output="html" or output="md" is requested. Prefer passing SerpApi engine parameters as keyword arguments. A parameter dictionary is also accepted when your code already has parameters in a mapping.

Parameters:
  • params – Optional mapping of SerpApi search parameters such as engine, q, location, and output.

  • kwargs – Additional SerpApi parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/search-api

serpapi.search_archive(params: dict = None, **kwargs)

Get a result from the SerpApi Search Archive API.

Parameters:
  • params – Archive parameters. Must include search_id. output accepts json (default), html, or md.

  • kwargs – Additional archive parameters or request options. timeout, proxies, verify, stream, and cert are passed to the underlying HTTP request.

Learn more: https://serpapi.com/search-archive-api

serpapi.upload_image(image, **kwargs)

Upload an image to SerpApi’s Image API.

image can be a filesystem path or an open binary file object. The returned dictionary contains an image_id that can be passed to search() for engines that accept uploaded images, such as Google Lens.

Parameters:
  • image – a path or open binary file object containing a JPG/JPEG, PNG, or WebP image no larger than 500 KB.

  • api_key – the API Key to use for SerpApi.com.

  • ** – any additional multipart form fields to pass to the API.

Learn more: https://serpapi.com/image-api

async AsyncClient.aclose()

Close the client’s HTTP connections. The client cannot send requests afterwards.

SerpResults.as_dict()

Returns the data as a standard Python dictionary.

This can be useful when passing results to libraries that expect a plain dict.

SerpResults.next_page_url

The URL of the next page of results, if any.

AsyncSerpResults.as_dict()

Returns the data as a standard Python dictionary.

This can be useful when passing results to libraries that expect a plain dict.

AsyncSerpResults.next_page_url

The URL of the next page of results, if any.