---
title: "Update context"
description: "Update the context of an active WebSocket session with the session:context:update event, and handle the success and error events."
---

# Update context

> Update the context of an active WebSocket session with the session:context:update event, and handle the success and error events.

## Event `session:context:update`

The `session:context:update` event allows you to modify the
[**context**](/docs/ai-agents/context) of an existing session. This is useful when you need to
update user information or add new context data after the initial session creation.

### Use cases

- Updating user information **after** initial form submission
- Adding new context data during the conversation
- Modifying user preferences or settings
- Updating user location or device information
- Adding or modifying custom context fields

### Payload example

```json
{
  "session_id": "123e4567-e89b-12d3-a456-426614174000",
  "context": {
    "user": {
      "display_name": "John Doe",
      "phone": "+1234567890",
      "email": "john.doe@example.com"
    },
    "custom_fields": {
      "preferred_language": "en",
      "subscription_tier": "premium"
    }
  },
  "timestamp": 1635789600000
}
```

### Payload fields

| Field        | Type    | Required | Description                                                                                                             |
| ------------ | ------- | -------- | ----------------------------------------------------------------------------------------------------------------------- |
| `session_id` | String  | Yes      | The unique identifier of the session to update. Check [this page](https://docs.moveo.ai/api/websocket/create-session.md) for more information.            |
| `context`    | Object  | Yes      | A [ContextObject](/docs/ai-agents/context) containing the updated context information. Must contain a `user` object. |
| `timestamp`  | Integer | Yes      | Unix timestamp in milliseconds representing when the context update was initiated.                                      |

### Server responses

#### Success: `session:context:updated`

On successful context update, the server responds with a `session:context:updated` event containing
the updated context:

```json
{
  "session_id": "123e4567-e89b-12d3-a456-426614174000",
  "context": {
    "user": {
      "display_name": "John Doe",
      "phone": "***",
      "email": "john.doe@example.com"
    },
    "custom_fields": {
      "preferred_language": "en",
      "subscription_tier": "premium"
    }
  }
}
```

:::warning
Moveo controls certain fields in the context object. As these are likely to be sensitive,
Moveo obfuscates these fields in the response and shows them as `***`.
:::

#### Error: `session:context:update:error`

If the context update fails, the server responds with an error event. If the session does not exist
or has expired, the server sends a `session:expired` event instead.

```json
{
  "error_code": "internal-server-error",
  "description": "unable to update context"
}
```

### Example usage

```javascript
// Initialize socket connection
const socket = io(URL, socketOptions);

// Update context payload
const contextUpdatePayload = {
  session_id: "123e4567-e89b-12d3-a456-426614174000",
  context: {
    user: {
      display_name: "Jane Smith",
      email: "jane.smith@example.com",
      phone: "+1987654321",
    },
    custom_fields: {
      preferred_language: "en",
      subscription_tier: "premium",
    },
  },
  timestamp: Date.now(),
};

// Send context update event
socket.emit("session:context:update", contextUpdatePayload);

// Handle success response
socket.on("session:context:updated", (data) => {
  console.log("Context updated successfully:", data.context);
});

// Handle error response
socket.on("session:context:update:error", (error) => {
  console.error("Error updating context:", error.description);
});
```

:::note
The context update merges with the existing context, so you only need to include the fields
that you want to update. Any fields not included in the update keep their previous values.
:::
