Skip to main content
Coho Kotlin SDK
Updated over 3 months ago

Overview

The Coho Kotlin SDK provides an easy way to integrate with Coho's services, allowing you to send custom events to Coho's endpoint. The SDK supports retry mechanisms for transient errors and provides extensive logging capabilities.

Installation

  1. To add Coho Kotlin SDK to your project, include the following repository in your settings.gradle.kts file (Project Settings):

    dependencyResolutionManagement {
    ...
    repositories {
    ...
    maven {
    url = uri("https://us-maven.pkg.dev/coho-shared/maven-public")
    }
    }
    }
  2. Add the SDK version to your libs.versions.toml file:

    [versions]
    ...
    cohosdk = "SDK_VERSION"

    [libraries]
    ...
    cohosdk = { group = "ai.coho.sdk", name = "cohosdk", version.ref = "cohosdk"}
  3. Add the SDK dependency to your build.gradle.kts file (Module level):

    dependencies {
    ...
    implementation(libs.cohosdk)
    }
  4. Finally, sync your project with Gradle (File -> Sync Project with Gradle Files)

Usage

Initializing the SDK

Before using the SDK, you need to initialize it with the appropriate options.

Make sure you choose the region your account is in. If you’re unsure which is it, contact us.

import ai.coho.sdk.CohoSDK 
import ai.coho.sdk.CohoSDKOptions
import ai.coho.utils.Region

val sdkOptions = CohoSDKOptions(
tenantId = "your-tenant-id",
region = Region.EU, //or Region.US
retries = 3, //default is 0
retryDelay = 1000, //default is 100ms
enableLogging = true )

val cohoSDK = CohoSDK(sdkOptions)

Setting the User ID

Before sending events, you need to set the user ID using the setUserId method.

cohoSDK.setUserId("user-id")

Sending Events

To send an event, use the sendEvent method. This method takes the event name and an optional map of additional properties.

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

val coroutineScope = CoroutineScope(Dispatchers.IO)

coroutineScope.launch {
cohoSDK.sendEvent("EventName", mapOf("customField" to "customValue"))
}

Example

Here's a complete example that initializes the SDK, sets the user ID, and sends an event.

import ai.coho.sdk.CohoSDK
import ai.coho.sdk.CohoSDKOptions
import ai.coho.utils.Region
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

fun main() {
val sdkOptions = CohoSDKOptions(
tenantId = "your-tenant-id",
region = Region.EU,
retries = 3,
retryDelay = 1000,
enableLogging = true
)

val cohoSDK = CohoSDK(sdkOptions)
val coroutineScope = CoroutineScope(Dispatchers.IO)

cohoSDK.setUserId("user-id")

coroutineScope.launch {
cohoSDK.sendEvent("EventName", mapOf("customField" to "customValue"))
}
}

Detailed API

CohoSDKOptions

CohoSDKOptions is used to configure the SDK.

data class CohoSDKOptions(
val tenantId: String,
val region: Region,
val retries: Int = 0,
val retryDelay: Long = 100,
val enableLogging: Boolean = false
)

CohoSDK

CohoSDK provides methods to set the user ID and send events.

setUserId

Sets the user ID for the SDK. This method must be called before sending any events.

fun setUserId(userId: String)

sendEvent

Sends an event to Coho's endpoint. This method takes the event name and an optional map of additional properties.

@Throws(IOException::class)
suspend fun sendEvent(eventName: String, additionalProperties: Map<String, Any> = emptyMap())

Error Handling

The SDK provides robust error handling mechanisms, including retries for transient errors. If the user ID is not set or is empty, the event will not be sent, and a log message will indicate that the user ID is missing.

Logging

Logging can be enabled by setting the enableLogging flag in CohoSDKOptions. When enabled, the SDK logs detailed information about requests and responses, which can be useful for debugging.

Did this answer your question?