Getting Started with Cloud Firestore with Node.js - Firecasts
This is what the video above suggested, when you want to run your own NodeJS server.
const firebase_admin = require('firebase-admin'); const serviceAccount = require("/path/to/yourserviceaccountkey.json"); const admin = firebase_admin.initializeApp({ credential: firebase_admin.credential.cert(serviceAccount) });
What if I don’t have ServiceAccountKey.json?
Make sure to add and initialize the SDK by following these steps:
By following the steps in that article, you can generate that JSON file.
An alternative (and easier) approach by just using the Google Cloud Shell:
Open Google Cloud Shell editor: https://console.cloud.google.com/home/
Create a new file.
Add the Firebase Admin SDK to your server
npm install firebase-admin
Now you have access to your firestore!
const admin = require('firebase-admin'); admin.initializeApp( { credential: admin.credential.applicationDefault(), projectId: "xxxxx-xxxx", databaseURL: "YOUR_DATABASE_URL", } ); // Initialize Cloud Firestore and get a reference to the service const db = admin.firestore(); // Read or write to the firestore database db.collection("users") .get().then((querySnapshot) => { querySnapshot.forEach((doc) => { console.log(doc.data()); }); });