# Protect API Key in Vue SPA

> Vue / SPA cannot safely store REST API production keys. Vault-bound headers reach https://jsonplaceholder.typicode.com/users/{{query.userId}} on Workers while apps call your hide-api-key-vue-spa edge URL.

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

## Problem: Why Vue / SPA leaks REST API credentials

Attackers routinely extract REST API secrets from Vue / SPA builds. Direct browser calls also expose internal error payloads with hostnames.

- Production REST API keys copied from Vue / SPA bundles
- Compliance violations after credential leaks
- Attackers replaying vault-grade scopes from a laptop

## Solution steps

1. **Bind vault keys to the Worker**: Create API_BEARER_TOKEN vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Set headers, mapping, and cache TTL**: 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. 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 Vue / SPA**: Call https://realcomposer.com/v1/workspace/hide-api-key-vue-spa with a restricted rc_live_ client token. Vue / SPA never sends upstream secrets.

```
const { data } = await useFetch('https://realcomposer.com/v1/workspace/hide-api-key-vue-spa', {
  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": "Protect API Key in Vue SPA",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "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/hide-api-key-vue-spa" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_hide-api",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "hide-api-key-vue-spa"
  },
  "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

### Can Vue / SPA call REST API without holding the production key?

Use scoped tokens where the vendor allows them on hide-api-key-vue-spa. Admin keys stay {{secrets.*}} references only.

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

hide-api-key-vue-spa POST steps bypass caches.default even if cacheTtlSeconds is set in the dashboard.

### How do I rotate upstream credentials?

Allowed origins on the hide-api-key-vue-spa client token restrict which domains may call the edge URL.
