Examples
Google Maps Geocoding
A storefront endpoint that geocodes addresses with the Google Maps API
A worker that exposes a storefront endpoint for address geocoding, keeping your Google Maps API key server-side.
Examples are deliberately generic — they’re here to show the moving parts, not to be pasted in verbatim. Comments mark the spots you’ll need to adapt to your own storefront and data.
Setup
- A Google Maps Geocoding API key, stored as a secret named
GOOGLE_MAPS_API_KEYand enabled on this worker. - The worker is reachable at
https://<your-shop>/apps/bound/<handle>— your storefront JavaScript calls it withfetch().
The worker
// Storefront endpoint: GET /apps/bound/<handle>?address=<address>
// Remember: you write the body of the fetch handler — `request` is the
// incoming request, capabilities live on `this.ctx.props`.
const url = new URL(request.url);
const address = url.searchParams.get("address");
if (!address) {
return Response.json({ error: "Missing ?address parameter" }, { status: 400 });
}
// The API key is a secret enabled on this worker — it never reaches the browser.
const apiKey = this.ctx.props.secrets.GOOGLE_MAPS_API_KEY;
const geocodeUrl = new URL("https://maps.googleapis.com/maps/api/geocode/json");
geocodeUrl.searchParams.set("address", address);
geocodeUrl.searchParams.set("key", apiKey);
const upstream = await fetch(geocodeUrl);
const result = await upstream.json();
// Return only what your storefront needs rather than proxying Google's
// response verbatim — the full response can be large, and it can embed
// details you'd rather not hand to the browser.
const first = result.results?.[0];
if (!first) {
return Response.json({ found: false }, { status: 404 });
}
return Response.json({
found: true,
lat: first.geometry.location.lat,
lng: first.geometry.location.lng,
formatted: first.formatted_address,
}); Notes
- This endpoint is public on your storefront like any storefront URL. Geocoding calls cost money per lookup, so consider light abuse protection — for example rejecting requests without your shop’s referer, or caching results for repeated addresses.
- Any HTTP method works through the app proxy; a
POSTwith a JSON body suits richer queries.