Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 31 ยท Backend as a Service

Creating Projects and Using the SDKs

Create a Firebase and a Supabase project, install the SDKs, understand public keys versus secrets, use environment variables and run local emulators.

Before any code talks to a BaaS, you need a project: a container in the cloud that holds your database, users and files. Creating one is quick, but the setup step hides the most common beginner mistake in this field, which is mixing up public keys (fine in a browser) with secret keys (never in a browser). This lesson walks through both platforms and shows how to tell the two kinds of key apart.

All commands and SDK code here are read-only, because creating a project needs an account. The runnable stand-in at the end validates a config object and hides secrets, which is the habit worth practicing.

Firebase: console and CLI

In the Firebase console you click Add project, give it a name, and then register an app (web, iOS or Android). For a web app the console shows a config object like this one. All values below are made up.

JavaScript
// firebase.js  (read-only)
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
  apiKey: "AIzaSyExampleExampleExampleExample000",
  authDomain: "my-demo-app.firebaseapp.com",
  projectId: "my-demo-app",
  storageBucket: "my-demo-app.firebasestorage.app",
  messagingSenderId: "123456789012",
  appId: "1:123456789012:web:abcdef0123456789",
};

export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const db = getFirestore(app);

The apiKey here only identifies your project to Google's services. It is not a password, and it ships in every browser bundle. What actually protects your data are the security rules and, optionally, App Check. We cover those in Firestore Security Rules and Security Pitfalls in Both Platforms.

The Firebase CLI (installed with npm) handles projects from the terminal:

Terminal
npm install -g firebase-tools
firebase login
firebase init            # choose products: Firestore, Functions, Hosting, Emulators
firebase deploy          # publish rules, functions and hosting

firebase init creates firebase.json (which products you use) and .firebaserc (which project the folder belongs to).

Supabase: dashboard and CLI

In the Supabase dashboard you create a project, pick a region and set a database password. The project gets a URL and two API keys.

JavaScript
// supabase.js  (read-only)
import { createClient } from "@supabase/supabase-js";

export const supabase = createClient(
  "https://abcdefghijklmnop.supabase.co",       // your project URL
  "YOUR_PUBLIC_ANON_KEY"                        // the public (anon) key
);

The two keys have very different powers.

  • The anon key (newer projects may call it a publishable key) is meant for browsers. It acts as the role anon, or as authenticated once a user signs in, and everything it does passes through your row level security policies.
  • The service role key (newer projects may call it a secret key) bypasses row level security completely. It belongs only on servers: Edge Functions, scripts, CI. If it leaks into a browser bundle, anyone can read and delete your entire database.

Never put the service role key in front-end code, in a public repository, or in a variable that your bundler exposes to the browser. If it ever leaks, rotate it in the dashboard immediately.

The Supabase CLI manages the local stack and your database migrations:

Terminal
npm install supabase --save-dev
npx supabase init        # creates a supabase/ folder
npx supabase start       # starts the local stack in Docker
npx supabase link --project-ref abcdefghijklmnop
npx supabase db push     # apply local migrations to the hosted project

Public or secret?

Public Firebase config, Supabase anon key, project URL.
Secret Service role key, database password, private API keys.

Public keys identify. Rules and policies protect.

1 / 2

Environment variables

Even public values should not be pasted in many places. Keep them in environment variables and read them in one module. Frameworks mark which variables may reach the browser by a name prefix. For example, Vite exposes only names starting with VITE_, and Next.js only NEXT_PUBLIC_. That prefix is a safety fence: anything without it stays on the server.

Terminal
# .env.local  (read-only)  -- add this file to .gitignore
VITE_SUPABASE_URL=https://abcdefghijklmnop.supabase.co
VITE_SUPABASE_ANON_KEY=public-anon-key-here

# No VITE_ prefix: never reaches the browser
SUPABASE_SERVICE_ROLE_KEY=secret-service-role-key-here

Commit a .env.example with fake values so teammates know which variables exist, and keep real .env files out of version control. For Node.js basics see the Node.js tutorial.

Emulators and the local stack

You do not want to develop against your live project. Both platforms offer a local version.

  • Firebase Emulator Suite: run firebase emulators:start to get local Auth, Firestore, Functions, Storage and Hosting, plus a web UI. Your code connects with calls such as connectFirestoreEmulator(db, "127.0.0.1", 8080). It is the right place to test security rules.
  • Supabase local stack: npx supabase start launches PostgreSQL, Auth, Storage, Realtime and the dashboard in Docker containers and prints a local URL and local keys. Migrations in supabase/migrations are applied on npx supabase db reset. It needs Docker running, which you can learn in the Docker tutorial.

Local data is disposable, so you can break things freely and reset.

Stand-in: validating a config and hiding secrets

The first runnable example checks that required variables exist, classifies each as public or secret by its name, and prints a masked report so a secret never reaches the logs. This is a stand-in for the validation module you would write at the start of a real project.

Node.js Stand-in: validate config and mask secrets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Add VITE_FIREBASE_APP_ID to env and the missing-variable problem disappears. Now rename SUPABASE_SERVICE_ROLE_KEY to VITE_SUPABASE_SERVICE_ROLE_KEY and rerun: the checker flags it, because that name would put a secret into the browser bundle.

Stand-in: spotting a leaked key in a bundle

A second small check scans text, such as a built JavaScript file, for a JWT-shaped value whose payload says role: service_role. Real Supabase keys are JWTs, and their middle part is readable base64url. This example builds a fake key itself so the demo is self-contained.

Node.js Stand-in: scan a bundle for a service role key

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Real keys come from the dashboard, so this is only an illustration. The idea is real, though: attackers scan public bundles for exactly this pattern.

A short checklist

  1. Create separate projects for development and production, never one shared one.
  2. Put public values in prefixed variables and secrets in server-only variables.
  3. Keep a .env.example, ignore .env* files in git.
  4. Develop against emulators or the local stack.
  5. Make a single firebase.js or supabase.js module that creates the client once and exports it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which value is safe to ship in a browser bundle?

    The anon key is designed for browsers because everything it does passes through row level security. The others grant broad or privileged access.
  2. 2. What does the Supabase service role key do?

    The service role key skips RLS entirely, so it must stay on trusted servers.
  3. 3. What protects Firestore data when the Firebase apiKey is public?

    The apiKey only identifies the project. Rules decide who may read and write.
  4. 4. Why does a build tool like Vite require a VITE_ prefix?

    Unprefixed variables stay on the server side, which helps prevent accidental secret leaks.

Summary

  • A project holds your backend resources; use separate projects for development and production.
  • Firebase's config values and Supabase's anon key and URL are public identifiers; rules and policies provide the protection.
  • The Supabase service role key bypasses RLS and belongs only on servers, in environment variables.
  • Use prefixed environment variables, a .env.example, and git ignores to keep secrets out of bundles.
  • Emulators (Firebase) and the local Docker stack (Supabase) let you develop without touching production.

Next: Email and Password Sign-In