Session tokens are essential for optimising Google Maps API costs, and the good news is: the places-autocomplete-svelte package handles them automatically. By grouping autocomplete requests and place details into a single session, you can save 50-70% on API costs—without writing any session management code. This page explains how it works behind the scenes.
You don't need to manage session tokens manually! The PlaceAutocomplete component automatically:
Just use the component normally—session token optimisation happens automatically in the background, reducing your API costs without any extra code.
The Google Places API charges separately for autocomplete and place details requests:
A session groups all autocomplete requests leading to a single place selection. When the user selects a place, Google's API recognises the shared session token and bills the entire interaction as a single Place Details request. For example:
10 autocomplete requests + 1 details request = billed as 1 Place Details call only
Behind the scenes: The component automatically
creates a new AutocompleteSessionToken() and includes
it in the internal request object. After each place selection, it generates a fresh token
for the next search. This all happens without any code from you.
The PlaceAutocomplete component automatically manages session tokens through this lifecycle:
new AutocompleteSessionToken() from
Google's Places API. This happens in the background—no code needed from you.request.sessionToken, which
is passed automatically to fetchAutocompleteSuggestions().place.fetchFields() call. Google's API
recognises this and groups all the requests under one billing session.reset() function calls setSessionToken() to generate a fresh token for the next search. This ensures
each user interaction is properly isolated.Inside the component, this is handled in a few key places:
onMount(): Calls setSessionToken() to initialisesetSessionToken(): Creates new AutocompleteSessionToken()makeAcRequest(): Passes request.sessionToken to APIreset(): Calls setSessionToken() after selection