You have an app and want to send data (e.g., events, logs, etc.) to an append-only database. You don't want to integrate any database connectors or other libraries into the app; you just want to make an HTTPS request and forget about it.
This is the code for a Firebase project that provides an integrated solution to this problem. Specifically, it defines:
- an HTTP-triggered function providing endpoint you can query with your data
- the database (i.e., the configuration for it) where the data gets stored
- a hosted webpage that allows quick access to the data
- a script that lets you download the data (and that you can use a starting point for more complicated data processing)
With Firebase, you have the option of the function running under a custom domain name that you bring in.
To store data, make a POST request to https://your_project_url/save
with the following fields:
value
, the data you want to storekey
, the key you want to use to identify this data (it need not be unique). For example, this could be the type of the event you're logging.uid
, an identifier for the user associated with this eventclient
, an identifier for the client issuing this request (e.g., the app version)
These fields are required, but the data types are not enforced. Especially useful for value
, they can be nested JSON objects.
Anyone can write to the database. Requests are neither authenticated nor validated. It is assumed that you have an ex post facto way of identifying valid requests.
To see your data on a webpage, go to https://your_project_url/get
. You will need to authenticate with a whitelisted email (see Setup below).
A Python script for printing the contents of the database is provided in the client
directory.
To run it, you will need to:
- Install the
firebase-admin
dependency into your Python environment. (A Pipfile is provided for your convenience.) - Create a service account and obtain its JSON key file
- Place the key file in the
client
directory askey.json
(or, conversely, update the script with the path to the file)
Here are the instructions for setting up your own instance of this application.
- Clone this repo
- Modify
firestore.rules
to whitelist the email address(es) you want to be able to access the data through a webpage. Authentication will be done (as with everything else) by Firebase. You can also skip this step and only use the command-line client (see above). - In the top level of the repo, run
npm install
to getfirebase-tools
- Run
npx firebase login
to log in - Go to the Firebase console and Add project
- Once you've created the project, go to the Authentication product dashboard, click on Sign-in method, and enable the Email/Password option. (I also use the Email link option.)
- Go to the Database dashboard and provision a database.
- Run
npx firebase use --add
and select the project you just created. (default
is a reasonable choice for the alias, when you're prompted for it.) - Go into the
functions
directory and runnpm install
there - Back in the root of the repo, run
npx firebase deploy
.
When the deploy is finished, the command will display the "Hosting URL." This is URL to use for storing and retrieving data (just remember to append /save
or /get
).
If you'd like to use your own domain for the URL (rather than the one provided by Firebase), you can follow these steps:
- In the Firebase console, select your project, and go to the Hosting dashboard.
- Click the Connect domain button and follow the instructions. You'll need to update the DNS settings of your domain.
- Go to the Authentication product dashboard, click on Sign-in method, and scroll down to Authorized domains. Add the (sub)domain you want to use.
- After some time, you should be able to use your domain instead of the Firebase subdomain.
(These instructions were current at the time of writing, but the UI may have changed in the meantime.)