---
title: Initialization
slug: lKw0-initialization
docTags: 
createdAt: 2026-09-14T23:58:02.656Z
---

## Initialize the SDK

The first step to use the Kindly SDK is to initialize it with your bot key and market slug:

```kotlin
KindlySDK.start(
    application = application, 
    botKey = "YOUR_BOT_KEY", 
    languageCode = "en",
    market = "YOUR_MARKET"
)
```

You can also provide an authentication token provider if your bot requires authentication:

```kotlin
KindlySDK.start(
    application = application,
    botKey = "YOUR_BOT_KEY",
    languageCode = "en",
    market = "YOUR_MARKET",
    authTokenProvider = KindlyAuthTokenProvider { request ->
        // Suspend code — return a JWT for request.chatId.
        fetchJwtFromBackend(request.chatId)
    }
)
```

For more details on authentication, see [Using Authentication](authentication.md).

## Markets (per-location settings)

Bots can serve their UI, greeting, and responses per market. The `market` slug is **required** —
pass the market configured for your bot:

```kotlin
KindlySDK.start(
    application = application,
    botKey = "YOUR_BOT_KEY",
    languageCode = "en",
    market = "eu"
)
```

The SDK loads that market's settings and tells the backend which market was picked (so
greeting/responses are localized to it). If the market's settings can't be loaded the chat won't
load — there is **no fallback** to another market and no per-bot default settings.
Find your market slugs in the Kindly platform (Settings → General → Details & markets).
See the [Markets](markets.md) guide for details.

## Change Language at Runtime

You can change the language after initialization using:

```kotlin
KindlySDK.setLanguage("lt")  // Change to Lithuanian
```

This method will:

- Call the language switch API
- Update the cached settings
- Refresh UI translations
- Notify all UI components of the language change

The language setting will persist across chat sessions until explicitly changed.

## Bot Switching and Reinitialization

The SDK intelligently handles bot switching and reinitialization scenarios when you call `start()` multiple times with different parameters.

### Automatic Bot Key Switching

When you call `start()` with a **different bot key**, the SDK automatically:

1. **Detects the change**: Compares the new bot key with the current one
2. **Performs cleanup**: Calls `endChat()` internally to end the current session
3. **Resets state**: Clears SDK initialization flags and session data
4. **Reinitializes**: Connects to the new bot seamlessly

```kotlin
// First initialization
KindlySDK.start(application = this, botKey = "bot-key-1", market = "YOUR_MARKET")
KindlySDK.launchChat(context = this)

// Later, switch to different bot - automatic cleanup happens
KindlySDK.start(application = this, botKey = "bot-key-2", market = "YOUR_MARKET")  // SDK handles cleanup internally
KindlySDK.launchChat(context = this)
```

### Connection State Handling

When the SDK is **disconnected** (e.g., after calling `endChat()`), calling `start()` with the same bot key will **reconnect**:

```kotlin
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY", market = "YOUR_MARKET")
KindlySDK.launchChat(context = this)

// Later, end the chat
KindlySDK.endChat()

// Reconnect with same bot key - allowed reconnection  
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY", market = "YOUR_MARKET")  // Reconnects instead of skipping
KindlySDK.launchChat(context = this)
```

### Skipping Duplicate Initialization

The SDK **skips initialization** only when all conditions are met:

- Same bot key
- **Still connected** (not disconnected)

```kotlin
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY", market = "YOUR_MARKET")
// This second call will be skipped - no changes needed
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY", market = "YOUR_MARKET")
```

### Best Practices for Bot Switching

### Option 1: Automatic Switching (Recommended)

```kotlin
// SDK handles cleanup automatically
KindlySDK.start(application = this, botKey = "new-bot-key", market = "YOUR_MARKET")
KindlySDK.launchChat(context = this)
```

### Option 2: Explicit End-then-Start

```kotlin
// Explicit control over the lifecycle
KindlySDK.endChat()
// endChat() completes synchronously, so you can immediately call start()
KindlySDK.start(application = this, botKey = "new-bot-key", market = "YOUR_MARKET")
KindlySDK.launchChat(context = this)
```

Both approaches work identically - choose based on your preference for explicit vs automatic lifecycle management.
