Java Text to Speech Example

Call the Narakeet text to speech API from Java to produce audio files programmatically. The example on this page uses Apache HttpClient, but any HTTP library works — the API is a straightforward REST endpoint. No proprietary SDK or JNI binding required.

Refer to the Text to Speech API documentation for endpoint details, authentication setup, and features shared across all programming languages.

Java Text to Speech Example

The method below sends a string to the API and writes the MP3 response to disk. This Java text to speech example demonstrates the full request cycle with minimal boilerplate.

public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {

    String apiKey = Objects.requireNonNull(System.getenv("NARAKEET_API_KEY"), "NARAKEET_API_KEY environment variable is not set");

    String voice = "raymond";
    String text = "Hello from Java!";

    String url = String.format("https://api.narakeet.com/text-to-speech/mp3?voice=%s", voice);
    String outputFilePath = "output.mp3";

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("Accept", "application/octet-stream");
    httpPost.setHeader("Content-Type", "text/plain");
    httpPost.setHeader("x-api-key", apiKey);

    byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
    ByteArrayEntity requestBody = new ByteArrayEntity(utf8Bytes);
    httpPost.setEntity(requestBody);

    FileOutputStream outputStream = new FileOutputStream(outputFilePath);
    HttpResponse response = httpClient.execute(httpPost);
    response.getEntity().writeTo(outputStream);
    outputStream.close();
}

The NARAKEET_API_KEY environment variable must be set before launching. Get a key from the API Keys dashboard. The full Maven project is at text-to-speech-api-java-example on GitHub.

Java Text to Speech API

The Java text to speech API is a plain REST endpoint, so any HTTP client works — Apache HttpClient (shown above), OkHttp, or even java.net.HttpURLConnection from the standard library. Pick whichever your project already uses; there is no dedicated SDK to evaluate or maintain.

Situations where the Java text to speech API is particularly effective:

  • Spring Boot microservices that generate on-demand audio responses
  • Batch jobs in enterprise systems that narrate daily financial or operational reports
  • Android applications that need consistent server-side voice quality across devices
  • Jenkins or Gradle plugins that produce spoken test summaries
  • Kafka consumers that turn incoming messages into audio notifications

When input exceeds 1 KB or you want uncompressed WAV, use the Long Content (Polling) API. See the Java polling example for a complete implementation.

Java TTS API

Using the Narakeet Java TTS API requires no voice model bundling, no native dependencies, and no proprietary SDK. Your application sends text over HTTPS and receives studio-quality audio from 900 AI voices in 100 languages, with identical results on every JVM. Traditional options like FreeTTS or MaryTTS ship large voice files and produce dated-sounding output by comparison.

Java Text to Speech API Options

Modify the query string on the endpoint URL to adjust the audio:

  • voice — Choose a speaker by name, e.g. ?voice=raymond. The voice catalogue lists all 900 options.
  • voice-speed — Decimal multiplier. 1.3 reads 30% faster; 0.7 reads 30% slower.
  • voice-volumesoft, medium, or loud.
  • format — Swap /mp3 in the URL for /m4a (smaller files, similar quality) or /wav (uncompressed, polling API only).

Advanced features like pitch control and multi-voice scripts use the script header inside the request body. Details at Configuring Audio Tasks.