# Merge contentful with openweather for location pages

> Contentful page body and OpenWeather current conditions in parallel. For "merge contentful with openweather for location pages", store CONTENTFUL_ACCESS_TOKEN, OPENWEATHER_API_KEY in Secret Vault, call https://cdn.contentful.com/spaces/{{query.spaceId}}/entries from a Worker, and let Static HTML / Jamstack hit only https://realcomposer.com/v1/workspace/merge-contentful-with-openweather-pages with an rc_live_ client token.

- Canonical page: https://realcomposer.com/recipes/merge-contentful-with-openweather-pages
- Client platform: Static HTML / Jamstack
- Upstream services: Contentful, OpenWeather
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/merge-contentful-with-openweather-pages.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=merge-contentful-with-openweather-pages&via=agent

## Problem: Waterfall latency from multiple Contentful and OpenWeather requests

Static HTML / Jamstack widgets often await Contentful and OpenWeather sequentially, multiplying TLS handshakes on cellular networks and amplifying failure rates.

- Race conditions between parallel manual fetches
- Higher mobile data usage from redundant JSON
- Timeouts when any upstream exceeds cellular RTT

## Solution steps

1. **Store upstream credentials in Secret Vault**: Create CONTENTFUL_ACCESS_TOKEN, OPENWEATHER_API_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Set headers, mapping, and cache TTL**: Use GET against https://cdn.contentful.com/spaces/{{query.spaceId}}/entries. Allowed origins on the client token should match leave cache ttl at 0 when each response is user-specific (include userid or record id in query params before enabling cache).

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

```
fetch('https://realcomposer.com/v1/workspace/merge-contentful-with-openweather-pages', {
  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": "Merge contentful with openweather for location pages",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "contentful",
      "upstreamUrlTemplate": "https://cdn.contentful.com/spaces/{{query.spaceId}}/entries",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.CONTENTFUL_ACCESS_TOKEN}}"
      },
      "timeoutMs": 3000
    },
    {
      "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": "contentful.entryId",
        "sourcePath": "items.0.sys.id",
        "type": "string",
        "sourceStep": "contentful"
      },
      {
        "targetPath": "contentful.title",
        "sourcePath": "items.0.fields.title",
        "type": "string",
        "sourceStep": "contentful"
      },
      {
        "targetPath": "weather.temp",
        "sourcePath": "main.temp",
        "type": "number",
        "sourceStep": "weather"
      },
      {
        "targetPath": "weather.city",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "weather"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/merge-contentful-with-openweather-pages" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_merge-co",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "merge-contentful-with-openweather-pages"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "contentful_entryId": "sample",
  "contentful_title": "sample",
  "weather_temp": "sample",
  "weather_city": "sample"
}
```

## FAQ

### What should Static HTML / Jamstack store instead of Contentful secrets?

Log request IDs for merge-contentful-with-openweather-pages, not raw Contentful payloads, because upstream bodies may include PII.

### How do I forward JSON bodies to POST upstreams?

Cache key for merge-contentful-with-openweather-pages: internal edge-cache path plus sorted query. Identical params hit; vary userId per user.

### Can I restrict which domains call this pipeline?

Allowed origins on the merge-contentful-with-openweather-pages client token restrict which domains may call the edge URL.
