Stale While Revalidate Caching for Third Party REST Apis
REST API GET traffic cached ahead of rate limits
REST API rate limits hit fast from Frontend / Web. Cache 300s on GET calls where the query string scopes the response.
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
How RealComposer Solves This at the Edge
Bind vault keys to the Worker
Create API_BEARER_TOKEN vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.
Configure upstream steps and output mapping
Configure headersTemplate to inject vault secrets, target https://jsonplaceholder.typicode.com/users/{{query.userId}}, and Add output mapping fields that mirror the JSON contract your screen already expects.
Call from Frontend / Web
Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.
const res = await fetch('https://realcomposer.com/v1/workspace/stale-while-revalidate-third-party-api', {
headers: { Authorization: 'Bearer rc_live_your_token' }
});
{
"requestId": "req_stale-wh",
"upstreamLatencyMs": 142,
"audit": {
"region": "edge",
"trace": "stale-while-revalidate-third-party-api"
},
"payload": {
"id": "sample_id",
"status": "active",
"nested": {
"tags": [
"demo"
],
"internal": true
},
"unused_blob": {
"foo": "bar",
"bar": "baz"
}
}
}
{
"userId": "sample",
"name": "sample",
"email": "sample"
}
Recipe Specs
- HTTP method
- GET
- Upstream calls
- 1
- Edge cache TTL
- 300s
- Client auth
- Restricted client token
- Mapped output fields
- 3
- Payload
- 257 B to 52 B (-80%)
curl -X GET "https://realcomposer.com/v1/workspace/stale-while-revalidate-third-party-api" \ -H "Authorization: Bearer rc_live_client_token"
Developer Questions & Troubleshooting
What should Frontend / Web store instead of REST API secrets?
Use scoped tokens where the vendor allows them on stale-while-revalidate-third-party-api. Admin keys stay {{secrets.*}} references only.
How do I forward JSON bodies to POST upstreams?
Cache key for stale-while-revalidate-third-party-api: internal edge-cache path plus sorted query. Identical params hit; vary userId per user.
How do I rotate upstream credentials?
Allowed origins on the stale-while-revalidate-third-party-api client token restrict which domains may call the edge URL.
Sources & Definitions Used in This Answer
- DefinitionStale-While-Revalidate (SWR)
- DefinitionCache-Control Headers
- DefinitionStale-If-Error Caching
- DefinitionHTTP Caching
- DefinitionNegative Caching
- DefinitionSoft TTL
- DefinitionAPI SLO and SLA
- Stack guideWeb Frontends safe & unsafe practices
- Stack guideREST APIs safe & unsafe practices
- Official docsWeb Frontends documentation
- Official docsREST APIs documentation
Deploy this Frontend / Web + REST API Recipe
Clone this pre-configured pipeline into your RealComposer workspace in one click. We pre-populate upstream URLs, headers, and mapping rules.