# Custom Storage

The SDK persists two internal keys: a first-launch flag (`Detour_firstEntranceFlag`) and an analytics device identifier (`Detour_deviceId`). Left unset, `DetourConfig.storage` is `null` and the SDK falls back to its own `SharedPreferences`-based store. To use encrypted storage, DataStore or another persistence layer, pass a `DetourStorage` implementation instead.

:::caution[Values must persist across restarts]
Both keys have to persist across app restarts. A storage adapter that loses them makes the SDK re-run the deferred flow and generate a new analytics device id.
:::

## Storage interface

```kotlin
interface DetourStorage {
    suspend fun getItem(key: String): String?
    suspend fun setItem(key: String, value: String)
    suspend fun removeItem(key: String) { }
}
```

## Examples

The adapter below wraps `EncryptedSharedPreferences`. Any class that implements `DetourStorage` works, so use it as a starting point for DataStore or another persistence layer.

### EncryptedSharedPreferences

```kotlin
import android.content.Context
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.swmansion.detour.storage.DetourStorage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class EncryptedStorageProvider(context: Context) : DetourStorage {
    private val masterKey = MasterKey.Builder(context)
        .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
        .build()

    private val prefs = EncryptedSharedPreferences.create(
        context,
        "DetourSecureStorage",
        masterKey,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    )

    override suspend fun getItem(key: String): String? = withContext(Dispatchers.IO) {
        prefs.getString(key, null)
    }

    override suspend fun setItem(key: String, value: String) = withContext(Dispatchers.IO) {
        prefs.edit().putString(key, value).apply()
    }
}
```

```kotlin
val config = DetourConfig(
    apiKey = BuildConfig.DETOUR_API_KEY,
    appId = BuildConfig.DETOUR_APP_ID,
    storage = EncryptedStorageProvider(this)
)
```

## Related pages

- [Usage](https://detour.swmansion.com/docs/sdk/android/sdk-usage)
- [API Reference](https://detour.swmansion.com/docs/sdk/android/api-reference)