---
url: https://talkjs.com/docs/Guides/Store_Messages/
title: "Store a copy of messages | Guides | TalkJS Docs"
---

For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

# Store a copy of messages

Save message events in your own database.

TalkJS provides a REST API and real-time access to messages, but bulk analysis is easier with your own local copy. You can use webhooks to keep a copy of your TalkJS messages in your own database, updated in real time. This example uses NodeJS and PostgreSQL.

## Save message events

TalkJS sends events when messages are sent, edited or deleted. Store each event as a row, using its event ID as the primary key:

```sql
CREATE TABLE talkjs_events (
  id text PRIMARY KEY,
  event_type text NOT NULL,
  conversation_id text,
  payload jsonb NOT NULL,
  received_at timestamptz NOT NULL DEFAULT now()
);
```

Add a POST endpoint to your app that inserts the event, then returns `200 OK`. For example, with Express and `pg`:

```javascript
import express from 'express';
import { Pool } from 'pg';

const app = express();
const db = new Pool({ connectionString: process.env.DATABASE_URL });

// Register this route before any express.json() middleware.
app.post('/webhooks/talkjs', express.raw({ type: 'application/json' }), async (req, res) => {
  // Optional: Verify that the webhook actually came from TalkJS.
  // https://talkjs.com/docs/Features/Security/#verify-webhook-integrity
  if (!verifyTalkJSSignature(req)) {
    res.sendStatus(401);
    return;
  }

  try {
    const event = JSON.parse(req.body.toString('utf8'));

    await db.query(
      `INSERT INTO talkjs_events (id, event_type, conversation_id, payload)
       VALUES ($1, $2, $3, $4)
       ON CONFLICT (id) DO NOTHING`,
      [event.id, event.type, event.data.conversation?.id || null, JSON.stringify(event)]
    );

    res.sendStatus(200);
  } catch {
    res.sendStatus(500);
  }
});

app.listen(process.env.PORT || 3000);
```

The endpoint returns `200 OK` only after the insert succeeds. If the database write fails, it returns `500` so TalkJS can retry. The primary key and `ON CONFLICT` prevent repeated deliveries from adding duplicate rows.

In your app’s **Settings** in the TalkJS dashboard, open **Webhooks → Add new webhook** and enter the public URL of this endpoint, such as `https://example.com/webhooks/talkjs`. Under **Select Events to Subscribe**, select **message.sent**, **message.updated** and **message.deleted**, then click **Save**.

Send a chat message. You can now read its event from `talkjs_events`, alongside subsequent message edits and deletions. For example, to find the events for conversation `booking_1042`:

```sql
SELECT event_type, payload
FROM talkjs_events
WHERE conversation_id = 'booking_1042'
ORDER BY received_at;
```

See the [webhook event reference](https://talkjs.com/docs/Webhooks/#webhook-events) for the fields in each payload.

## Receive webhooks when your server is down

If your application is offline during an update or maintenance, TalkJS tries to deliver each event up to three times before giving up. If losing events during that time is a concern, see [Setting up a webhook relay](https://talkjs.com/docs/Guides/Webhook_Relay/) for how to keep receiving events on a separate server while your application is offline.
