# Cache contentful delivery api at edge avoid limits

> 300s TTL on composed entry responses at Cloudflare edge. For "cache contentful delivery api at edge avoid limits", store CONTENTFUL_ACCESS_TOKEN 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/cache-contentful-delivery-api-edge with an rc_live_ client token. Enable 300s GET cache only when query params identify the resource.

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

## Problem: Contentful rate limits from Next.js / React traffic

Without edge caching, identical Contentful reads during a session still round-trip to the origin.

- HTTP 429 responses during launch traffic
- Surprise SaaS overages from duplicate reads
- Slower repeat views than necessary

## Solution steps

1. **Store upstream credentials in Secret Vault**: Add CONTENTFUL_ACCESS_TOKEN to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Configure upstream steps and output mapping**: Point step contentful at https://cdn.contentful.com/spaces/{{query.spaceId}}/entries 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 Next.js / React**: Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/cache-contentful-delivery-api-edge', {
  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": "Cache contentful delivery api at edge avoid limits",
  "method": "GET",
  "cacheTtlSeconds": 300,
  "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
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "entryId",
        "sourcePath": "items.0.sys.id",
        "type": "string",
        "sourceStep": "contentful"
      },
      {
        "targetPath": "title",
        "sourcePath": "items.0.fields.title",
        "type": "string",
        "sourceStep": "contentful"
      }
    ]
  }
}
```

## Call the pipeline

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

## Payload before and after

Raw upstream response:

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

Slimmed RealComposer response:

```json
{
  "entryId": "sample",
  "title": "sample"
}
```

## FAQ

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

Store CONTENTFUL_ACCESS_TOKEN for cache-contentful-delivery-api-edge. Next.js / React never sees decrypted values.

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

cache-contentful-delivery-api-edge vault headers attach server-side, but Anthropic or OpenAI model and messages must come from the client body.

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

Upstream steps in cache-contentful-delivery-api-edge honor timeoutMs and optional fallback JSON without leaking secrets.
