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-tools
Doesn'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.
- The cors npm package to allow Cross Origin AJAX requests on the endpoint.
Deploy to Firebase using the following command:
firebase deploy
This deploys and activates the date Function.
The first time you call
firebase deploy
on a new project with Functions will take longer than usual.
After deploying the function you can open the following URLs in your browser:
https://us-central1-<project-id>.cloudfunctions.net/date
https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20Do%20YYYY%2C%20h%3Amm%3Ass%20a
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"}' /
https://us-central1-<project-id>.cloudfunctions.net/date
Formatted 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"}' https://us-central1-<project-id>.cloudfunctions.net/date
We'd love that you contribute to the project. Before doing so please read our Contributor guide.
© Google, 2016. Licensed under an Apache-2 license.