
Client for OpenAI's API enabling features like models, chat, images, embeddings, and fine-tuning. Supports coroutines and provides sample apps, troubleshooting guides, and configuration flexibility.
Kotlin client for OpenAI's API with multiplatform and coroutines capabilities.
build.gradle file:repositories {
mavenCentral()
}
dependencies {
implementation "com.aallam.openai:openai-client:4.2.0"
}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:4.2.0')
// define dependencies without versions
implementation 'com.aallam.openai:openai-client'
runtimeOnly 'io.ktor:ktor-client-okhttp'
}In multiplatform projects, add openai client dependency to commonMain, and choose
an engine for each target.
Gradle is required for multiplatform support, but there's nothing stopping you from using the jvm client in a Maven project. You still need to add to your dependencies one of Ktor's engines.
<dependencies>
<dependency>
<groupId>com.aallam.openai</groupId>
<artifactId>openai-client-jvm</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-okhttp-jvm</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>The BOM is not supported for Maven projects.
[!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.
The following interfaces remain available for existing consumers, but their OpenAI endpoints have shut down. See the migration guide.
Vector Stores remain independent of the retired Assistants endpoints.
Looking for a tokenizer? Try ktoken, a Kotlin library for tokenizing text.
Get started and understand more about how to use OpenAI API client for Kotlin with these guides:
Sample apps are available under sample, please check the README for running instructions.
The specific rules are already bundled into the Jar which can be interpreted by R8 automatically.
To import snapshot versions into your project, add the following code snippet to your gradle file:
repositories {
//...
maven { url 'https://central.sonatype.com/repository/maven-snapshots/' }
}For common issues and their solutions, check the Troubleshooting Guide.
openai-client tests are live integration tests and can generate billable API traffic.
OPENAI_LIVE_TESTS=1 and OPENAI_API_KEY.The JVM live suite excludes mocked offline fixtures. CI runs those once in Offline Checks, then runs the live suite plus the separate Live and Content Provenance tasks. Containers live tests remain opt-in for eligible projects.
Examples:
# Free/offline checks
./gradlew :openai-core:jvmTest :openai-core:jsTest :openai-core:wasmJsTest :openai-core:apiCheck :openai-client:apiCheck
# Mocked HTTP client tests (no credentials or API traffic)
./gradlew :openai-client:offlineJvmTest
# Live smoke (billable)
OPENAI_LIVE_TESTS=1 OPENAI_API_KEY=... ./gradlew :openai-client:jvmTest --tests "*.TestModels"Appreciate the project? Here's how you can help:
OpenAI Kotlin API Client is an open-sourced software licensed under the MIT license. This is an unofficial library, it is not affiliated with nor endorsed by OpenAI. Contributions are welcome.
Kotlin client for OpenAI's API with multiplatform and coroutines capabilities.
build.gradle file:repositories {
mavenCentral()
}
dependencies {
implementation "com.aallam.openai:openai-client:4.2.0"
}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:4.2.0')
// define dependencies without versions
implementation 'com.aallam.openai:openai-client'
runtimeOnly 'io.ktor:ktor-client-okhttp'
}In multiplatform projects, add openai client dependency to commonMain, and choose
an engine for each target.
Gradle is required for multiplatform support, but there's nothing stopping you from using the jvm client in a Maven project. You still need to add to your dependencies one of Ktor's engines.
<dependencies>
<dependency>
<groupId>com.aallam.openai</groupId>
<artifactId>openai-client-jvm</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-okhttp-jvm</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>The BOM is not supported for Maven projects.
[!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.
The following interfaces remain available for existing consumers, but their OpenAI endpoints have shut down. See the migration guide.
Vector Stores remain independent of the retired Assistants endpoints.
Looking for a tokenizer? Try ktoken, a Kotlin library for tokenizing text.
Get started and understand more about how to use OpenAI API client for Kotlin with these guides:
Sample apps are available under sample, please check the README for running instructions.
The specific rules are already bundled into the Jar which can be interpreted by R8 automatically.
To import snapshot versions into your project, add the following code snippet to your gradle file:
repositories {
//...
maven { url 'https://central.sonatype.com/repository/maven-snapshots/' }
}For common issues and their solutions, check the Troubleshooting Guide.
openai-client tests are live integration tests and can generate billable API traffic.
OPENAI_LIVE_TESTS=1 and OPENAI_API_KEY.The JVM live suite excludes mocked offline fixtures. CI runs those once in Offline Checks, then runs the live suite plus the separate Live and Content Provenance tasks. Containers live tests remain opt-in for eligible projects.
Examples:
# Free/offline checks
./gradlew :openai-core:jvmTest :openai-core:jsTest :openai-core:wasmJsTest :openai-core:apiCheck :openai-client:apiCheck
# Mocked HTTP client tests (no credentials or API traffic)
./gradlew :openai-client:offlineJvmTest
# Live smoke (billable)
OPENAI_LIVE_TESTS=1 OPENAI_API_KEY=... ./gradlew :openai-client:jvmTest --tests "*.TestModels"Appreciate the project? Here's how you can help:
OpenAI Kotlin API Client is an open-sourced software licensed under the MIT license. This is an unofficial library, it is not affiliated with nor endorsed by OpenAI. Contributions are welcome.