Text to Speech in Kotlin

Generate spoken audio from Kotlin using the Narakeet REST API. The example below uses java.net.HttpURLConnection from the standard library — no Gradle dependency or third-party SDK required. It compiles with any Kotlin version targeting JDK 8 or later and runs in CLI tools, Spring Boot services, and backend systems that power Android frontends.

For endpoint details, authentication, and cross-language features, see the main Text to Speech API reference.

Kotlin Text to Speech Example

The following program sends plain text to the Narakeet API and saves the audio response as an MP3 file. It uses HttpURLConnection from the JDK, so no external libraries are needed.

import java.io.File
import java.net.HttpURLConnection
import java.net.URL

fun textToSpeech(apiKey: String, voice: String, text: String, outputPath: String) {
    val url = URL("https://api.narakeet.com/text-to-speech/mp3?voice=$voice")
    val connection = url.openConnection() as HttpURLConnection
    connection.requestMethod = "POST"
    connection.connectTimeout = 30000
    connection.readTimeout = 30000
    connection.setRequestProperty("Accept", "application/octet-stream")
    connection.setRequestProperty("Content-Type", "text/plain")
    connection.setRequestProperty("x-api-key", apiKey)
    connection.doOutput = true

    connection.outputStream.use { it.write(text.toByteArray()) }

    if (connection.responseCode != 200) {
        val errorBody = connection.errorStream?.bufferedReader()?.readText() ?: "no details"
        throw RuntimeException("API error ${connection.responseCode}: $errorBody")
    }

    connection.inputStream.use { input ->
        File(outputPath).outputStream().use { output ->
            input.copyTo(output)
        }
    }
}

fun main() {
    val apiKey = System.getenv("NARAKEET_API_KEY")
    if (apiKey.isNullOrEmpty()) {
        println("Please set NARAKEET_API_KEY environment variable")
        System.exit(1)
    }

    textToSpeech(apiKey, "chloe", "Hi there from Kotlin", "output.mp3")
    println("File saved at: output.mp3")
}

Set NARAKEET_API_KEY in your environment before running — create a key from the API Keys page. Save the file as Tts.kt and run it with kotlinc Tts.kt -include-runtime -d tts.jar && java -jar tts.jar.

For the complete project with Gradle configuration, see the Kotlin streaming API example on GitHub.

Why Kotlin for Text to Speech

Kotlin’s concise syntax, null-safe operators, and use scoping function make HTTP integrations compact and safe. The example above handles resource cleanup, error streams, and file writing in fewer lines than the equivalent Java code, with less ceremony and no risk of unclosed streams.

Where Kotlin text to speech fits in practice:

  • Spring Boot microservices that generate audio responses for chatbot or accessibility endpoints
  • Ktor or http4k server handlers that return spoken audio on demand
  • Gradle build scripts that produce localised audio assets during CI/CD
  • Backend services that power Android apps needing server-rendered speech
  • CLI tools that batch-convert documentation or notification templates into audio files

For content longer than 1 KB or uncompressed WAV output, switch to the Long Content (Polling) API.

Spring Boot Text to Speech

Kotlin is the preferred language for new Spring Boot projects, and adding text to speech to a Spring Boot service requires nothing beyond the JDK classes already on the classpath. Expose a REST controller that accepts text, call the Narakeet API from a service bean, and return the audio bytes as a ResponseEntity<ByteArray>. Because HttpURLConnection is part of the JDK, there is no starter dependency to add and no auto-configuration to manage. If your project already uses Spring’s RestTemplate or WebClient, the same API call translates directly — POST text, receive an audio stream.

For Android apps backed by a Spring Boot service, generating speech on the server keeps your API key out of the APK. The Android client sends text to your endpoint, and the server relays it to Narakeet and streams the audio back.

Kotlin TTS Without External Libraries

With Narakeet, no Kotlin or Java text to speech library is required. HttpURLConnection has been part of the JDK since version 1.1, so the example runs on every JVM without pulling in OkHttp, Retrofit, Fuel, or Ktor Client. This keeps your dependency tree clean and avoids version conflicts in multi-module Gradle projects. The textToSpeech function in the example above can be dropped into any existing Kotlin project as-is.

If your project already includes an HTTP client such as OkHttp or Ktor Client, the API call is identical — POST plain text with the x-api-key header and read the binary response.

Kotlin vs Java for Text to Speech

Both Kotlin and Java can call the Narakeet API with the same JDK classes, but Kotlin produces noticeably shorter code for the same task. Kotlin’s use extension replaces Java’s try-with-resources blocks, string templates replace String.format or concatenation, and the ?. safe-call operator eliminates explicit null checks on the error stream. The result is a text to speech integration that reads top-to-bottom without boilerplate. If your team already writes Kotlin, there is no reason to drop into Java for this task — and if you are evaluating both, the Java Text to Speech API page shows the equivalent Java version for comparison.

Kotlin Voice Generation API Options

The endpoint URL accepts query parameters that control the voice and output:

Selecting a voice: Set ?voice=chloe (or any of the 900 names listed at Text to Speech Voices).

Adjusting speed: Add &voice-speed=1.2 for faster reading, or &voice-speed=0.8 to slow it down. The value is a multiplier around normal pace.

Controlling volume: Add &voice-volume=soft or &voice-volume=loud.

Choosing a format: The endpoint path determines the codec — /mp3, /m4a, or /wav. WAV output is only available with the polling API.

For pitch control, sentence pauses, and multi-voice scripts, use the script header format inside the request body. Full reference at Configuring Audio Tasks.