# How to Cache Openweathermap API Responses at Edge

> To how to cache openweathermap api responses at edge, set cacheTtlSeconds on a GET pipeline targeting https://api.openweathermap.org/data/2.5/weather?q={{query.city}}&appid={{secrets.OPENWEATHER_API_KEY}}. Identical query strings reuse Cloudflare caches.default before upstream sees traffic.

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

## Problem: Uncached OpenWeather spikes cost and latency

Every Frontend / Web visitor can trigger a fresh OpenWeather 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**: Store OPENWEATHER_API_KEY in the vault. Frontend / Web never embeds the raw secret, only a revocable rc_live_ client token.

2. **Target the real upstream URL**: Configure headersTemplate to inject vault secrets, target https://api.openweathermap.org/data/2.5/weather?q={{query.city}}&appid={{secrets.OPENWEATHER_API_KEY}}, and Add output mapping fields that mirror the JSON contract your screen already expects.

3. **Call from Frontend / Web**: Call https://realcomposer.com/v1/workspace/cache-weather-api-edge with a restricted rc_live_ client token. Frontend / Web never sends upstream secrets.

```
const res = await fetch('https://realcomposer.com/v1/workspace/cache-weather-api-edge', {
  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": "How to Cache Openweathermap API Responses at Edge",
  "method": "GET",
  "cacheTtlSeconds": 600,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "weather",
      "upstreamUrlTemplate": "https://api.openweathermap.org/data/2.5/weather?q={{query.city}}&appid={{secrets.OPENWEATHER_API_KEY}}",
      "httpMethod": "GET",
      "headersTemplate": {},
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "temp",
        "sourcePath": "main.temp",
        "type": "number",
        "sourceStep": "weather"
      },
      {
        "targetPath": "city",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "weather"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/cache-weather-api-edge" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

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

Slimmed RealComposer response:

```json
{
  "temp": "sample",
  "city": "sample"
}
```

## FAQ

### Does Frontend / Web need CORS changes after adding RealComposer?

Use scoped tokens where the vendor allows them on cache-weather-api-edge. Admin keys stay {{secrets.*}} references only.

### When does edge caching apply to this recipe?

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

### What happens if an upstream step times out?

Rotate vault secrets for cache-weather-api-edge anytime; clients keep the same rc_live_ token.
