# Aggregate contentful content with algolia search

> Algolia search results enriched with Contentful entry fields at edge. For "aggregate contentful content with algolia search", store CONTENTFUL_ACCESS_TOKEN, ALGOLIA_API_KEY in Secret Vault, call https://cdn.contentful.com/spaces/{{query.spaceId}}/entries from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/aggregate-contentful-with-algolia-search with an rc_live_ client token. POST upstream calls are not stored in caches.default; clients must send JSON bodies and headers forward unchanged.

- Canonical page: https://realcomposer.com/recipes/aggregate-contentful-with-algolia-search
- Client platform: Next.js / React
- Upstream services: Contentful, Algolia
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/aggregate-contentful-with-algolia-search.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=aggregate-contentful-with-algolia-search&via=agent

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

Next.js / React widgets often await Contentful and Algolia 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, ALGOLIA_API_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Configure upstream steps and output mapping**: Point step contentful at https://cdn.contentful.com/spaces/{{query.spaceId}}/entries with POST. Add output mapping fields that mirror the JSON contract your screen already expects. POST pipelines set cache TTL to 0 because RealComposer only stores GET responses at the edge.

3. **Call from Next.js / React**: Allowed origins on the token should list every Next.js / React host that calls this URL.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/aggregate-contentful-with-algolia-search', {
  cache: 'no-store',
  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": "Aggregate contentful content with algolia search",
  "method": "POST",
  "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": "algolia",
      "upstreamUrlTemplate": "https://{{secrets.ALGOLIA_APP_ID}}-dsn.algolia.net/1/indexes/{{query.index}}/query",
      "httpMethod": "POST",
      "headersTemplate": {
        "X-Algolia-API-Key": "{{secrets.ALGOLIA_API_KEY}}",
        "X-Algolia-Application-Id": "{{secrets.ALGOLIA_APP_ID}}",
        "Content-Type": "application/json"
      },
      "timeoutMs": 10000
    }
  ],
  "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": "algolia.hitCount",
        "sourcePath": "nbHits",
        "type": "number",
        "sourceStep": "algolia"
      },
      {
        "targetPath": "algolia.firstHit",
        "sourcePath": "hits.0.objectID",
        "type": "string",
        "sourceStep": "algolia"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/aggregate-contentful-with-algolia-search" \
  -H "Authorization: Bearer rc_live_client_token" \
  -H "Content-Type: application/json" \
  -d '{"model":"example"}'
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_aggregat",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "aggregate-contentful-with-algolia-search"
  },
  "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",
  "algolia_hitCount": "sample",
  "algolia_firstHit": "sample"
}
```

## FAQ

### Is it safe to log Contentful responses in Next.js / React?

Use scoped tokens where the vendor allows them on aggregate-contentful-with-algolia-search. Admin keys stay {{secrets.*}} references only.

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

aggregate-contentful-with-algolia-search keeps cache TTL at 0 because it uses POST upstream calls or per-user responses. Add ids to query strings before enabling GET cache.

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

Allowed origins on the aggregate-contentful-with-algolia-search client token restrict which domains may call the edge URL.
