JEN PERSON: Hey, all you Firebase developers.
Let's talk about two of my favorite things, Cloud
Firestore and the Admin SDK.
So I love that Firebase has such robust client-side SDKs
for iOS, Android, and web.
But did you know that Firebase also
has a server-side option, the Admin SDK?
The Admin SDK lets you access Firebase features server side.
So you can, for example, read and write
to Cloud Firestore outside of the client.
This is especially useful for offloading CPU-intensive tasks
and keeping secrets away from the client.
Now, if you've seen Todd's videos
on getting started with Cloud Firestore for iOS or Android,
you'll already be familiar with the sample
app I'm going to use, one that stores an inspiring quote.
If you haven't seen these videos,
I highly recommend you check them out.
Todd gives a great overview of the Firestore structure.
I'm going to use the iOS version of the inspiring quotes
app, which looks like this.
You can see the quote and author, as well as text fields
to write a new quote and a new author.
In this video, I'll show you how to use the Admin SDK to make
changes to Cloud Firestore, which can then
be seen here in the client.
So you may be wondering, how can I
keep my quotes fresh with new inspiration every day?
I could rely solely on users to write quotes or just
try to remember something interesting and type
it in the console manually.
After all, there's no shortage of misinterpreted quotes
on the internet.
Didn't George Washington say that?
Anyway, one neat way I could handle this is server side.
I could use one of the many quote-generating REST
APIs out there to get my quotes for me
and then write them to the database using the Admin SDK.
I can also use server-side code to send a notification
to users or something like that, if I wanted to.
So that's what I want to show you today,
how to get started using the Admin SDK to read and write
data to Cloud Firestore.
I'm going to show you the Firebase Admin SDK for Python.
But Node.js, Java, and Go are also supported.
Let's get started.
First I need service account credentials,
which I can download from the console.
Under the Firebase Admin SDK tab,
I'll select Generate Private Key.
Now, this service account JSON file
contains sensitive information, including
your service account's private encryption key.
Remember to keep it confidential.
Never add it to version control.
Never put it in a client app.
And never store it in a public repository.
Now I'll create a new project.
I'm going to use PyCharm, but you can
use whatever IDE you prefer.
I'll generate a new virtual environment for this project.
And then I'll install the Firebase Admin package.
I'll do it from the PyCharm preferences,
but, of course, you can run pip install from the terminal
if you prefer.
I'm going to create a new Python file.
Oh, and I'll add the service account file to my project.
Again, remember not to add it to your version control.
This would be a good time to add it to your gitignore.
Phew, all right.
Finally, let's get coding.
I can initialize the SDK using my downloaded service account
key file like this.
Note that if I were using the Admin SDK on the Google Cloud
Platform, I can initialize the app using the application
default credentials like this.
No matter how you initialized the app,
I'll then initialize an instance of Cloud Firestore
the same way, calling firestore.client.
Now that I've initialized the Admin SDK,
I'm ready to get a random quote and write it to the database.
But before I get coding, let's briefly examine the structure
of the quotes app.
At the top level, I start with a collection called sample data.
This collection contains just one document
called Inspiration.
This document will itself have two key value pairs,
or we can call them fields, one called Quote and another called
Author.
Now, as an aside, keep in mind I'm
not storing one quote per user.
Everybody in the world will be altering this one document.
If I were looking to save a different quote per user,
I'd probably set up a user's collection
and create a different document for each user.
And again, if you'd like to see how to build this app on iOS
or Android, check out Todd's Firecast
on getting started with Cloud Firestore.
Now I'm going to call a function I'm calling getQuote,
the result of which contains a JSON
object with a random quote.
The result of the function looks something like this.
All right, let's generate a quote
and then add it to Cloud Firestore.
First off I'm going to call the getQuote function, which
returns the response from the HTTP end point.
I'll get the quote and the author
from the body of the response.
I'll make a reference to the inspiration document
in the sample data collection.
I'll then call set on the document reference, which
sets the document's data to whatever data
I pass to the function.
If the document does not yet exist, it will be created.
Do keep in mind this replaces all data
in the document with the data that I just passed.
If I wanted to update some fields or add to the document,
I'd use the update instead of set function.
Once that work is complete, I'll log that the quote
was written to the database.
All right, let's see this code in action.
The console logs tell me that the new quote
was written successfully.
And we can see that quote here.
In the Firebase console, I can also see the new quote.
But I think it's cooler to watch the quote change from the app.
So here's the iOS version of the quotes
app with the current randomly generated quote.
When I run my Python app again, I
can see that the quote changed in the iOS app as well.
See?
Pretty cool.
My server-side code can now generate quotes.
Great.
That's sure to keep users motivated.
But I might also want to be able to read data
from Cloud Firestore server side,
say, if I wanted to sanitize bad words from quotes
or send notifications when new quotes are written.
So let's look at getting data from Firestore.
First I'll start with a reference
to the document holding the quote.
Then I'll get data from the inspiration doc
in the sample data collection using get.
I'll use try so I can create an exception for when
the document is not found.
Then if the document exists, I can use the document data
on whatever my app needs, such as sending a notification.
And there you have it.
Now you know how to use Cloud Firestore for Python
on your server.
I hope this inspires you to incorporate
Cloud Firestore and the Admin SDK
into your Firebase projects.
So be sure to subscribe to be the first to know
about new videos from Firebase, including
Firecasts like this one.
Thanks for watching, and I'll see you in a future episode.
[MUSIC PLAYING]