{
  "slug": "call-firebase-admin-from-nextjs-securely",
  "title": "Call firebase admin api from nextjs without exposing service account",
  "metaTitle": "Call firebase admin api from nextjs - RealComposer",
  "metaDescription": "Call firebase admin api from nextjs without exposing service account. Keep Firebase keys in RealComposer Vault; Next.js / React uses rc_live_ tokens only.",
  "headline": "Firebase Admin calls only from the edge",
  "category": "security",
  "categoryLabel": "Security & Secret Vault",
  "urgency": 10,
  "clientPlatform": "Next.js / React",
  "upstreamServices": [
    "Firebase"
  ],
  "searchIntentQuery": "call firebase admin api from nextjs without exposing service account",
  "directAnswerSummary": "Store service account JSON in Vault; Next.js routes call RealComposer instead of Firebase Admin SDK in serverless bundles. For \"call firebase admin api from nextjs without exposing service account\", store FIREBASE_ACCESS_TOKEN in Secret Vault, call https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}} from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/call-firebase-admin-from-nextjs-securely with an rc_live_ client token.",
  "problem": {
    "title": "Why Next.js / React leaks Firebase credentials",
    "explanation": "Attackers routinely extract Firebase secrets from Next.js / React builds. Direct browser calls also expose internal error payloads with hostnames.",
    "risks": [
      "Production Firebase keys copied from Next.js / React bundles",
      "Compliance violations after credential leaks",
      "Attackers replaying vault-grade scopes from a laptop"
    ]
  },
  "solutionSteps": [
    {
      "stepNumber": 1,
      "title": "Add vault secrets for this pipeline",
      "description": "Add FIREBASE_ACCESS_TOKEN to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream."
    },
    {
      "stepNumber": 2,
      "title": "Target the real upstream URL",
      "description": "Point step firebase at https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}} 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)."
    },
    {
      "stepNumber": 3,
      "title": "Call from Next.js / React",
      "description": "Allowed origins on the token should list every Next.js / React host that calls this URL.",
      "codeSnippet": "// app/api route or client component\nconst res = await fetch('https://realcomposer.com/v1/workspace/call-firebase-admin-from-nextjs-securely', {\n  cache: 'no-store',\n  headers: { Authorization: 'Bearer rc_live_your_token' }\n});"
    }
  ],
  "pipelineConfig": {
    "name": "Call firebase admin api from nextjs without exposing service account",
    "method": "GET",
    "cacheTtlSeconds": 0,
    "authMode": "client_token",
    "steps": [
      {
        "stepKey": "firebase",
        "upstreamUrlTemplate": "https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}}",
        "httpMethod": "GET",
        "headersTemplate": {
          "Authorization": "Bearer {{secrets.FIREBASE_ACCESS_TOKEN}}"
        },
        "timeoutMs": 3000
      }
    ],
    "outputMapping": {
      "version": 1,
      "fields": [
        {
          "targetPath": "docName",
          "sourcePath": "name",
          "type": "string",
          "sourceStep": "firebase"
        },
        {
          "targetPath": "fields",
          "sourcePath": "fields",
          "type": "object",
          "sourceStep": "firebase"
        }
      ]
    }
  },
  "sampleBloatedInput": {
    "requestId": "req_call-fir",
    "upstreamLatencyMs": 142,
    "audit": {
      "region": "edge",
      "trace": "call-firebase-admin-from-nextjs-securely"
    },
    "payload": {
      "id": "sample_id",
      "status": "active",
      "nested": {
        "tags": [
          "demo"
        ],
        "internal": true
      },
      "unused_blob": {
        "foo": "bar",
        "bar": "baz"
      }
    }
  },
  "sampleSlimmedOutput": {
    "docName": "sample",
    "fields": "sample"
  },
  "curlSnippet": "curl -X GET \"https://realcomposer.com/v1/workspace/call-firebase-admin-from-nextjs-securely\" \\\n  -H \"Authorization: Bearer rc_live_client_token\"",
  "faqs": [
    {
      "question": "Does Next.js / React need CORS changes after adding RealComposer?",
      "answer": "Log request IDs for call-firebase-admin-from-nextjs-securely, not raw Firebase payloads, because upstream bodies may include PII."
    },
    {
      "question": "What cache key does RealComposer use?",
      "answer": "call-firebase-admin-from-nextjs-securely keeps cache TTL at 0 because it uses POST upstream calls or per-user responses. Add ids to query strings before enabling GET cache."
    },
    {
      "question": "Can I restrict which domains call this pipeline?",
      "answer": "Rotate vault secrets for call-firebase-admin-from-nextjs-securely anytime; clients keep the same rc_live_ token."
    }
  ],
  "relatedRecipeSlugs": [
    "protect-supabase-service-role-key-nextjs",
    "hide-openai-api-key-react"
  ],
  "updatedAt": "2026-11-20",
  "links": {
    "page": "https://realcomposer.com/recipes/call-firebase-admin-from-nextjs-securely",
    "markdown": "https://realcomposer.com/recipes/call-firebase-admin-from-nextjs-securely.md",
    "json": "https://realcomposer.com/recipes/call-firebase-admin-from-nextjs-securely.json",
    "install": "https://realcomposer.com/dashboard/pipelines/new?recipe=call-firebase-admin-from-nextjs-securely&via=agent"
  }
}