# REST API Edge Cdn Caching Guide

> To rest api edge cdn caching guide, set cacheTtlSeconds on a GET pipeline targeting https://jsonplaceholder.typicode.com/users/{{query.userId}}. Identical query strings reuse Cloudflare caches.default before upstream sees traffic.

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

## Problem: Uncached REST API spikes cost and latency

Every Frontend / Web visitor can trigger a fresh REST API 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. **Add vault secrets for this pipeline**: Store API_BEARER_TOKEN in the vault. Frontend / Web never embeds the raw secret, only a revocable rc_live_ client token.

2. **Target the real upstream URL**: Use GET against https://jsonplaceholder.typicode.com/users/{{query.userId}}. Allowed origins on the client token should match set cache ttl to 300s so repeat gets with the same query string hit caches.default.

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

```
const res = await fetch('https://realcomposer.com/v1/workspace/rest-api-edge-cdn-caching-guide', {
  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": "REST API Edge Cdn Caching Guide",
  "method": "GET",
  "cacheTtlSeconds": 300,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "upstream",
      "upstreamUrlTemplate": "https://jsonplaceholder.typicode.com/users/{{query.userId}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.API_BEARER_TOKEN}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "userId",
        "sourcePath": "id",
        "type": "number",
        "sourceStep": "upstream"
      },
      {
        "targetPath": "name",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "upstream"
      },
      {
        "targetPath": "email",
        "sourcePath": "email",
        "type": "string",
        "sourceStep": "upstream"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/rest-api-edge-cdn-caching-guide" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_rest-api",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "rest-api-edge-cdn-caching-guide"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "userId": "sample",
  "name": "sample",
  "email": "sample"
}
```

## FAQ

### Can Frontend / Web call REST API without holding the production key?

Log request IDs for rest-api-edge-cdn-caching-guide, not raw REST API payloads, because upstream bodies may include PII.

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

rest-api-edge-cdn-caching-guide: only GET requests cache (300s on caches.default). Key is workspace slug plus sorted query params, not Authorization.

### How do I rotate upstream credentials?

Upstream steps in rest-api-edge-cdn-caching-guide honor timeoutMs and optional fallback JSON without leaking secrets.
