Client Usage
Create a serpapi.Client to reuse your API key, timeout, and HTTP connection settings across requests.
Create a Client
First, install the package and set SERPAPI_KEY. The examples on this page use the following client:
import os
import serpapi
client = serpapi.Client(
api_key=os.environ["SERPAPI_KEY"],
timeout=20,
)
timeout=20 sets the default connection and read timeout in seconds for this client’s requests. It applies to searches, archive lookups, account and location requests, and image uploads. See Errors and Timeouts for how the timeout works.
Search
Pass search parameters by name, using Python keyword arguments:
results = client.search(engine="google", q="coffee")
You can also pass a dictionary when parameters are already stored in one:
params = {
"engine": "google",
"q": "coffee",
}
results = client.search(params)
You can combine a dictionary with keyword arguments. The client adds the keyword arguments to the dictionary and replaces existing values with the same name:
params = {"engine": "google", "q": "coffee"}
results = client.search(params, location="Austin, Texas")
Module Helpers
You can call functions such as serpapi.search() directly without creating a client:
import serpapi
results = serpapi.search(
api_key="secret_api_key",
engine="google",
q="coffee",
)
These functions use a shared client. Create your own serpapi.Client when you need separate settings for an application, test, or worker that runs searches.
Search Archive
Use search_archive() to retrieve a saved search. Get its ID from the original response:
search_id = results["search_metadata"]["id"]
archived = client.search_archive(search_id=search_id)
If search_id is missing, the client raises serpapi.SearchIDNotProvided.
Account and Locations
account = client.account()
locations = client.locations(q="Austin", limit=3)
account() returns account information for your API key. locations() returns supported Google locations that match the query. See Account and Locations for examples and how to use a location in a search.
Request Options
search(), search_archive(), account(), and locations() pass these keyword arguments to the underlying requests call:
Option |
Use |
|---|---|
|
Set a different timeout, in seconds, for one request. |
|
Send the request through a proxy. |
|
Check the server’s TLS certificate, or use a custom certificate authority bundle. |
|
Set the |
|
Authenticate the request with a client certificate. |
For example, set a shorter timeout for this search:
results = client.search(
engine="google",
q="coffee",
timeout=10,
)
See Request Options for proxy, TLS, certificate, and per-request timeout examples.
Response Objects
For JSON searches, the client returns a serpapi.SerpResults object. Read its fields like a dictionary:
first = results["organic_results"][0]
print(first.get("title"))
print(first.get("link"))
Use as_dict() when another library needs a plain dictionary:
payload = results.as_dict()
Use output="html" to receive the original search results page as an HTML string:
html = client.search(engine="google", q="coffee", output="html")
print(html[:500])
Use output="md" to receive a Markdown string, for example to pass search results to an AI agent:
markdown = client.search(engine="google", q="coffee", output="md")
print(markdown[:500])
See Output Formats for details on JSON, Markdown, and HTML responses.
Pagination Helpers
For one additional page, call next_page():
next_results = results.next_page()
Use yield_pages() in a loop to read the current page and fetch more pages. This example reads up to ten pages and stops if a page contains an error message:
for page_number, page in enumerate(results.yield_pages(max_pages=10), start=1):
if "error" in page:
print(page["error"])
break
current = page.get("serpapi_pagination", {}).get("current", page_number)
print(current)
See Pagination for more examples and how to use Google’s start parameter.
Error Handling
Use try and except to handle a timeout, a connection failure, or an HTTP error:
import serpapi
try:
results = client.search(engine="google", q="coffee")
except serpapi.TimeoutError:
print("The request timed out.")
except serpapi.HTTPConnectionError:
print("Could not connect to SerpApi.")
except serpapi.HTTPError as exc:
print(exc.status_code, exc.error)
See Errors and Timeouts for error details and timeout settings.