---
schema_version: "1"
id: 03bc926ab340d20969412fe8fe7e74188f6f5a3f0d109a1f68e7a797e04dac78
title: Use the Session-Managed Bot Client
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/bot-api/einstein-bot/guides/sdk/sdk-use-session-managed-bot-client.md
canonical_url: https://developer.salesforce.com/docs/service/einstein-bot-api/guide/sdk-use-session-managed-bot-client.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: f0f04781ce129bb9711de81d3fbfe01edd814ca82bc6411eba98c754349b953a
products:
  - Einstein Bots API and SDK
  - Enstein Bot API and SDK
---
# Use the Session-Managed Bot Client

This section provides code samples of running a bot session with the `SessionManagedChatbotClient` interface.

## Set Up the Client

The `SessionManagedChatbotClient` adds session management capabilities on top of the `BasicChatbotClient`. Therefore, to create an instance of the session-managed client, pass `BasicChatbotClient` as a parameter to the `SessionManagedChatbotClient`. See [Use the Basic Bot Client](https://developer.salesforce.com/docs/service/einstein-bot-api/guide/sdk-use-basic-bot-client.md).

```java
// Create a JWT Bearer authentication mechanism.
AuthMechanism oAuth = JwtBearerOAuth.with()
    .privateKeyFilePath(privateKeyFilePath)
    .loginEndpoint(loginEndpoint)
    .connectedAppId(connectedAppId)
    .connectedAppSecret(secret)
    .userId(userId)
    .build();

// Create a session-managed client.
SessionManagedChatbotClient client = ChatbotClients
    .sessionManaged()
    .basicClient(ChatbotClients.basic()
        .basePath(basePath)
        .authMechanism(oAuth)
        .build())
    .cache(new RedisCache(YOUR_TTL_SECONDS,YOUR_REDIS_URL))
    .build();
```

If you are running the client locally, you can use `InMemoryCache(YOUR_TTL_SECONDS)` instead of `RedisCache(YOUR_TTL_SECONDS,YOUR_REDIS_URL)` for the `.cache` value. The `InMemoryCache` is the better caching method when running the client locally but for production use `RedisCache` or your own distributed cache.

Then create a `RequestConfig` object and add the bot ID, org ID, ForceConfig Endpoint that you already set up in the [Set Up the Configuration Variables](https://developer.salesforce.com/docs/service/einstein-bot-api/guide/sdk-set-up.md) section.

```java
RequestConfig config = RequestConfig.with()
    .botId(botId)
    .orgId(orgId)
    .forceConfigEndpoint(forceConfigEndPoint)
    .build();
```

## Send Messages

The session-managed client starts a new session and sends subsequent messages using the same method instead of two separate methods. A session is tracked by a combination of the external session ID, bot ID, and org ID. If the client sends a message request with a new combination of those IDs, the bot starts a new session. If the combination exists, the message is part of an active session.

```java
// Retrieve the external session ID from the external channel.
ExternalSessionId externalSessionId =
    new ExternalSessionId(UUID.randomUUID().toString());

// Build the request to send a message to the bot.
BotSendMessageRequest sendMessageRequest = BotRequest
    .withMessage(buildTextMessage(message))
    .build();

// Send the request using the external session ID.
BotResponse response = client
   .sendMessage(config, externalSessionId, sendMessageRequest);
```

## Retrieve the Bot Response

The `BotResponse` object is the same as the one of the `BasicChatbotClient` so you can [Retrieve the Bot’s Response](https://developer.salesforce.com/docs/service/einstein-bot-api/guide/sdk-basic-bot-client-retrieve-bots-response.md) the same way.

## End the Bot Session

The code is the same as [End a Bot Session](https://developer.salesforce.com/docs/service/einstein-bot-api/guide/sdk-basic-bot-client-end-session.md) for the `BasicChatbotClient` except that you pass `externalSessionId` instead of the bot’s internal session ID.

```java
// Build the request to end the bot session.
BotEndSessionRequest botEndSessionRequest = BotRequest
    .withEndSession(EndSessionReason.USERREQUEST).build();

// Use the external session ID of the external channel.
ExternalSessionId externalSessionId =
    new ExternalSessionId(UUID.randomUUID().toString());

// Send the request to the bot to end the session.
BotResponse endSessionResponse = client
    .endChatSession(config, externalSessionId, botEndSessionRequest);
```
