# Call Clerk Backend API from Client Side

> Never place CLERK_SECRET_KEY inside Frontend / Web source. RealComposer injects them when calling https://api.clerk.com/v1/users/{{query.userId}}, so browsers see only a client token.

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

## Problem: Clerk keys in Frontend / Web network tabs

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

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

## Solution steps

1. **Add vault secrets for this pipeline**: Create CLERK_SECRET_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Target the real upstream URL**: Point step clerk at https://api.clerk.com/v1/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 Frontend / Web**: Call https://realcomposer.com/v1/workspace/clerk-backend-api-proxy with a restricted rc_live_ client token. Frontend / Web never sends upstream secrets.

```
const res = await fetch('https://realcomposer.com/v1/workspace/clerk-backend-api-proxy', {
  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": "Call Clerk Backend API from Client Side",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "clerk",
      "upstreamUrlTemplate": "https://api.clerk.com/v1/users/{{query.userId}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.CLERK_SECRET_KEY}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "userId",
        "sourcePath": "id",
        "type": "string",
        "sourceStep": "clerk"
      },
      {
        "targetPath": "email",
        "sourcePath": "email_addresses.0.email_address",
        "type": "string",
        "sourceStep": "clerk"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/clerk-backend-api-proxy" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_clerk-ba",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "clerk-backend-api-proxy"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "userId": "sample",
  "email": "sample"
}
```

## FAQ

### Does Frontend / Web need CORS changes after adding RealComposer?

Yes for clerk-backend-api-proxy. Ship only an rc_live_ client token in Frontend / Web; RealComposer injects CLERK_SECRET_KEY on https://api.clerk.com/v1/users/{{query.userId}}.

### What cache key does RealComposer use?

clerk-backend-api-proxy forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates.

### How do I rotate upstream credentials?

Upstream steps in clerk-backend-api-proxy honor timeoutMs and optional fallback JSON without leaking secrets.
