Check out a free preview of the full Firebase with React, v2 course:
The "Creating an API Endpoint" Lesson is part of the full, Firebase with React, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Steve creates an API endpoint using cloud functions to get all of the posts. The Firebase function logs are also briefly introduced.

Get Unlimited Access Now

Transcript from the "Creating an API Endpoint" Lesson

>> Steve Kinney: It would be cool, what if we made an API endpoint to get all of the posts, right? That would involve, it would be an HTTP request, but it would hook into Firestore, get all the posts, and send them back as JSON. That seems like a good first attempt in this case.

[00:00:17] So, let's, let's give that a shot.
>> Steve Kinney: We'll go back into index and we'll do exports.getAllPosts. This seems fine. And we'll do functions.https.onRequest(). And now we'll get the request and response objects. I wanna warn you now, I don't expect to make this mistake, but back I learn a lot of express, I would have this mystifying about we're nothing works to get the request response objects mixed up.

[00:00:54] Specially back that I use my Raz and wreck and never never works. So, someone's gonna be look out for that. So, let's go ahead and we'll get the snapshot. Now, I set the engine to node eight. So, I should be able to make this an async request. If not, we can write promises.

[00:01:12] So, we'll do snapshots and we'll do await. So, I need to pull in this admin library, right. And admin is simply gonna be const. admin equals require firebase admin. This a version of Firebase that has all the superpowers that the admin console has. You can just use this.

[00:01:35] You don't have to use this kind of functions. You can use this just as a script you run from your computer. Like have to just change a bunch of data cuz I have changed the way comments are formatted. You can run it. Should you do that, you need to go to the console and like get the secret key pair.

[00:01:50] Like the actual secret one, not the one that we had, that we just put in our client-side code. And then you can run those scripts. If you are running a Lambda function, you are already running on Google's infrastructure. and they know who you are, so you don't necessarily need to get those keys if you're doing it in a cloud function.

[00:02:07] So, we'll say admin, so you know how before we said firebase initialize app. Here, we'll say admin.initializeApp, and we'll say functions.config. This is where it's getting that secret key from. This will be passed by the environment when it's riding on Google servers. So, if you were to run this like on your own computer, this is not a thing that you're gonna get anything back from.

[00:02:33] But when it's running on Google servers, that will be populated with you know your secret key pair and stuff like that. Cool, and so, we'll actually say const firestore, very much like we were doing before, it was like admin.firestore. I could call that admin thing firebase if I really wanted to.

[00:02:53] We do need to do that const settings. We'll do firestore.settings
>> Steve Kinney: That was that warning we got in the very beginning of this. Cool, so now I should be able to do a firestore.collection('posts').get()
>> Steve Kinney: And with that, we'll say const posts =,, and this is if we wanted the IDs on there.

[00:03:36] We don't necessarily have to, we could just send them back.
>> Steve Kinney: Say id is
>> Steve Kinney: Single snapshot. And we'll just do a response. JSON is effectively the same API as Express. So, I response that JSON sends a response with the format application JSON. Cool, and then what we'll do is we'll just apply that.

[00:04:25] Up we go. Yep. Using node eight now so I can do a single way you do have to opt into that with that engines thing in the package.json. Otherwise you will get note six. I understand that. No, 10 is out and no 11 is the. And version and the node 12 is in route.

[00:04:42] I don't make the rules here, I just tell you that node six is the default, even though I think it was end of life at this point. But anyway, all right look so now we are updating, Hello World is not allowed to update there. And we are creating get all posts.

[00:05:04] Those are both going out, while it happens. Let's go ahead and let's go to the Firebase console. Let's check out functions. All right, here are my two functions, you can see them. You view the logs and their health. The logs are really interesting. They're good for debugging right and so if like especially for the ones that are triggered like, it should have been reading it should have been responding to a document change.

[00:05:34] Nothing happened. You can see like did it fire at all? Did it blow up, right? So, you can see function execute started, getting yelled at about billing. And then it was okay, and it finished status code at 200. These are obviously more interesting when they don't work. [LAUGH] Logs that everything went okay are not particularly interesting.

[00:05:55] But you can also see the different log levels is the errors and infos so on and so forth, cool? I was hoping that would make, it's smaller from or it's like when I click on it, but nope. Let's do again the post and see how it goes. [SOUND] Takes a little bit the first time, we'll be talking about this during the question earlier, where there's a kind of a cold start.

[00:06:27] If I do it again it's much faster. Like it is spinning up a virtual machine somewhere. And so here you can see these are All of the fun posts that are in the database sent as JSON. So now we have an API for our application. So just because Firebase is great for front engineers to have a back end really easily.

[00:06:52] But what if I need a back end. You can have a back end, that's fine. Just make a back end. Deploy it to other people's servers. You're good to go.