Stock Master Tracker docs
v2.0.0
Get help
● Set up · Cloud Functions

Firebase backend
project, rules, functions.

Create your own Firebase project, put it on the Blaze plan, deploy the functions and rules, and copy the API address the app needs.

Blaze planNode 22us-central1 by default

011. Create the project

  1. New projectIn the Firebase console create a project. Note its project id.
  2. Upgrade to BlazeCloud Functions, Cloud Scheduler and calls to outside services need the pay-as-you-go plan. Google bills usage above the free tier to you. In Google Cloud Billing set a budget alert.
  3. AuthenticationBuild → Authentication → Sign-in method: enable Anonymous (required) and, if you want them, Google, Apple and Email/Password.
  4. FirestoreBuild → Firestore Database → Create database (Native mode).
  5. Cloud MessagingEnabled by default; iOS also needs an APNs key (Alerts and push).

022. Deploy rules, indexes and functions

npm i -g firebase-tools
firebase login
cd firebase
firebase use --add                 # pick your project; writes .firebaserc
cd functions && npm ci && cd ..
firebase deploy --only firestore:rules,firestore:indexes,functions
  • Region: us-central1 by default (copy firebase/functions/.env.example to .env and set FUNCTIONS_REGION to change it). Use the same region in API_BASE_URL.
  • api is deployed with public invoker access; it checks Firebase ID tokens itself for account endpoints.
  • The indexes the console's queries need are in firebase/firestore.indexes.json. Skip them and some console pages fail with a missing-index message.
  • Until the console has saved its settings once, the backend uses the defaults from the contract: everything that costs or earns money is off, and no market-data provider is configured.
How to confirm success

After the deploy the CLI prints the function URLs. Open https://<region>-<your-project-id>.cloudfunctions.net/api/v1/config in a browser: you get JSON with brand.appName "Stock Master". In the Firebase console → Functions you see api, evaluateAlerts, refreshNews, refreshBrief and sendCampaigns.

033. API_BASE_URL for the app

The app talks to exactly one HTTP API. Build it with the function URL:

flutter build apk --dart-define=API_BASE_URL=https://us-central1-<your-project-id>.cloudfunctions.net/api

Paths are relative to it: <API_BASE_URL>/v1/quotes. Optionally put the API behind Firebase Hosting on your own domain with a rewrite so the CDN honours the API's Cache-Control headers:

"hosting": { "public": "public", "rewrites": [ { "source": "/api/**", "function": { "functionId": "api", "region": "us-central1" } } ] }

and use API_BASE_URL=https://<your-domain>/api. Any prefix before /v1 is ignored by the API.

04Keys and secrets live in the console, never in code

Every third-party key is stored in the Firestore document config/private, which no client can read. You enter them in the console, which writes the document. Nothing secret is in the app or in an environment file of the functions.

Field in config/privateWhat it is
stocks.finnhubKey, stocks.twelveDataKeyMarket-data keys
stocks.edgarContactThe contact the SEC asks for (not a secret)
stocks.route, budgets, ttl.*Routing, per-minute and per-day budgets, cache times
news.sourcesYour RSS feeds
ai.provider, ai.apiKey, ai.model, ai.baseUrl, limitsOptional AI text
iap.*Play and App Store verification
adReporting.*Optional ad-revenue reporting keys

Without iap settings /v1/purchases/verify answers 503 iap_unconfigured and grants nothing.

05Account deletion

Settings → "Delete account" in the app calls DELETE /v1/me (the stores require in-app deletion). The API deletes users/{uid} with its alerts, devices and sync data, the user's usage counters and alert events, then the Firebase Auth user. It keeps purchases/* and revenueEvents/*: financial records you may have to retain (tax, refunds); their uid remains only as an opaque id. Mention this in your privacy policy. Source: firebase/functions/src/api/users.ts.

06What drives the Firebase bill

  • Function invocations of the API and of the 5-minute jobs; Firestore reads and writes for the cache and users.
  • Every response is cached (quotes 60 s, charts 5 min to 1 h, profile 24 h, filings 15 min and so on; editable in the console) in memory and in Firestore cache/*, which cuts both provider calls and cost.
  • AI calls are billed by the AI provider, not by Firebase, and are capped per day.

Check Firebase's and Google Cloud's pricing pages yourself; these docs quote no prices.

07Local development and tests

cd firebase/functions
npm run dev:api     # the API on http://localhost:8787; in-memory store, demo config
npm test            # unit tests, no network
npm run test:emu    # integration on the Auth + Firestore emulators
npm run test:rules  # security rules on the Firestore emulator

DEV_AUTH=1 npm run dev:api accepts Authorization: Bearer dev:<uid> for testing account endpoints. Never use it on a server.