---
hide_table_of_contents: true
---

import CodeBlock from "@theme/CodeBlock";

# Firestore Chat Memory

For longer-term persistence across chat sessions, you can swap out the default in-memory `chatHistory` that backs chat memory classes like `BufferMemory` for a firestore.

## Setup

First, install the Firebase admin package in your project:

```bash npm2yarn
yarn add firebase-admin
```

Go to your the Settings icon Project settings in the Firebase console.
In the Your apps card, select the nickname of the app for which you need a config object.
Select Config from the Firebase SDK snippet pane.
Copy the config object snippet, then add it to your firebase functions FirestoreChatMessageHistory.

## Usage

import Example from "@examples/memory/firestore.ts";

<CodeBlock language="typescript">{Example}</CodeBlock>

## Firestore Rules

If your collection name is "chathistory," you can configure Firestore rules as follows.

```
      match /chathistory/{sessionId} {
       allow read: if request.auth.uid == resource.data.createdBy;
       allow write: if request.auth.uid == request.resource.data.createdBy;
			 }
			 match /chathistory/{sessionId}/messages/{messageId} {
       allow read: if request.auth.uid == resource.data.createdBy;
       allow write: if request.auth.uid == request.resource.data.createdBy;
		    }
```
