openrouter-kotlin¶
Kotlin client library for the OpenRouter API.
openrouter-kotlin provides a clean, type-safe, and idiomatic way to interact with OpenRouter from Kotlin and other JVM languages.
Key Features¶
- Type-Safe DSL: Build complex chat requests with an intuitive Kotlin DSL.
- Full Streaming Support: Easily handle streaming responses with Kotlin Coroutines Flow.
- Robust Error Handling: Exhaustive sealed exception hierarchy for all API and HTTP error states.
- Complete API Coverage: Support for Chat Completions, Models, Providers, and Account endpoints.
- Custom Serialization: Built-in support for OpenRouter's flexible JSON union types.
- Ktor-Powered: Built on the modern Ktor HTTP client with the efficient CIO engine.
Quick Example¶
import dev.toliner.openrouter.client.OpenRouterClient
import dev.toliner.openrouter.client.OpenRouterConfig
import dev.toliner.openrouter.l2.chat.chat
import io.ktor.client.engine.cio.CIO
val config = OpenRouterConfig(apiKey = "sk-or-...")
val client = OpenRouterClient(CIO.create(), config)
val response = client.chat {
model = "openai/gpt-4o-mini"
userMessage("Hello! Tell me about Kotlin.")
}
println(response.choices.first().message?.content)