# How to Cache Shopify Storefront API Queries

> Shopify 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/cache-shopify-storefront-api
- Client platform: Frontend / Web
- Upstream services: Shopify
- Category: Edge Caching & Rate Limits
- JSON version: https://realcomposer.com/recipes/cache-shopify-storefront-api.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=cache-shopify-storefront-api&via=agent

## Problem: Shopify rate limits from Frontend / Web traffic

Without edge caching, identical Shopify 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**: Create SHOPIFY_ACCESS_TOKEN vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Configure upstream steps and output mapping**: Use GET against https://{{secrets.SHOPIFY_STORE}}.myshopify.com/products/{{query.handle}}.js. Allowed origins on the client token should match set cache ttl to 300s so repeat gets with the same query string hit caches.default. Storefront product .js endpoints are GET-friendly for edge cache. Admin GraphQL is POST and bypasses this cache layer.

3. **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/cache-shopify-storefront-api', {
  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": "How to Cache Shopify Storefront API Queries",
  "method": "GET",
  "cacheTtlSeconds": 300,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "shopify",
      "upstreamUrlTemplate": "https://{{secrets.SHOPIFY_STORE}}.myshopify.com/products/{{query.handle}}.js",
      "httpMethod": "GET",
      "headersTemplate": {},
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "productId",
        "sourcePath": "products.0.id",
        "type": "number",
        "sourceStep": "shopify"
      },
      {
        "targetPath": "title",
        "sourcePath": "products.0.title",
        "type": "string",
        "sourceStep": "shopify"
      }
    ]
  }
}
```

## Call the pipeline

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

## Payload before and after

Raw upstream response:

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

Slimmed RealComposer response:

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

## FAQ

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

Use scoped tokens where the vendor allows them on cache-shopify-storefront-api. Admin keys stay {{secrets.*}} references only.

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

cache-shopify-storefront-api: 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 cache-shopify-storefront-api honor timeoutMs and optional fallback JSON without leaking secrets.
