---
title: "Save Messages | Micdrop"
description: "Capture and store conversation messages by listening to the Agent \"Message\" event for analytics, logging, and conversation history."
url: "https://micdrop.dev/docs/server/save-messages"
---

*   [Getting Started](/docs/getting-started)
*   [Client (Browser)](/docs/client)
    
    *   [Installation](/docs/client/installation)
    *   [React Hooks](/docs/client/react-hooks)
    *   [Start/Stop Call](/docs/client/start-stop-call)
    *   [Pause/Resume Call](/docs/client/pause-resume-call)
    *   [Mute/Unmute Call](/docs/client/mute-unmute-call)
    *   [Call State](/docs/client/call-state)
    *   [Display Conversation Messages](/docs/client/display-conversation-messages)
    *   [Handling Tool Calls](/docs/client/handling-tool-calls)
    *   [Device Management](/docs/client/devices-management)
    *   [Voice Activity Detection (VAD)](/docs/client/vad)
    *   [Error Handling](/docs/client/error-handling)
    *   Utility Classes
        
        *   [Mic](/docs/client/utility-classes/mic)
        *   [MicdropClient](/docs/client/utility-classes/micdrop-client)
        *   [MicRecorder](/docs/client/utility-classes/mic-recorder)
        *   [Speaker](/docs/client/utility-classes/speaker)
        
    
*   [Server (Node.js)](/docs/server)
    
    *   [Installation](/docs/server/installation)
    *   [With Fastify](/docs/server/with-fastify)
    *   [With NestJS](/docs/server/with-nestjs)
    *   [Auth and Parameters](/docs/server/auth-and-parameters)
    *   [First Message](/docs/server/first-message)
    *   [Save Messages](/docs/server/save-messages)
    *   [Resume a Conversation](/docs/server/resume-conversation)
    *   [Recording Audio](/docs/server/recording-audio)
    *   [Error Handling](/docs/server/error-handling)
    *   [Tools](/docs/server/tools)
    *   [Extract Value from Answer](/docs/server/extract)
    *   [Auto End Call](/docs/server/auto-end-call)
    *   [Semantic Turn Detection](/docs/server/semantic-turn-detection)
    *   [Noise Filtering](/docs/server/noise-filtering)
    *   [Micdrop Protocol](/docs/server/protocol)
    
*   [AI Integrations](/docs/ai-integration)
    
    *   Provided Integrations
        
        *   [AI SDK](/docs/ai-integration/provided-integrations/ai-sdk)
        *   [Cartesia](/docs/ai-integration/provided-integrations/cartesia)
        *   [ElevenLabs](/docs/ai-integration/provided-integrations/elevenlabs)
        *   [Gladia](/docs/ai-integration/provided-integrations/gladia)
        *   [Gradium](/docs/ai-integration/provided-integrations/gradium)
        *   [Mistral](/docs/ai-integration/provided-integrations/mistral)
        *   [OpenAI](/docs/ai-integration/provided-integrations/openai)
        
    *   Custom Integrations
        
        *   [Agent (LLM)](/docs/ai-integration/custom-integrations/custom-agent)
        *   [Speech-to-Text (STT)](/docs/ai-integration/custom-integrations/custom-stt)
        *   [Text-to-Speech (TTS)](/docs/ai-integration/custom-integrations/custom-tts)
        
    *   Fallback Strategies
        
        *   [FallbackAgent](/docs/ai-integration/fallback-strategies/agent-fallback)
        *   [FallbackSTT](/docs/ai-integration/fallback-strategies/stt-fallback)
        *   [FallbackTTS](/docs/ai-integration/fallback-strategies/tts-fallback)
        
    *   [IA Vocale Souveraine 🇫🇷🇪🇺](/docs/ai-integration/sovereign-voice-ai)
    

[Micdrop](/) › [Documentation](/docs/getting-started)

# Save Messages

Capture and store conversation messages by listening to the Agent “Message” event for analytics, logging, and conversation history.

## Basic Message Logging

Listen for conversation messages:

```
const agent = new OpenaiAgent({  apiKey: process.env.OPENAI_API_KEY,  systemPrompt: 'You are a helpful assistant',})
// Listen for all conversation messagesagent.on('Message', (message) => {  console.log('New message:', {    role: message.role, // 'user' or 'assistant'    content: message.content, // The message text    timestamp: new Date().toISOString(),  })})
new MicdropServer(socket, { agent, tts })
```

## Save Messages to Database

Store messages in your database:

```
import { db } from './database' // Your database connection
agent.on('Message', async (message) => {  try {    await db.conversations.create({      userId: currentUserId,      sessionId: currentSessionId,      role: message.role,      content: message.content,      timestamp: new Date(),    })  } catch (error) {    console.error('Failed to save message:', error)  }})
```

## Save Conversation when Call ends

Save the complete conversation when the call ends by listening to the `End` event:

```
import { MicdropServer } from '@micdrop/server'
const server = new MicdropServer(socket, {  agent,  stt,  tts,})
server.on('End', async (call) => {  // Save conversation  await db.conversations.create({    userId: currentUserId,    sessionId: currentSessionId,    messages: call.conversation,    endedAt: new Date(),    duration: call.duration,    totalMessages: call.conversation.length,  })})
```

> ⚠️ **Warning**: In case of server errors or crashes, the conversation may not be saved using this approach. For critical applications, it’s recommended to save messages individually as they arrive (see [Save to Database](#save-messages-to-database) section above) to ensure no messages are lost.

[Previous← First Message](/docs/server/first-message)[NextResume a Conversation →](/docs/server/resume-conversation)

On this page

*   [Basic Message Logging](#basic-message-logging)
*   [Save Messages to Database](#save-messages-to-database)
*   [Save Conversation when Call ends](#save-conversation-when-call-ends)
