{
  "slug": "hide-anthropic-api-key-nextjs",
  "title": "Hide anthropic api key in nextjs chat interface",
  "metaTitle": "Hide anthropic api key in nextjs - RealComposer",
  "metaDescription": "Hide anthropic api key in nextjs chat interface. Keep Anthropic keys in RealComposer Vault; Next.js / React uses rc_live_ tokens only.",
  "headline": "Streaming Claude responses with vault-only keys",
  "category": "security",
  "categoryLabel": "Security & Secret Vault",
  "urgency": 9,
  "clientPlatform": "Next.js / React",
  "upstreamServices": [
    "Anthropic"
  ],
  "searchIntentQuery": "hide anthropic api key in nextjs chat interface",
  "directAnswerSummary": "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.",
  "problem": {
    "title": "Why Next.js / React leaks Anthropic credentials",
    "explanation": "Attackers routinely extract Anthropic secrets from Next.js / React builds. Direct browser calls also expose internal error payloads with hostnames.",
    "risks": [
      "Production Anthropic keys copied from Next.js / React bundles",
      "Compliance violations after credential leaks",
      "Attackers replaying vault-grade scopes from a laptop"
    ]
  },
  "solutionSteps": [
    {
      "stepNumber": 1,
      "title": "Store upstream credentials in Secret Vault",
      "description": "Add ANTHROPIC_API_KEY to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream."
    },
    {
      "stepNumber": 2,
      "title": "Configure upstream steps and output mapping",
      "description": "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."
    },
    {
      "stepNumber": 3,
      "title": "Call from Next.js / React",
      "description": "Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.",
      "codeSnippet": "// app/api route or client component\nconst res = await fetch('https://realcomposer.com/v1/workspace/hide-anthropic-api-key-nextjs', {\n  cache: 'no-store',\n  headers: { Authorization: 'Bearer rc_live_your_token' }\n});"
    }
  ],
  "pipelineConfig": {
    "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"
        }
      ]
    }
  },
  "sampleBloatedInput": {
    "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"
      }
    }
  },
  "sampleSlimmedOutput": {
    "role": "sample",
    "model": "sample",
    "stopReason": "sample"
  },
  "curlSnippet": "curl -X POST \"https://realcomposer.com/v1/workspace/hide-anthropic-api-key-nextjs\" \\\n  -H \"Authorization: Bearer rc_live_client_token\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"model\":\"example\"}'",
  "faqs": [
    {
      "question": "Which Anthropic credential belongs in the vault for this pipeline?",
      "answer": "Point Next.js / React at /recipes/hide-anthropic-api-key-nextjs edge URL. RealComposer answers OPTIONS with Access-Control headers for allowed origins."
    },
    {
      "question": "Does POST traffic get cached?",
      "answer": "Cache key for hide-anthropic-api-key-nextjs: internal edge-cache path plus sorted query. Identical params hit; vary userId per user."
    },
    {
      "question": "What happens if an upstream step times out?",
      "answer": "Upstream steps in hide-anthropic-api-key-nextjs honor timeoutMs and optional fallback JSON without leaking secrets."
    }
  ],
  "relatedRecipeSlugs": [
    "hide-openai-api-key-react",
    "anthropic-claude-api-frontend-proxy"
  ],
  "updatedAt": "2026-11-20",
  "links": {
    "page": "https://realcomposer.com/recipes/hide-anthropic-api-key-nextjs",
    "markdown": "https://realcomposer.com/recipes/hide-anthropic-api-key-nextjs.md",
    "json": "https://realcomposer.com/recipes/hide-anthropic-api-key-nextjs.json",
    "install": "https://realcomposer.com/dashboard/pipelines/new?recipe=hide-anthropic-api-key-nextjs&via=agent"
  }
}