Skip to main content

Overview

Baileys provides utilities to download media (images, videos, audio, documents) from received messages. Media is stored encrypted on WhatsApp’s servers and must be downloaded and decrypted.

Basic Media Download

Download media from a message:

Download as Stream

For large files, use stream to avoid loading everything into memory:

Check Message Type

Before downloading, check if the message contains media:

Download Options

Control the download behavior:

Media Reupload

WhatsApp automatically removes old media from servers. If a download fails, request a reupload:

Manual Reupload Request

Download from Content

Directly download from message content:

Media Types

Supported media types for download:

Get File Extension

Determine the correct file extension:

Complete Download Example

Full example with type checking, error handling, and file saving:

Stream Processing Example

Process large media files without loading into memory:

Thumbnail Download

Some messages have thumbnails that can be extracted:

Media Message Properties

Useful properties available on media messages:

Direct Download from URL

If you have the direct path and media key:

Error Handling

Best Practices

  1. Use Streams: For files larger than 10MB, use stream mode to avoid memory issues
  2. Type Checking: Always check message type before downloading
  3. Error Handling: Implement retry logic for failed downloads
  4. Reupload Support: Always provide reuploadRequest function
  5. File Extensions: Use extensionForMediaMessage() for correct extensions
  6. Cleanup: Remove downloaded files when no longer needed

Performance Tips

Next Steps

Media Messages

Learn how to send media messages

Handling Events

Handle incoming messages