Why Integrate ChatGPT into a Kotlin Application?
Imagine: you're writing an app in Kotlin, and suddenly you realize it lacks a "brain." The user asks a question, and the program stalls. Sound familiar? Integrating ChatGPT API in Kotlin turns an ordinary application into a smart assistant. This isn't a passing trend but a working tool: chatbots, content generators, translators, code helpers — you can build all of this yourself.
In this article, we won't beat around the bush. I'll show you how to connect ChatGPT to your project in literally 15 minutes, using modern Kotlin tools: Retrofit, OkHttp, Coroutines, and Gson. Even if you're a beginner, just follow the steps — the code is working and tested.
What Do We Need? Tools and Preparation
To work with OpenAI API Kotlin, we need a minimal set. Get ready with:
- OpenAI API key — register at platform.openai.com, create a key in the API keys section. Keep it secret!
- Development environment — IntelliJ IDEA (Community edition is enough) or Android Studio if you're developing for mobile.
- Gradle or Maven — for adding dependencies.
We'll use Retrofit Kotlin as the HTTP client and Gson Kotlin for working with JSON. Why these? Retrofit provides clean, declarative code, and Gson can convert API responses into data classes without extra effort.
Step 1. Create a Project and Add Dependencies
Create a new Gradle project (Kotlin/JVM). Add the following to the build.gradle.kts file:
dependencies { implementation("com.squareup.retrofit2:retrofit:2.9.0") implementation("com.squareup.retrofit2:converter-gson:2.9.0") implementation("com.squareup.okhttp3:okhttp:4.12.0") implementation("com.squareup.okhttp3:logging-interceptor:4.12.0") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0") implementation("com.google.code.gson:gson:2.10.1")}Important: logging-interceptor is optional, but it will save you during debugging — you'll see what actually goes to the server and what comes back. If you're developing for Android, also add implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0").
Step 2. Data Models: How ChatGPT "Sees" Your Request
The OpenAI API accepts and returns strict JSON. To avoid manual parsing, we'll create data classes. This is the foundation of any Kotlin ChatGPT code example.
// Request model for ChatGPTdata class ChatRequest( val model: String = "gpt-4o-mini", // cheapest and fastest model val messages: List<Message>, val temperature: Double = 0.7)
data class Message( val role: String, // "user", "assistant", "system" val content: String)
// Response model from ChatGPTdata class ChatResponse( val choices: List<Choice>?)
data class Choice( val message: Message?, val finish_reason: String?)Note: we set model to gpt-4o-mini — this is a modern model from OpenAI that costs pennies but delivers decent results. Perfect for testing.
Step 3. Retrofit: Configure the Client for Communicating with OpenAI
Now let's create an interface that describes our single endpoint. This is the classic Retrofit Kotlin approach.
interface OpenAiApi { @POST("v1/chat/completions") @Headers("Content-Type: application/json") suspend fun chatCompletion( @Header("Authorization") auth: String, @Body request: ChatRequest ): ChatResponse}Key point: suspend