# Cache Coinmarketcap Crypto Pricing API at Edge

> To cache coinmarketcap crypto pricing api at edge, set cacheTtlSeconds on a GET pipeline targeting https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol={{query.symbol}}. Identical query strings reuse Cloudflare caches.default before upstream sees traffic.

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

## Problem: Uncached CoinMarketCap spikes cost and latency

Every Frontend / Web visitor can trigger a fresh CoinMarketCap 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. **Store upstream credentials in Secret Vault**: Store COINMARKETCAP_API_KEY in the vault. Frontend / Web never embeds the raw secret, only a revocable rc_live_ client token.

2. **Configure upstream steps and output mapping**: Use GET against https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol={{query.symbol}}. 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**: Allowed origins on the token should list every Frontend / Web host that calls this URL.

```
const res = await fetch('https://realcomposer.com/v1/workspace/cache-crypto-pricing-api-edge', {
  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 Coinmarketcap Crypto Pricing API at Edge",
  "method": "GET",
  "cacheTtlSeconds": 300,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "cmc",
      "upstreamUrlTemplate": "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol={{query.symbol}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "X-CMC_PRO_API_KEY": "{{secrets.COINMARKETCAP_API_KEY}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "price",
        "sourcePath": "data.BTC.quote.USD.price",
        "type": "number",
        "sourceStep": "cmc"
      },
      {
        "targetPath": "symbol",
        "sourcePath": "data.BTC.symbol",
        "type": "string",
        "sourceStep": "cmc"
      }
    ]
  }
}
```

## Call the pipeline

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

## Payload before and after

Raw upstream response:

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

Slimmed RealComposer response:

```json
{
  "price": "sample",
  "symbol": "sample"
}
```

## FAQ

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

Log request IDs for cache-crypto-pricing-api-edge, not raw CoinMarketCap payloads, because upstream bodies may include PII.

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

cache-crypto-pricing-api-edge forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates.

### What happens if an upstream step times out?

Allowed origins on the cache-crypto-pricing-api-edge client token restrict which domains may call the edge URL.
