---
source_url: https://www.pubnub.com/docs/sdks/python/api-reference/message-actions
title: Message Actions API for Python SDK
updated_at: 2026-11-09T13:29:01.000Z
sdk_name: PubNub Python SDK
sdk_version: 10.8.1
description: Learn to add, remove, and get message actions like reactions using Python SDK with PubNub Docs.
---

# Message Actions API for Python SDK

PubNub Python SDK, use the latest version: 10.8.1

Install:

```bash
pip install pubnub==10.8.1
```

> For AI agents: documentation index at https://www.pubnub.com/llms-full.txt

Use message actions to add or remove metadata on published messages. Common uses include receipts and reactions. Clients subscribe to a channel to receive message action events. Clients can also fetch past message actions from Message Persistence, either on demand or when fetching original messages.

:::note Request execution and return values
You can decide whether to perform the Python SDK operations synchronously or asynchronously.
* `.sync()` returns an `Envelope` object, which has two fields: `Envelope.result`, whose type differs for each API, and `Envelope.status` of type `PnStatus`.
   ```python
   pubnub.publish() \
       .channel("myChannel") \
       .message("Hello from PubNub Python SDK") \
       .sync()
   ```
* `.pn_async(callback)` returns `None` and passes the values of `Envelope.result` and `Envelope.status` to a callback you must define beforehand.
   ```python
   def my_callback_function(result, status):
       print(f'TT: {result.timetoken}, status: {status.category.name}')

   pubnub.publish() \
       .channel("myChannel") \
       .message("Hello from PubNub Python SDK") \
       .pn_async(my_callback_function)
   ```
:::

:::tip Reactions
"Message Reactions" is a specific application of the Message Actions API for emoji or social reactions.
:::

