Code
OpenAI API client for Kotlin – With multiplatform and coroutines capabilities

Kotlin client for OpenAI’s API with multiplatform and coroutines capabilities.
📦 Setup
- Install OpenAI API Kotlin client by adding the following dependency to your
build.gradle
file:
repositories {
mavenCentral()
}
dependencies {
implementation "com.aallam.openai:openai-client:3.2.4"
}
- Choose and add to your dependencies one of Ktor’s engines.
BOM
Alternatively, you can use openai-client-bom by adding the following dependency to your build.gradle
file
dependencies {
// import Kotlin API client BOM
implementation platform('com.aallam.openai:openai-client-bom:3.2.4')
// define dependencies without versions
implementation 'com.aallam.openai:openai-client'
runtimeOnly 'io.ktor:ktor-client-okhttp'
}
Multiplaform
In multiplatform projects, add openai client dependency to commonMain
, and choose an engine for each target.
⚡️ Getting Started
Note: OpenAI encourages using environment variables for the API key. Read more.
Create an instance of OpenAI
client:
val openai = OpenAI(
token = "your-api-key",
timeout = Timeout(socket = 60.seconds),
// additional configurations...
)
Or you can create an instance of OpenAI
using a pre-configured OpenAIConfig
:
val config = OpenAIConfig(
token = apiKey,
timeout = Timeout(socket = 60.seconds),
// additional configurations...
)
val openAI = OpenAI(config)
Use your OpenAI
instance to make API requests. Learn more.
Supported features
ℹ️ Sample apps
Sample apps are available under sample
, please check the README for running instructions.
🔒 ProGuard / R8
The specific rules are already bundled into the Jar which can be interpreted by R8 automatically.
