Firebase is awesome. Let’s just start with that. Super cheap, super easy to set up and use, and loads of functionality offered. That being said, let’s look at how to create cloud functions for Firebase in TypeScript.
What are Cloud Functions?
Let’s Get Started
Assuming you already have a Firebase account created with an App on it (if you don’t already, it’s super easy to create one, so I won’t go through it for the purposes of this post), open up a new terminal, navigate to whatever directory you want your firebase cloud functions to live in (or create a new one [
mkdir cloud-functions]) and type
firebase login. If you get an error that firebase isn’t a recognized cmdlet or something of that nature, just run
npm install -g firebase. If you had to run the install command, either restart your terminal, or if you are using Powershell or MS Command Line just run
refreshenv and run the
firebase login again. You’ll be directed to your web browser to log in to your Firebase console. Login, come back to your terminal and run
firebase init. You’ll get a prompt saying “You’re about to initialize a Firebase project in this directory:
You’ll be prompted with 1 final question - Do you want to install dependencies with npm now? Type
Y, as we’ll need to install those dependencies eventually, so it might as well be now!
Write & Deploy
Once your firebase project has been initialized, open it up in your code editor of choice and look at the default TypeScript code. It’s commented out, but it shows you a good example of a helloWorld cloud function, shown below:
Uncomment that out, and let’s send it up to our project. Note that this is how you create functions. If you want a function that will add two numbers, you can do that like so:
Now, let’s send these up to the firebase app. Open up your terminal and type
firebase deploy. Once they’re deployed, you can see them in the “Cloud Functions” section of your Firebase console. Then you can call them from your browswer or ping them however you prefer. Cloud functions are super flexible and I would highly recommend trying them out.