# Reverse Proxy for Third Party API Key Protection

> Never place API_BEARER_TOKEN inside Frontend / Web source. RealComposer injects them when calling https://{{secrets.API_HOST}}/status, so browsers see only a client token.

- Canonical page: https://realcomposer.com/recipes/reverse-proxy-api-key-protection
- Client platform: Frontend / Web
- Upstream services: REST API
- Category: Security & Secret Vault
- JSON version: https://realcomposer.com/recipes/reverse-proxy-api-key-protection.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=reverse-proxy-api-key-protection&via=agent

## Problem: REST API keys in Frontend / Web network tabs

Frontend / Web ships code or scripts to end-user devices. Any REST API bearer token in those bundles can be copied from DevTools, proxies, or repackaged apps.

- Shared Frontend / Web projects revealing REST API tokens
- Sudden REST API quota burn from scripted abuse
- Inability to rotate keys without redeploying clients

## Solution steps

1. **Store upstream credentials in Secret Vault**: Add API_BEARER_TOKEN to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Set headers, mapping, and cache TTL**: Point step upstream at https://{{secrets.API_HOST}}/status with GET. Add output mapping fields that mirror the JSON contract your screen already expects. Leave cache TTL at 0 when each response is user-specific (include userId or record id in query params before enabling cache).

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/reverse-proxy-api-key-protection', {
  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": "Reverse Proxy for Third Party API Key Protection",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "upstream",
      "upstreamUrlTemplate": "https://{{secrets.API_HOST}}/status",
      "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/reverse-proxy-api-key-protection" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_reverse-",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "reverse-proxy-api-key-protection"
  },
  "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

### What should Frontend / Web store instead of REST API secrets?

Log request IDs for reverse-proxy-api-key-protection, not raw REST API payloads, because upstream bodies may include PII.

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

Cache key for reverse-proxy-api-key-protection: internal edge-cache path plus sorted query. Identical params hit; vary userId per user.

### Can I restrict which domains call this pipeline?

Allowed origins on the reverse-proxy-api-key-protection client token restrict which domains may call the edge URL.
