Python client library for Google Maps Platform providing access to mapping services, geocoding, routing, and location APIs
—
Configure the Google Maps client for authentication, rate limiting, timeout handling, and request customization to optimize performance and reliability for your application.
Create and configure a Google Maps API client with authentication credentials and operational parameters.
class Client:
def __init__(self, key=None, client_id=None, client_secret=None,
timeout=None, connect_timeout=None, read_timeout=None,
retry_timeout=60, requests_kwargs=None,
queries_per_second=60, queries_per_minute=6000, channel=None,
retry_over_query_limit=True, experience_id=None,
requests_session=None,
base_url="https://maps.googleapis.com"):
"""
Initialize Google Maps API client.
Args:
key (str): Maps API key. Required unless using enterprise credentials.
Most users should use an API key.
client_id (str): Enterprise client ID for Maps API for Work customers.
Most users should use an API key instead.
client_secret (str): Enterprise client secret (base64 encoded).
Most users should use an API key instead.
timeout (int): Combined connect and read timeout for HTTP requests,
in seconds. Specify None for no timeout.
connect_timeout (int): Connection timeout for HTTP requests, in
seconds. Requires requests >= 2.4.0.
read_timeout (int): Read timeout for HTTP requests, in seconds.
Requires requests >= 2.4.0.
retry_timeout (int): Timeout across multiple retriable requests,
in seconds. Default: 60.
queries_per_second (int): Number of queries per second permitted.
Default: 60. Rate limiting will sleep when exceeded.
queries_per_minute (int): Number of queries per minute permitted.
Default: 6000. Rate limiting will sleep when exceeded.
channel (str): Channel parameter for Maps API for Work customers.
Used for tracking purposes. ASCII alphanumeric only.
retry_over_query_limit (bool): Whether to retry requests that exceed
query rate limits. Default: True.
experience_id (str): Value for X-Goog-Maps-Experience-ID header.
requests_kwargs (dict): Extra keyword arguments for requests library,
including proxy authentication settings.
requests_session (requests.Session): Reused persistent session.
base_url (str): Base URL for all requests. Default: Google Maps API server.
Should not have trailing slash.
Raises:
ValueError: When credentials are missing, incomplete, or invalid.
NotImplementedError: If connect_timeout and read_timeout are used
with requests < 2.4.0.
"""Set, get, and clear the X-Goog-Maps-Experience-ID header for request tracking and debugging.
def set_experience_id(self, *experience_id_args):
"""
Set the experience ID header value.
Args:
*experience_id_args: Variable arguments to construct experience ID
"""
def get_experience_id(self):
"""
Get the current experience ID header value.
Returns:
str: Current experience ID value or None
"""
def clear_experience_id(self):
"""Clear the experience ID header value."""import googlemaps
# Initialize with API key (most common)
gmaps = googlemaps.Client(key='YOUR_API_KEY')import googlemaps
# Initialize with enterprise credentials
gmaps = googlemaps.Client(
client_id='your_client_id',
client_secret='your_base64_encoded_secret',
channel='your_channel'
)import googlemaps
# Configure custom rate limits
gmaps = googlemaps.Client(
key='YOUR_API_KEY',
queries_per_second=10, # Limit to 10 QPS
queries_per_minute=500, # Limit to 500 QPM
retry_over_query_limit=True # Retry when limits exceeded
)import googlemaps
# Configure timeouts and retries
gmaps = googlemaps.Client(
key='YOUR_API_KEY',
timeout=30, # 30 second total timeout
retry_timeout=120, # 2 minute retry timeout
retry_over_query_limit=False # Don't retry rate limit errors
)import googlemaps
import requests
# Custom session with proxy settings
session = requests.Session()
session.proxies = {'https': 'http://proxy.example.com:8080'}
gmaps = googlemaps.Client(
key='YOUR_API_KEY',
requests_session=session,
requests_kwargs={
'headers': {'Custom-Header': 'value'},
'auth': ('username', 'password')
}
)import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Set experience ID for request tracking
gmaps.set_experience_id('my-app-v1.0', 'user-session-123')
# Make API calls (all will include the experience ID header)
result = gmaps.geocode('1600 Amphitheatre Parkway')
# Check current experience ID
current_id = gmaps.get_experience_id()
# Clear experience ID
gmaps.clear_experience_id()Install with Tessl CLI
npx tessl i tessl/pypi-googlemaps