# Hide anthropic api key in nextjs chat interface

> Next.js client chat UI calls edge stream; Anthropic never touches browser. For "hide anthropic api key in nextjs chat interface", store ANTHROPIC_API_KEY in Secret Vault, call https://api.anthropic.com/v1/messages from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/hide-anthropic-api-key-nextjs with an rc_live_ client token. POST upstream calls are not stored in caches.default; clients must send JSON bodies and headers forward unchanged.

- Canonical page: https://realcomposer.com/recipes/hide-anthropic-api-key-nextjs
- Client platform: Next.js / React
- Upstream services: Anthropic
- Category: Security & Secret Vault
- JSON version: https://realcomposer.com/recipes/hide-anthropic-api-key-nextjs.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=hide-anthropic-api-key-nextjs&via=agent

## Problem: Why Next.js / React leaks Anthropic credentials

Attackers routinely extract Anthropic secrets from Next.js / React builds. Direct browser calls also expose internal error payloads with hostnames.

- Production Anthropic keys copied from Next.js / React bundles
- Compliance violations after credential leaks
- Attackers replaying vault-grade scopes from a laptop

## Solution steps

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

2. **Configure upstream steps and output mapping**: Point step anthropic at https://api.anthropic.com/v1/messages with POST. Add output mapping fields that mirror the JSON contract your screen already expects. POST pipelines set cache TTL to 0 because RealComposer only stores GET responses at the edge.

3. **Call from Next.js / React**: Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/hide-anthropic-api-key-nextjs', {
  cache: 'no-store',
  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": "Hide anthropic api key in nextjs chat interface",
  "method": "POST",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "anthropic",
      "upstreamUrlTemplate": "https://api.anthropic.com/v1/messages",
      "httpMethod": "POST",
      "headersTemplate": {
        "x-api-key": "{{secrets.ANTHROPIC_API_KEY}}",
        "anthropic-version": "2023-06-01",
        "Content-Type": "application/json"
      },
      "timeoutMs": 10000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "role",
        "sourcePath": "content.0.text",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "model",
        "sourcePath": "model",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "stopReason",
        "sourcePath": "stop_reason",
        "type": "string",
        "sourceStep": "anthropic"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/hide-anthropic-api-key-nextjs" \
  -H "Authorization: Bearer rc_live_client_token" \
  -H "Content-Type: application/json" \
  -d '{"model":"example"}'
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_hide-ant",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "hide-anthropic-api-key-nextjs"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "role": "sample",
  "model": "sample",
  "stopReason": "sample"
}
```

## FAQ

### Which Anthropic credential belongs in the vault for this pipeline?

Point Next.js / React at /recipes/hide-anthropic-api-key-nextjs edge URL. RealComposer answers OPTIONS with Access-Control headers for allowed origins.

### Does POST traffic get cached?

Cache key for hide-anthropic-api-key-nextjs: internal edge-cache path plus sorted query. Identical params hit; vary userId per user.

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

Upstream steps in hide-anthropic-api-key-nextjs honor timeoutMs and optional fallback JSON without leaking secrets.
