# Reduce Third Party API Costs with Shared Edge Caching

> REST API rate limits hit fast from Frontend / Web. Cache 300s on GET calls where the query string scopes the response.

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

## Problem: REST API rate limits from Frontend / Web traffic

Without edge caching, identical REST API 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. **Add vault secrets for this pipeline**: Add API_BEARER_TOKEN to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Target the real upstream URL**: Point step upstream at https://jsonplaceholder.typicode.com/users/{{query.userId}} 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 Frontend / Web**: Allowed origins on the token should list every Frontend / Web host that calls this URL.

```
const res = await fetch('https://realcomposer.com/v1/workspace/reduce-third-party-api-costs-caching', {
  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": "Reduce Third Party API Costs with Shared Edge Caching",
  "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/reduce-third-party-api-costs-caching" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_reduce-t",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "reduce-third-party-api-costs-caching"
  },
  "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

### Which REST API credential belongs in the vault for this pipeline?

Point Frontend / Web at /recipes/reduce-third-party-api-costs-caching edge URL. RealComposer answers OPTIONS with Access-Control headers for allowed origins.

### Does POST traffic get cached?

reduce-third-party-api-costs-caching 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?

Rotate vault secrets for reduce-third-party-api-costs-caching anytime; clients keep the same rc_live_ token.
