---
source_url: https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/files
title: Send files
updated_at: 2026-06-16T12:48:09.310Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Send files

Attach files to messages to share information or improve collaboration.

Chat SDK supports multiple file attachments per message (unlike the JavaScript SDK). Each file must be 5 MB or less. Files upload sequentially, so consider implementing a progress indicator for better UX.

:::warning Requires File Sharing
Enable [File Sharing](https://youtu.be/6hnhZ8t1_CU) in the [Admin Portal](https://admin.pubnub.com/) and configure storage region and retention. Align file retention with [Message Persistence](https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/history) retention.
:::

## Send files

Attach files to a [draft message](https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/drafts) and publish using [send()](https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/drafts#send-a-draft-message).

### Method signature

##### Under the hood

The `sendText()` method calls PubNub Files API and the JavaScript SDK [sendFile() method](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#send-file) but extends its logic to let users send multiple files instead of just one. To do that, the file reaches the PubNub server after calling `sendFile()` - file data doesn't get stored in Message Persistence ([storeInHistory param](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#publishfileparams) is set intentionally to `false`), and PubNub only returns the ID and name of the file in response. Having those details, Chat SDK calls another JavaScript method [getFileUrl()](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#get-file-url) to construct URL based on the received info from the server. Finally, all collected data - file name, ID, URL, and ([MIME](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)) type of the file - get attached to the message as metadata ([TextMessageContent](https://www.pubnub.com/docs/chat/chat-sdk/learn/chat-entities/message)).

To add files, you must add elements to the `files` field (`[InputFile]`) of a [MessageDraft](https://www.pubnub.com/docs/chat/chat-sdk/learn/chat-entities/message-draft) object.

### Sample code

Attach two files of different formats to a text message.

```js
const isTypingIndicatorTriggered = channel.type !== 'public';

// Create a message draft
const messageDraft = channel.createMessageDraft({ isTypingIndicatorTriggered });

if (messageDraft) {
  // Update the text content of the draft
  messageDraft.update("Here's some important documents:");

  // Specify files to upload
  const textFileStream = {
    name: "txtFileStream.txt",
    type: "text/plain",
    source: {
      stream: new File(["content"], "filename.txt", { type: "text/plain" }),
      contentType: "text/plain",
      contentLength: 14578
    }
  };

  const pdfFileStream = {
    name: "pdfFileStream.pdf",
    type: "application/pdf",
    source: {
      stream: new File(["content"], "document.pdf", { type: "application/pdf" }),
      contentType: "application/pdf",
      contentLength: 12578
    }
  };

  // Attach files to the draft message
  messageDraft.files = [textFileStream, pdfFileStream];

  // Send the draft message
  messageDraft.send().then(timetoken => {
    console.log(`Message sent successfully with timetoken: ${timetoken}`);
  }).catch(error => {
    console.error(`Failed to send message:`, error);
  });
}
```

## Get all message files

`files` returns all files attached to a message.

### Method signature

This method has the following signature:

```ts
message.files: {
    name: string;
    id: string;
    url: string;
    type?: string;
}[]
```

#### Properties

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| object | array | Optional |  | List of key-value pairs for specific file parameters. |
| > name | string | Optional |  | Name of the file, like `error-1.jpg`. |
| > id | string | Optional |  | Unique identifier assigned to the file by PubNub, like `736499374`. |
| > url | string | Optional |  | File's direct downloadable URL, like `https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg`. |
| > type? | string | Optional |  | [(MIME](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)) type of the file, like `image/jpeg`. |

### Sample code

List all files attached to the last message on the `support` channel.

```ts
// reference the "support" channel with the message you want to check
const channel = await chat.getChannel("support")

// reference the last message on that "support" channel
const { messages } = await channel.getHistory({count: 1})
const message = messages[0]

// list all files added to the message
message.files
```

## Get all channel files

`getFiles()` returns all files attached to messages on a channel.

##### Under the hood

`getFiles()` calls Files API through [listFiles()](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#list-files) and [getFileUrl()](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#get-file-url) methods. Thanks to combining these two methods in one, `getFiles()` returns more file details - not only file names and IDs (like `listFiles()`) but also file URLs (`getFileUrl()`).

### Method signature

This method takes the following parameters:

```ts
channel.getFiles({
    limit?: number;
    next?: string;
}): Promise<{
    files: {
        name: string;
        id: string;
        url: string;
    }[];
    next: string;
    total: number;
}>
```

#### Input

| Parameter | Description |
| --- | --- |
| `limit`Type: `number`Default: `100` | Number of files to return. |
| `next`Type: `string`Default: n/a | String token to get the next batch of files. |

#### Output

| Parameter | Description |
| --- | --- |
| `Promise<>`Type: `object` | Returned object containing these fields: `files`, `next`, and `total`. |
| `> files`Type: `object` | Array containing file details. |
| `>> name`Type: `string` | Name of the file, like `error-1.jpg`. |
| `>> id`Type: `string` | Unique identifier assigned to the file by PubNub, like `736499374`. |
| `>> url`Type: `string` | File's direct downloadable URL, like `https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg`. |
| `> next`Type: `string` | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
| `> total`Type: `number` | Total number of files. |

### Sample code

List all files published on the `support` channel.

```ts
// reference the "support" channel
const channel = await chat.getChannel("support")
// list all files on that channel
await channel.getFiles()
```

## Delete files

`deleteFile()` removes files from PubNub storage.

:::note Endpoint limitation
After deleting a file, you cannot identify which historical messages contained it.
:::

##### Under the hood

`deleteFile()` calls PubNub Files API and the JavaScript SDK [deleteFile()](https://www.pubnub.com/docs/sdks/javascript/api-reference/files#delete-file) method.

### Method signature

This method takes the following parameters:

```ts
channel.deleteFile({
    id: string;
    name: string;
}): Promise<{ status: number }>
```

#### Input

| Parameter | Description |
| --- | --- |
| `id` *Type: `string`Default: n/a | Unique identifier assigned to the file by PubNub. |
| `name` *Type: `string`Default: n/a | Name of the file. |

#### Output

| Type | Description |
| --- | --- |
| `Promise<{ status: number }>` | Status code of the response. |

### Sample code

Remove a file named `error-screenshot.png` from the `support` channel.

```ts
// reference the "support" channel
const channel = await chat.getChannel("support")
// remove the file
channel.deleteFile({
    id: "18345892973",
    name: "error-screenshot.png"
})
```