# Cache Google Places Autocomplete API to Reduce Bills

> To cache google places autocomplete api to reduce bills, set cacheTtlSeconds on a GET pipeline targeting https://maps.googleapis.com/maps/api/place/autocomplete/json?input={{query.input}}&key={{secrets.GOOGLE_MAPS_API_KEY}}. Identical query strings reuse Cloudflare caches.default before upstream sees traffic.

- Canonical page: https://realcomposer.com/recipes/cache-google-places-api-reduce-bills
- Client platform: Frontend / Web
- Upstream services: Google Maps
- Category: Edge Caching & Rate Limits
- JSON version: https://realcomposer.com/recipes/cache-google-places-api-reduce-bills.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=cache-google-places-api-reduce-bills&via=agent

## Problem: Uncached Google Maps spikes cost and latency

Every Frontend / Web visitor can trigger a fresh Google Maps call. Bursts exhaust per-minute quotas even when data changes slowly.

- Thundering herds after cache expiry on the origin
- Analytics skewed by client-side retry storms
- Support tickets blaming "API downtime" during spikes

## Solution steps

1. **Bind vault keys to the Worker**: Create GOOGLE_MAPS_API_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Set headers, mapping, and cache TTL**: Point step maps at https://maps.googleapis.com/maps/api/place/autocomplete/json?input={{query.input}}&key={{secrets.GOOGLE_MAPS_API_KEY}} with GET. Add output mapping fields that mirror the JSON contract your screen already expects. Set cache TTL to 300s so repeat GETs with the same query string hit caches.default.

3. **Call from Frontend / Web**: Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.

```
const res = await fetch('https://realcomposer.com/v1/workspace/cache-google-places-api-reduce-bills', {
  headers: { Authorization: 'Bearer rc_live_your_token' }
});
```

## Pipeline config

Import this config into a RealComposer pipeline. `{{secrets.NAME}}` values are read from the encrypted Secret Vault at request time, so the client never sees them.

```json
{
  "name": "Cache Google Places Autocomplete API to Reduce Bills",
  "method": "GET",
  "cacheTtlSeconds": 300,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "maps",
      "upstreamUrlTemplate": "https://maps.googleapis.com/maps/api/place/autocomplete/json?input={{query.input}}&key={{secrets.GOOGLE_MAPS_API_KEY}}",
      "httpMethod": "GET",
      "headersTemplate": {},
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "description",
        "sourcePath": "predictions.0.description",
        "type": "string",
        "sourceStep": "maps"
      },
      {
        "targetPath": "placeId",
        "sourcePath": "predictions.0.place_id",
        "type": "string",
        "sourceStep": "maps"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/cache-google-places-api-reduce-bills" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_cache-go",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "cache-google-places-api-reduce-bills"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "description": "sample",
  "placeId": "sample"
}
```

## FAQ

### What should Frontend / Web store instead of Google Maps secrets?

Point Frontend / Web at /recipes/cache-google-places-api-reduce-bills edge URL. RealComposer answers OPTIONS with Access-Control headers for allowed origins.

### Will Anthropic or Linear prompt bodies be rewritten?

Cache key for cache-google-places-api-reduce-bills: internal edge-cache path plus sorted query. Identical params hit; vary userId per user.

### How do I rotate upstream credentials?

Rotate vault secrets for cache-google-places-api-reduce-bills anytime; clients keep the same rc_live_ token.
