Connect to Firebase

Before you start

This guide is for an app that already uses Firebase. moonbase runs your app; your data stays in your Firebase project.

moonbase has its own managed PostgreSQL database too. It is in a closed pilot: customers can't create one yet, and there is no date for wider availability. When a mission has one, its status is in the Database section of the mission's page, and the owner can export it, see Export Your Database. Until then, connect the outside service you already use, as below.

Which value goes in which variable

Take the values from your Firebase project settings. Use the variable names your code already reads; these are the usual ones:

• The web app config (apiKey, authDomain, projectId, storageBucket, messagingSenderId, appId): VITE_FIREBASE_API_KEY, VITE_FIREBASE_AUTH_DOMAIN, VITE_FIREBASE_PROJECT_ID, VITE_FIREBASE_STORAGE_BUCKET, VITE_FIREBASE_MESSAGING_SENDER_ID, VITE_FIREBASE_APP_ID (or the same names with NEXT_PUBLIC_ for Next.js). Public: Firebase designs this config to be in the browser. What protects your data is Firebase Security Rules, so write rules for every collection and bucket the browser uses.

• A service account (the JSON key file for the Admin SDK): server-only. moonbase refuses a value with line breaks, and the file has many, so save it base64-encoded as one line in FIREBASE_SERVICE_ACCOUNT_BASE64 and decode it in your server code (example below).

Where to enter them

Open your mission in Mission Control and go to Environment Variables. Type each value there. Values are write-only: after a save the screen shows only the name. Secrets go on that screen only, never in a chat with an AI assistant and never in your code or your ZIP.

An assistant connected to moonbase can save the public values for you. For a secret, it sends you to that same screen and you type it yourself.

A change applies on the next deploy: use Save and redeploy, or Deploy new version.

How the values reach your app

How a value reaches your app depends on what kind of mission it is:

• A mission served as it is (an index.html at the root, no build step): moonbase passes it no environment variables at all. Every file in it is public, so it can only hold public values, written straight into the page.

• A front end moonbase builds (Vite, Vue, Create React App, Astro, SvelteKit, Next.js): during the build, moonbase gives the build only the variables that carry the framework's public prefix: NEXT_PUBLIC_ for Next.js, VITE_ for Vite and Vue, REACT_APP_ for Create React App, PUBLIC_ for Astro and SvelteKit. The framework writes those into the files the browser downloads. A value without the prefix never reaches the build.

• Server code (a Next.js server, Express, Nuxt, SvelteKit with the Node adapter, Python): every saved variable is in the environment when the app starts (process.env in Node, os.environ in Python). Server-only secrets belong here, without a public prefix.

The build can download packages but can't reach other services. Load data when a visitor opens the page, not while the app is being built.

A Firebase web app publishes with or without a build step

The Firebase web config is public by Firebase's design, so moonbase publishes it as it is. Paste the config object the Firebase console gives you (apiKey, authDomain ending in .firebaseapp.com, projectId and the rest) straight into your HTML or JavaScript: no build step needed. Its apiKey starts with AIza, the same shape as other Google API keys, and those are still refused (hardcoded_secret_detected): a Maps or Places key, an AIza key anywhere outside a Firebase config, and any AIza key in a .env file. Keep those keys in an environment variable read by server code. With a build step (Vite, Next.js) you can also keep the web config in VITE_ or NEXT_PUBLIC_ variables, as in the examples below.

Example: a Vite app (no server)

// src/firebase.js
import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from "firebase/firestore";

const app = initializeApp({
  apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
  authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
  projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
  storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
  appId: import.meta.env.VITE_FIREBASE_APP_ID,
});

export const db = getFirestore(app);
export const loadNotes = async () => (await getDocs(collection(db, "notes"))).docs.map((d) => d.data());

Upload the source (package.json, index.html, src/), not a built dist folder. moonbase reads the variable names from your code and lists any that have no value yet.

Example: a Next.js app with the Admin SDK

The web config uses the NEXT_PUBLIC_ names in browser code, as in the Vite example. Server code uses the service account:

// app/api/notes/route.js: server only, runs on each request
import { cert, getApps, initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";

export const dynamic = "force-dynamic";

function adminApp() {
  if (getApps().length) return getApps()[0];
  const json = Buffer.from(process.env.FIREBASE_SERVICE_ACCOUNT_BASE64, "base64").toString("utf8");
  return initializeApp({ credential: cert(JSON.parse(json)) });
}

export async function GET() {
  const snapshot = await getFirestore(adminApp()).collection("notes").get();
  return Response.json(snapshot.docs.map((d) => d.data()));
}

To make the one-line value on your own computer: base64 -i service-account.json | tr -d '\n' (macOS) or base64 -w0 service-account.json (Linux). Paste the output on the Environment Variables screen, then delete the copy.

Sign-in with Firebase Authentication

If people sign in, add your mission's live address (and your custom domain, if you have one) to the authorized domains in your Firebase Authentication settings. Until you do, sign-in from the live address fails.