Session Token Cost Optimisation

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.

✓ Automatic Session Token Management

You don't need to manage session tokens manually! The PlaceAutocomplete component automatically:

  • Creates a new session token when the component mounts
  • Includes it in all autocomplete API requests
  • Uses the same token for the place details request when a user selects a place
  • Generates a fresh token after each place selection

Just use the component normally—session token optimisation happens automatically in the background, reducing your API costs without any extra code.

Understanding API Costs

The Google Places API charges separately for autocomplete and place details requests:

Without Session Tokens

  • Each autocomplete request: charged
  • Place details request: charged
  • Total cost: Autocomplete + Details

With Session Tokens

  • Autocomplete requests: not charged separately
  • Session billed as: one Place Details charge
  • Potential savings: up to 50-70%

💡 How It Works (Automatically)

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.

How It Works

The PlaceAutocomplete component automatically manages session tokens through this lifecycle:

  1. Component Initialisation: When the component mounts, it automatically creates a session token using new AutocompleteSessionToken() from Google's Places API. This happens in the background—no code needed from you.
  2. Autocomplete Requests: As the user types, the component includes the session token in all autocomplete API calls. The internal request object contains request.sessionToken, which is passed automatically to fetchAutocompleteSuggestions().
  3. Same Token Throughout: The component maintains the same session token for all autocomplete requests leading to a single place selection. This groups all related API calls together for billing purposes.
  4. Place Details Request: When a user selects a place, the component uses the same session token for the place.fetchFields() call. Google's API recognises this and groups all the requests under one billing session.
  5. Automatic Reset: After the place details are retrieved, the component's reset() function calls setSessionToken() to generate a fresh token for the next search. This ensures each user interaction is properly isolated.
  6. Session Timeout: If a user abandons a search within 3 minutes (Google's limit), the session expires and autocomplete calls are billed normally. The component handles this automatically—you don't need to worry about timeouts.

📝 Implementation Details

Inside the component, this is handled in a few key places:

  • onMount(): Calls setSessionToken() to initialise
  • setSessionToken(): Creates new AutocompleteSessionToken()
  • makeAcRequest(): Passes request.sessionToken to API
  • reset(): Calls setSessionToken() after selection

✓ What This Means for You

  • Zero configuration required: Session tokens work out of the box with no setup.
  • Automatic cost optimisation: Get 50-70% savings without any extra code.
  • Proper session lifecycle: Tokens are created, used, and refreshed automatically.
  • No token management code: The component handles all the complex session logic internally.
  • Focus on your app: Spend time on features, not API billing optimisation.
Previous
Styling