:::note Message Actions vs. Message Reactions
**Message Actions** is the flexible, low-level API for adding any metadata to messages (read receipts, delivery confirmations, custom data), while **Message Reactions** specifically refers to using Message Actions for emoji/social reactions.
In PubNub [Core](https://www.pubnub.com/docs/sdks.md) and [Chat](https://www.pubnub.com/docs/chat/overview.md) SDKs, the same underlying Message Actions API is referred to as **Message Reactions** when used for emoji reactions - it's the same functionality, just different terminology depending on the use case.
:::

## Add message action

:::warning Requires Message Persistence
This method requires that Message Persistence is [enabled](https://www.pubnub.com/docs/architecture/authentication/set-up-your-account.md#enable-the-features-your-app-calls) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

Add an action to a published message. The response includes the added action.

### Method(s)

Use this Python method:

```python
pubnub.add_message_action() \
    .channel(String) \
    .message_action(PNMessageAction) \
    .pn_async(Function message_action_callback)
```

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| channel | String | Yes | Channel name to add the message action to. |
| message_action | PNMessageAction | Yes | Message action payload. |
| message_action.type | String | Yes | Message action type. |
| message_action.value | String | Yes | Message action value. |
| message_action.message_timetoken | Integer | Yes | Timetoken of the target message. |
| message_action_callback | Function | Yes | Callback for success or error. Details in [PNMessageAction callback](#pnmessageaction). |

### Sample code

#### Builder Pattern

```python
import os
import time
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.models.consumer.message_actions import PNMessageAction

def add_message_action(pubnub: PubNub):
    msg_action = PNMessageAction()
    msg_action.type = "reaction"
    msg_action.value = "smiley_face"
    msg_action.message_timetoken = str(int(time.time()))

    result = pubnub.add_message_action() \
        .channel("chats.room1") \
        .message_action(msg_action) \
        .sync()

    if result.status.is_error():
        print(f"Error: {result.status.error_data}")
    else:
        print("Message action added successfully.")
        print(f"Response: {result.result.__dict__}")

def main():
    # Configuration for PubNub instance
    pn_config = PNConfiguration()
    pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo')
    pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')
    pn_config.user_id = os.getenv('USER_ID', 'my_custom_user_id')

    # Initialize PubNub client
    pubnub = PubNub(pn_config)

    # Add message action
    add_message_action(pubnub)

if __name__ == "__main__":
    main()
```

#### Named Arguments

```python
import os
import time
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.models.consumer.message_actions import PNMessageAction

def add_message_action(pubnub: PubNub):
    msg_action = PNMessageAction()
    msg_action.type = "reaction"
    msg_action.value = "smiley_face"
    msg_action.message_timetoken = str(int(time.time()))

    result = pubnub.add_message_action(
        channel="chats.room1",
        message_action=msg_action
    ).sync()

    if result.status.is_error():
        print(f"Error: {result.status.error_data}")
    else:
        print("Message action added successfully.")
        print(f"Response: {result.result.__dict__}")

def main():
    # Configuration for PubNub instance
    pn_config = PNConfiguration()
    pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo')
    pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')
    pn_config.user_id = os.getenv('USER_ID', 'my_custom_user_id')

    # Initialize PubNub client
    pubnub = PubNub(pn_config)

    # Add message action
    add_message_action(pubnub)

if __name__ == "__main__":
    main()
```

### Returns

The `add_message_action()` operation returns an `Envelope` with the following fields:

| Field | Type | Description |
| --- | --- | --- |
| result | [`PNAddMessageActionResult`](#pnaddmessageactionresult) | Operation result. |
| status | `PNStatus` | Operation status. |

#### PNAddMessageActionResult

```json
{
  'action_timetoken': '15956343330507960',
  'message_timetoken': '1595634332',
  'type': 'reaction',
  'uuid': 'my_uuid',
  'value': 'smiley_face'
}
```

## Remove message action

:::warning Requires Message Persistence
This method requires that Message Persistence is [enabled](https://www.pubnub.com/docs/architecture/authentication/set-up-your-account.md#enable-the-features-your-app-calls) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

Remove a previously added action from a published message. The response is empty.

### Method(s)

Use this Python method:

```python
pubnub.remove_message_action() \
    .channel(String) \
    .action_timetoken(Integer) \
    .message_timetoken(Integer) \
    .pn_async(message_action_callback)
```

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| channel | String | Yes | Channel name to remove the message action from. |
| action_timetoken | Integer | Yes | Timetoken of the message action to remove. |
| message_timetoken | Integer | Yes | Timetoken of the target message. |
| message_action_callback | Function | Yes | Callback for success or error. Details in [PNMessageAction callback](#pnmessageaction). |

### Sample code

#### Builder Pattern

```python
pubnub.remove_message_action()\
    .channel("chats.room1")\
    .action_timetoken(15956346328442840)\
    .message_timetoken(1595634632)\
    .pn_async(message_action_callback)
```

#### Named Arguments

```python
from pubnub.models.consumer.message_actions import PNMessageAction

msg_action = PNMessageAction()
msg_action.type = "reaction"
msg_action.value = "smiley_face"
msg_action.message_timetoken = str(int(time.time()))

pubnub.remove_message_action(channel="chats.room1",
                             action_timetoken=15956346328442840,
                             message_timetoken=1595634632) \
    .pn_async(message_action_callback)
```

### Returns

The `remove_message_action()` operation returns an `Envelope` with the following fields:

| Field | Type | Description |
| --- | --- | --- |
| result | [`PNRemoveMessageActionResult`](#pnremovemessageactionresult) | Operation result. |
| status | `PNStatus` | Operation status. |

#### PNRemoveMessageActionResult

```python
# in case of success (empty object)
{}
```

## Get message actions

:::warning Requires Message Persistence
This method requires that Message Persistence is [enabled](https://www.pubnub.com/docs/architecture/authentication/set-up-your-account.md#enable-the-features-your-app-calls) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

Get a list of message actions in a channel. The response sorts actions by the action timetoken in ascending order.

:::warning How to use the start and end parameters
PubNub retrieves messages by searching **backward through time** (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:
* **start** is the **newer** boundary (higher timetoken value) - search begins here, **exclusive**
* **end** is the **older** boundary (lower timetoken value) - search stops here, **inclusive**
When you provide both parameters, `start` must be a **higher** timetoken than `end`.
```mermaid
graph LR
    A(["9:00 AM<br/>(oldest)"]) -->|"time flows →"| B["end = 10:00 AM<br/>older boundary<br/>inclusive ✓"]
    B --> C["messages<br/>retrieved"]
    C --> D["start = 11:00 AM<br/>newer boundary<br/>exclusive ✗"]
    D -->|"time flows →"| E(["11:59 AM<br/>(newest)"])
    D -.->|"◄ API searches backward"| B
    style A fill:#f8f9fa,stroke:#6c757d,color:#495057
    style E fill:#f8f9fa,stroke:#6c757d,color:#495057
    style B fill:#d4edda,stroke:#28a745,color:#000
    style C fill:#cce5ff,stroke:#004085,color:#000
    style D fill:#fff3cd,stroke:#856404,color:#000
```
9:00 AM10:00 AM11:00 AM11:59 AM
Results are always returned in oldest-first order.
:::

### Method(s)

Use this Python method:

```python
pubnub.get_message_actions() \
  .channel(String) \
    .start(String) \
    .end(String) \
    .limit(Integer) \
    .pn_async(message_action_callback)
```

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| channel | String | Yes | Channel name to list message actions for. |
| start | String | Optional | Newer boundary of the query (exclusive). Must be a higher timetoken value than `end` when both are provided. |
| end | String | Optional | Older boundary of the query (inclusive). Must be a lower timetoken value than `start` when both are provided. |
| limit | Integer | Optional | Maximum number of actions to return. If exceeded, results include a `more` token. See the [REST API documentation](https://www.pubnub.com/docs/sdks/rest-api/get-actions.md). |
| message_action_callback | Function | Yes | Callback for success or error. Details in [PNMessageAction callback](#pnmessageaction). |

### Sample code

#### Builder Pattern

```python
# Retrieve all actions on a single message

pubnub.get_message_actions() \
    .channel("chats.room1") \
    .start("15956342921084731") \
    .end("15956342921084730") \
    .limit(50) \
    .pn_async(message_action_callback)
```

#### Named Arguments

```python
pubnub.get_message_actions(channel="chats.room1",
                           start="15956342921084731",
                           end="15956342921084730",
                           limit=50) \
    .pn_async(message_action_callback)
```

### Returns

The `get_message_actions` operation returns an `Envelope` with the following fields:

| Field | Type | Description |
| --- | --- | --- |
| result | [`PNGetMessageActionsResult`](#pngetmessageactionsresult) | Operation result. |
| status | `PNStatus` | Operation status. |

#### PNGetMessageActionsResult

```json
{
  'actions': [
    {
      'actionTimetoken': '15956373593404068',
      'messageTimetoken': '15956342921084730',
      'type': 'reaction',
      'uuid': 'my_uuid',
      'value': 'smiley_face'
    }
  ]
}
```

## PNMessageAction

The structure of a `PNMessageAction` is as follows:

```python
action = PNMessageAction({
    'uuid': 'user1',
    'type': 'reaction',
    'value': 'smiley_face',
    'actionTimetoken': '15901706735798836',
    'messageTimetoken': '15901706735795200',
})
```

The following is a sample `message_action_callback` method you can use as a starting point for your own implementation:

```python
def message_action_callback(envelope, status):
    if status.is_error():
        print(f"Uh oh. We had a problem sending the message. :( \n {status}")
        return
    if isinstance(envelope, PNAddMessageActionResult):
        print(f"Message Action type: {envelope.type}")
        print(f"Message Action value: {envelope.value}")
        print(f"Message Action timetoken: {envelope.message_timetoken}")
        print(f"Message Action uuid: {envelope.uuid}")
        print(f"Message Action timetoken: {envelope.action_timetoken}")
    elif isinstance(envelope, PNRemoveMessageActionResult):
        # Envelope here is an empty dictionary {}
        pass
    elif isinstance(envelope, PNGetMessageActionsResult):
        print("Message Actions Result:\n")
        for action in envelope.actions:
            print(f"Message Action type: {action.type}")
            print(f"Message Action value: {action.value}")
            print(f"Message Action timetoken: {action.message_timetoken}")
            print(f"Message Action uuid: {action.uuid}")
            print(f"Message Action timetoken: {action.action_timetoken}")
            print("")
```

Last updated at: 2026-11-09T13:29:01.000Z
