> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Firebase Realtime Database

> Client-mode REST API connectors for Firebase: write device state, append events, and query history in the Realtime Database, and upsert, merge, and query Cloud Firestore documents from MQTT

Technical Preview

## Overview

Client-mode REST API connectors for Firebase: write device state, append events, and query history in the Realtime Database, and upsert, merge, and query Cloud Firestore documents from MQTT.

This is one of the templates in the Firebase Realtime Database and Cloud Firestore REST API app (`firebase-realtime-database-and-cloud-firestore`). Deploy it alongside the other connectors of that app when they depend on each other (for example a token connector plus an API connector). See [Firebase Realtime Database and Cloud Firestore](/latest/lot-language/routes/rest-api-apps/firebase-realtime-database-and-cloud-firestore) for the package overview.

> [!NOTE]
>
> Not yet verified against the live vendor API: Needs a lab Firebase project: project id, RTDB instance URL (https\://\<db>-default-rtdb.firebaseio.com or \<region>.firebasedatabase.app), and a Google OAuth2 access token from a service account (scopes userinfo.email + firebase.database for RTDB; roles/datastore.user with datastore or cloud-platform scope for Firestore), Hub secret FIREBASE\_ACCESS\_TOKEN. Gate: RTDB PUT devices/press-01/state.json 200 + POST events.json 200 + GET 200; Firestore PATCH documents/devices/press-01 200 + runQuery 200. No Firebase/Google credentials on the factory box. Emulator smoke 13/13 (11 events + 2 rules denials) and real-endpoint probes (public RTDB GET 200; writes 401; Firestore invalid token 401) are partial evidence only.

## Auth

Bearer \{value.json.access\_token} on both connectors. RTDB: Google OAuth2 SA token (userinfo.email + firebase.database); alt ?access\_token= or Firebase ID token via ?auth=. Firestore: Google OAuth2 SA token (roles/datastore.user) or Firebase ID token (rules apply). Hub secret FIREBASE\_ACCESS\_TOKEN.

Auth headers used by the template: `Authorization: Bearer {value.json.access_token}`.

## Template parameters

| Placeholder | Type | Description | Required |
| - | - | - | - |
| `RouteName` | string | Name of the Firebase Realtime Database connector | no |
| `BaseUrl` | string | Firebase Realtime Database base URL without trailing slash (e.g. [https://YOUR\_DATABASE-default-rtdb.firebaseio.com](https://YOUR_DATABASE-default-rtdb.firebaseio.com)) | yes |

`BASE_ADDRESS` default: `https://YOUR_DATABASE-default-rtdb.firebaseio.com`.

## Events

| Event | Method | Endpoint | Request topic | Response topic | Payload fields |
| - | - | - | - | - | - |
| `SetDeviceState` | PUT | `/{value.json.path}.json` | `data/firebase/rtdb/state/set` | `data/firebase/rtdb/state/set/response` | `access_token`, `data`, `path` |
| `AppendEvent` | POST | `/{value.json.path}.json` | `data/firebase/rtdb/event/append` | `data/firebase/rtdb/event/append/response` | `access_token`, `data`, `path` |
| `UpdateFields` | PATCH | `/{value.json.path}.json` | `data/firebase/rtdb/fields/update` | `data/firebase/rtdb/fields/update/response` | `access_token`, `data`, `path` |
| `ReadPath` | GET | `/{value.json.path}.json` | `data/firebase/rtdb/path/get` | `data/firebase/rtdb/path/get/response` | `access_token`, `path` |
| `QueryLatest` | GET | `/{value.json.path}.json?orderBy={value.json.order_by}&limitToLast={value.json.limit}` | `data/firebase/rtdb/path/query` | `data/firebase/rtdb/path/query/response` | `access_token`, `limit`, `order_by`, `path` |

## Deploy

Get the template card (JSON with `template` and `metadata`) from the broker, then deploy the connector:

```bash theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
mosquitto_pub -t '$SYS/Coreflux/Command' -m "-routeCode FIREBASE_REALTIME_DATABASE"
```

The deployable connector from the REST API app, with `BASE_ADDRESS` already filled in:

```lot theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE FirebaseRealtimeDatabaseRestApi WITH TYPE REST_API
    ADD REST_API_CONFIG
        WITH BASE_ADDRESS "https://YOUR_DATABASE-default-rtdb.firebaseio.com"
        WITH ENABLE_CLIENT "true"
        WITH USE_SSL "true"
        WITH REQUEST_TIMEOUT '60'
        WITH DEFAULT_CONTENT_TYPE "application/json"
    ADD EVENT SetDeviceState
        WITH SOURCE_TOPIC "data/firebase/rtdb/state/set"
        WITH DESTINATION_TOPIC "data/firebase/rtdb/state/set/response"
        WITH METHOD "PUT"
        WITH ENDPOINT "/{value.json.path}.json"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "Authorization" "Bearer {value.json.access_token}"
        WITH BODY "{value.json.data}"
    ADD EVENT AppendEvent
        WITH SOURCE_TOPIC "data/firebase/rtdb/event/append"
        WITH DESTINATION_TOPIC "data/firebase/rtdb/event/append/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/{value.json.path}.json"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "Authorization" "Bearer {value.json.access_token}"
        WITH BODY "{value.json.data}"
    ADD EVENT UpdateFields
        WITH SOURCE_TOPIC "data/firebase/rtdb/fields/update"
        WITH DESTINATION_TOPIC "data/firebase/rtdb/fields/update/response"
        WITH METHOD "PATCH"
        WITH ENDPOINT "/{value.json.path}.json"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "Authorization" "Bearer {value.json.access_token}"
        WITH BODY "{value.json.data}"
    ADD EVENT ReadPath
        WITH SOURCE_TOPIC "data/firebase/rtdb/path/get"
        WITH DESTINATION_TOPIC "data/firebase/rtdb/path/get/response"
        WITH METHOD "GET"
        WITH ENDPOINT "/{value.json.path}.json"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Authorization" "Bearer {value.json.access_token}"
    ADD EVENT QueryLatest
        WITH SOURCE_TOPIC "data/firebase/rtdb/path/query"
        WITH DESTINATION_TOPIC "data/firebase/rtdb/path/query/response"
        WITH METHOD "GET"
        WITH ENDPOINT "/{value.json.path}.json?orderBy={value.json.order_by}&limitToLast={value.json.limit}"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Authorization" "Bearer {value.json.access_token}"
```

Deploy it with `-addRoute` (do not include `ADD METADATA`).

## Usage example

Call `SetDeviceState` by publishing a JSON payload to its request topic, and read the reply on the response topic:

```bash theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
mosquitto_sub -t 'data/firebase/rtdb/state/set/response' &
mosquitto_pub -t 'data/firebase/rtdb/state/set' -m '{"access_token": "<access_token>", "data": "<data>", "path": "<path>"}'
```

## Related

* [REST API Connectors](/latest/lot-language/routes/rest-api-routes)
* [REST API Apps catalog](/latest/lot-language/routes/rest-api-apps)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.