Call firebase admin api from nextjs without exposing service account
Firebase Admin calls only from the edge
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.
Why Next.js / React leaks Firebase credentials
Attackers routinely extract Firebase secrets from Next.js / React builds. Direct browser calls also expose internal error payloads with hostnames.
- Production Firebase keys copied from Next.js / React bundles
- Compliance violations after credential leaks
- Attackers replaying vault-grade scopes from a laptop
How RealComposer Solves This at the Edge
Add vault secrets for this pipeline
Add FIREBASE_ACCESS_TOKEN to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.
Target the real upstream URL
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).
Call from Next.js / React
Allowed origins on the token should list every Next.js / React host that calls this URL.
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/call-firebase-admin-from-nextjs-securely', {
cache: 'no-store',
headers: { Authorization: 'Bearer rc_live_your_token' }
});
{
"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"
}
}
}
{
"docName": "sample",
"fields": "sample"
}
Recipe Specs
- HTTP method
- GET
- Upstream calls
- 1
- Edge cache TTL
- No caching
- Client auth
- Restricted client token
- Mapped output fields
- 2
- Payload
- 259 B to 38 B (-85%)
curl -X GET "https://realcomposer.com/v1/workspace/call-firebase-admin-from-nextjs-securely" \ -H "Authorization: Bearer rc_live_client_token"
Developer Questions & Troubleshooting
Does Next.js / React need CORS changes after adding RealComposer?
Log request IDs for call-firebase-admin-from-nextjs-securely, not raw Firebase payloads, because upstream bodies may include PII.
What cache key does RealComposer use?
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.
Can I restrict which domains call this pipeline?
Rotate vault secrets for call-firebase-admin-from-nextjs-securely anytime; clients keep the same rc_live_ token.
Sources & Definitions Used in This Answer
Deploy this Next.js / React + Firebase Recipe
Clone this pre-configured pipeline into your RealComposer workspace in one click. We pre-populate upstream URLs, headers, and mapping rules.