# Firebase Database Action

Use this action to insert, update, delete or retrieve one or more Firebase cloud documents from your Firebase account.

![](https://2532694803-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdPD9c4fJbXSAZcPHt9%2F-MROd23nLZYG6jGSbIly%2F-MROhpaZ4GksHUKuFWtN%2Fimage.png?alt=media\&token=17b4a99d-6bfa-427c-b8e5-50409af39c40)

### Settings

The action uses the following settings:

| Setting                 | Description                                                                                                                                        | [Parsed](https://docs.xenioo.com/actions-and-operations/dynamic-parsing) |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| Firebase Account JSON   | The JSON account token that you can retrieve from your [Firebase](https://www.xenioo.com/en/using-firebase-cloud-data-with-your-chatbot/) account. | :white\_check\_mark:                                                     |
| Action                  | The type of action you want to execute on the database.                                                                                            | :no\_entry:                                                              |
| Project Id              | The project id of your database.                                                                                                                   | :white\_check\_mark:                                                     |
| Database Name           | The name of your database. If you have only one database, you can leave (default) as name.                                                         | :white\_check\_mark:                                                     |
| Collection Name         | The name of the database collection you want to target with your command.                                                                          | :white\_check\_mark:                                                     |
| Document Id             | The Id of the document you want to target with your action.                                                                                        | :white\_check\_mark:                                                     |
| Document Payload        | The full JSON payload of your document.                                                                                                            | :white\_check\_mark:                                                     |
| Structured Query Filter | The full structured query payload                                                                                                                  | :white\_check\_mark:                                                     |
| JSON Token Path         | The JSON path of the data you want to extract for the Firebase reply.                                                                              | :white\_check\_mark:                                                     |
| Target Variable         | The name of the variable that will hold the result of the action executed.                                                                         | :white\_check\_mark:                                                     |

### Remarks

When requesting or uploading a full Firebase document, Xenioo will automatically translate each model into a simpler representation that will both speed up parsing and simplify representation.

As an example, let's have a look at the following Firebase Firebase document:

```javascript
{
    "name":"projects/somedb-14cf1/databases/(default)/documents/users/319daACFaz",
    "fields"{
        "full_name":{
            "stringValue":"Gian"
        },
        "age":{
            "integerValue":"48"
        }
    }
}
```

As you can see it's very complete, but also quite verbose and not very handy for quick variables reading. When accessing this document, Xenioo will automatically shrink the structure to a more concise version:

```javascript
{
    "Id":"319daACFaz",
    "full_name":"Gian",
    "age":48
}
```

&#x20;Note two things: first, the document id has been reduced to represent only the actual document final unique value. Second, the *age* field has been automatically translated in the projected model to the correct type, representing a number.

The same translation happens also in reverse. When using the second model to update a Firebase document, Xenioo will transform back the model to the Firebase format. Note that this is not mandatory: the Firebase Action supports both the default, Firebase native format and the Xenioo translated one.

When using QUERY mode, a [full structured query](https://firebase.google.com/docs/firestore/reference/rest/v1/StructuredQuery#Filter) can be specified inside the Query Payload parameter. As an example, searching for a document where the field "FirstName" is equal to "Mary" would require a query like this:

```javascript
where:{
    fieldFilter:{
        field:{
            fieldPath:"FirstName"
        },
        op:"EQUAL",
        value:{
            stringValue:"Mary"
        }
    }
}
```

All of the actions that can be executed by this action can also be executed directly by code, using [specific methods](https://docs.xenioo.com/actions-and-operations/integration/xenioo.bots.actions.base.executescriptaction/firebase-connection-methods) available in Xenioo [Cloud Scripting Action](https://docs.xenioo.com/actions-and-operations/integration/xenioo.bots.actions.base.executescriptaction).

{% hint style="warning" %}
To avoid overload, when no filter is active, the action **will not** query all the records available in a given collection. Only the first 20 fields will be extracted. Do not use the LIST mode to access the full list of records and then search manually. Use a structure
{% endhint %}

### Trigger

This action does not trigger any event.

### Variables

Beside the value set to the selected *Target Variable* setting, this action is producing the following variables:

| Variable                     | Description                                                                             |
| ---------------------------- | --------------------------------------------------------------------------------------- |
| firebase\_list\_empty        | Set to true if the last command returned an empty list or false if any record was found |
| firebase\_last\_document\_id | Contains the last document id returned by  an UPDATE or CREATE database command         |

### Availability

Channel specific availability follows the table below:

| Channel          | Availability     |
| ---------------- | ---------------- |
| Facebook         | Fully Available. |
| WhatsApp         | Fully Available. |
| Telegram         | Fully Available. |
| Web              | Fully Available. |
| Alexa            | Fully Available. |
| Google Assistant | Fully Available. |
| Slack            | Fully Available. |
| API              | Fully Available. |

This action can be used only inside interactions and not as child of other actions. This action requires a [Premium Account](https://docs.xenioo.com/basic-concepts/your-account/upgrading-from-free) to be used on a live chatbot.

### Further Reading

{% embed url="<https://www.xenioo.com/en/using-firebase-cloud-data-with-your-chatbot/>" %}

{% embed url="<https://firebase.google.com/docs/firestore/reference/rest/v1/StructuredQuery#Filter>" %}
