Web & SaaS Engineering 10 min read

Google Places API Tutorial: Address Autocomplete, Place Search and Details (with Code)

Learn the Google Places API (New): set up a key, add address autocomplete to checkout forms, search places by text or nearby, fetch place details with field masks, and keep your bill under control.

Umer Hasan

Umer Hasan

Co-Founder & CTO at Zapprex

Google Places API Tutorial: Address Autocomplete, Place Search and Details (with Code)
Table of contents

What you can do with the Places API

  • Autocomplete: suggest addresses and places as users type — great for checkout and sign-up forms.
  • Text Search: find places from a query like “dentist in Vijay Nagar Indore”.
  • Nearby Search: list places of certain types around a location.
  • Place Details: get address components, coordinates, opening hours, phone and ratings for a place ID.
  • Place Photos: show photos for a place, with required attributions.
At a glance: What you can do with the Places API — Autocomplete, Text Search, Nearby Search, Place Details
At a glance: What you can do with the Places API

Set up your project and key

Set up your project and key: 5 steps
  1. 1

    Create a Google Cloud project: and enable billing (Google offers free monthly usage for many SKUs).

  2. 2

    Enable the API: turn on Places API (New) in the API library.

  3. 3

    Create an API key: in APIs & Services → Credentials.

  4. 4

    Restrict the key: limit it to your website domains (HTTP referrers) or app package, and to the Places API only.

  5. 5

    Set budget alerts: create a billing budget so unexpected usage is caught early.

Tip: Use separate keys for the browser and the server. The browser key is visible to users, so it must be restricted to your domains.

Text Search with a field mask

The newer Places API asks you to list exactly which fields you need using a field mask. Fewer fields mean faster responses and, often, a cheaper SKU.

Server-side Text Search (Places API New)javascript
const res = await fetch("https://places.googleapis.com/v1/places:searchText", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "X-Goog-Api-Key": process.env.GOOGLE_MAPS_KEY,
    "X-Goog-FieldMask": "places.id,places.displayName,places.formattedAddress,places.location",
  },
  body: JSON.stringify({ textQuery: "dental clinic in Ujjain", languageCode: "en" }),
})
const { places } = await res.json()

Address autocomplete for checkout

  1. 1

    Add the widget: use Google’s Place Autocomplete web component or JavaScript library, or call the Autocomplete endpoint from your own UI.

  2. 2

    Bias to your area: restrict results to India (or your delivery countries) and bias towards your city.

  3. 3

    Use session tokens: group the typing requests and the final details request into one session to reduce costs.

  4. 4

    Fetch details: when the user selects a suggestion, request only the address components and location you need.

  5. 5

    Fill the form: map components to street, city, state and pin code; let the user edit house and floor details.

Keep costs and compliance in check

“Field masks and session tokens are the two habits that keep a Places API bill small.”

— Umer Hasan, CTO
  • Request only needed fields: richer fields such as ratings and opening hours can cost more.
  • Debounce typing: wait a moment after each keystroke before requesting suggestions.
  • Cache place IDs: place IDs can be stored; check the terms for what other data may be cached.
  • Show attributions: display Google’s attribution where required when showing Places data.

Topics

Google Places APIAddress AutocompleteGoogle Maps PlatformLocation API
Umer Hasan
Written by

Umer Hasan

Co-Founder & CTO at Zapprex

Leads engineering, cloud infrastructure and AI automation at Zapprex Technologies, with 7+ years of full-stack and mobile experience.

FAQ

Questions readers ask

It is a paid API with free monthly usage thresholds for many SKUs. Costs depend on the endpoint and fields requested, so check the current pricing and set budget alerts.

Have a project in mind?

Let's build something that moves your business forward.

Share your idea, process or problem — we will come back with a clear plan, timeline and cost range. No technical spec needed.

  • Founder reply within 1 business day
  • NDA on request · 100% code ownership
  • Talk directly to the engineers