This quickstart demonstrates using the Firebase SDK for Cloud Functions with an HTTPS trigger through building an endpoint returning the current time.
The function date returns the current server date. You can pass it a format URL Query parameter to format the date.
Further reading:
Clone or download this repo and open the quickstarts/time-server directory.
Create a Firebase Project on the Firebase Console.
Set up your Firebase project by running firebase use --add, select your Project ID and follow the instructions.
You need to have installed the Firebase CLI. If you haven't run:
npm install -g firebase-toolsDoesn't work? You may need to change npm permissions.
First you need to install the npm dependencies of the functions:
cd functions && npm install; cd ..This installs locally:
- The Firebase SDK and the Firebase Functions SDK.
- The moment npm package to format time.
Deploy to Firebase using the following command:
firebase deployThis deploys and activates the date Function.
The first time you call
firebase deployon a new project with Functions will take longer than usual.
Alteratively, you can call firebase emulators:start to test the functions on the local emulator suite.
After deploying the function, check the CLI's output to see the URL for your function.
It will look something like: https://<function-name>-<random-hash>-<region>.a.run.app
You can also send the format in the request body. For instance using cURL in the command line:
curl -H 'Content-Type: application/json' /
-d '{"format": "MMMM Do YYYY, h:mm:ss a"}' /
<function url>/dateFormatted dates should be displayed.
We are responding with a 403 error in case of PUT requests:
curl -X PUT -d '{"format": "MMMM Do YYYY, h:mm:ss a"}' <function-url>/dateWe'd love that you contribute to the project. Before doing so please read our Contributor guide.
© Google, 2022. Licensed under an Apache-2 license.