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
Clientfor use withawait.It has the same methods, parameters, and request options as
Client, and each method is a coroutine.AsyncClientneeds Python 3.11 or later and theasyncextra:pip install "serpapi[async]". It sends requests with httpx2 instead ofrequests.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 withblock, or callawait 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, andcertare applied to the underlying HTTP request.streamis 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. Setsengine=extractand returns aAsyncSerpResultsobject for JSON, or text for HTML and Markdown.- Parameters:
params – Optional mapping of Extract API parameters, including the required
url. Puttimeouthere to set the API fetch timeout in milliseconds.kwargs – Additional API parameters or HTTP request options, as in
search(). Thetimeoutkeyword 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
qandlimit.kwargs – Additional location parameters or request options.
timeout,proxies,verify, andcertare applied to the underlying HTTP request.streamis 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.AsyncSerpResultsobject for JSON responses, or text whenoutput="html"oroutput="md"is requested. Accepts the same parameters asClient.search.- Parameters:
params – Optional mapping of SerpApi search parameters such as
engine,q,location, andoutput.kwargs – Additional SerpApi parameters or request options.
timeout,proxies,verify, andcertare applied to the underlying HTTP request.streamis 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.outputacceptsjson(default),html, ormd.kwargs – Additional archive parameters or request options.
timeout,proxies,verify, andcertare applied to the underlying HTTP request.streamis 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.imagecan be a filesystem path or an open binary file object. The returned dictionary contains animage_idthat can be passed tosearch()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
AsyncClientrequest.It works like
SerpResults, butnext_page()is a coroutine andyield_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
yields the nextnpages 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, andcertare 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(). Setsengine=extractand returns aSerpResultsobject 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. Puttimeouthere to set the API fetch timeout in milliseconds.kwargs – Additional API parameters or HTTP request options, as in
search(). Thetimeoutkeyword controls the HTTP client timeout in seconds.
modeacceptsscrape(default),map, orcrawl.outputacceptsjson(default),html, ormd. JSON includes page content in the top-levelmarkdownfield by default, or in eachpagesentry for crawls. Setinclude_markdown=Falseto 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_pagesto limit the number of pages.
- locations(params: dict = None, **kwargs)
Get a list of supported Google locations.
- Parameters:
params – Location API parameters such as
qandlimit.kwargs – Additional location parameters or request options.
timeout,proxies,verify,stream, andcertare 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.SerpResultsobject for JSON responses, or text whenoutput="html"oroutput="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, andoutput.kwargs – Additional SerpApi parameters or request options.
timeout,proxies,verify,stream, andcertare 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.outputacceptsjson(default),html, ormd.kwargs – Additional archive parameters or request options.
timeout,proxies,verify,stream, andcertare 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.
imagecan be a filesystem path or an open binary file object. The returned dictionary contains animage_idthat can be passed tosearch()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
yields the nextnpages 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, andcertare 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(). Setsengine=extractand returns aSerpResultsobject 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. Puttimeouthere to set the API fetch timeout in milliseconds.kwargs – Additional API parameters or HTTP request options, as in
search(). Thetimeoutkeyword controls the HTTP client timeout in seconds.
modeacceptsscrape(default),map, orcrawl.outputacceptsjson(default),html, ormd. JSON includes page content in the top-levelmarkdownfield by default, or in eachpagesentry for crawls. Setinclude_markdown=Falseto 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_pagesto 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
qandlimit.kwargs – Additional location parameters or request options.
timeout,proxies,verify,stream, andcertare 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.SerpResultsobject for JSON responses, or text whenoutput="html"oroutput="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, andoutput.kwargs – Additional SerpApi parameters or request options.
timeout,proxies,verify,stream, andcertare 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.outputacceptsjson(default),html, ormd.kwargs – Additional archive parameters or request options.
timeout,proxies,verify,stream, andcertare 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.
imagecan be a filesystem path or an open binary file object. The returned dictionary contains animage_idthat can be passed tosearch()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.