Text to Speech with Dart

Build spoken audio from Dart using the Narakeet REST API. The example on this page uses only dart:io and dart:convert — no pub package required. It runs with the standard Dart SDK and works in CLI tools, server apps, and backend services that power Flutter frontends.

For endpoint details, authentication, and features shared across languages, see the main Text to Speech API reference.

Getting Started with Dart Text to Speech

Before running the example, you need two things:

  1. A Narakeet API key — create one from the API Keys dashboard
  2. The Dart SDK (version 2.17 or later for enhanced enums and super parameters, though the example works on older versions too)

Export your key as an environment variable:

export NARAKEET_API_KEY=your-key-here

Dart Text to Speech Example

The following Dart program sends text to the Narakeet API and saves the audio response as an MP3 file. It uses HttpClient from dart:io, so there are zero external dependencies.

import 'dart:convert';
import 'dart:io';

Future<void> main() async {
  final String apiKey = Platform.environment['NARAKEET_API_KEY'] ?? '';
  final String voice = 'mickey';
  final String text = 'Hi there from Dart';
  final String url = 'https://api.narakeet.com/text-to-speech/mp3?voice=$voice';

  if (apiKey.isEmpty) {
    print('Please set NARAKEET_API_KEY environment variable');
    return;
  }

  final HttpClient client = HttpClient();
  final HttpClientRequest request = await client.postUrl(Uri.parse(url));

  request.headers.set('Accept', 'application/octet-stream');
  request.headers.set('Content-Type', 'text/plain');
  request.headers.set('x-api-key', apiKey);
  request.add(utf8.encode(text));

  final HttpClientResponse response = await request.close();

  if (response.statusCode == 200) {
    final File file = File('output.mp3');
    final IOSink sink = file.openWrite();
    await response.pipe(sink);
    await sink.close();

    print('File saved at: ${file.path}');
  } else {
    print('Failed to download file: ${response.statusCode}');
  }

  client.close();
}

Save this as tts.dart and run it with dart run tts.dart. The program streams the response directly to disk, keeping memory usage low even for longer audio files.

For a complete project you can clone and run immediately, see the Dart streaming API example on GitHub.

Why Dart for Text to Speech

Dart’s async/await model makes HTTP-based integrations straightforward. The HttpClient class streams response bytes natively, so saving audio files does not require buffering the entire response in memory first. This matters when generating longer audio content.

Where Dart text to speech fits in practice:

  • Dart CLI tools that batch-convert localisation strings into audio for QA review
  • Dart scripts in CI pipelines that generate audio assets during the build step
  • Flutter apps that call the API directly — store the key encrypted rather than in plain text, since Flutter apps can be decompiled

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

Flutter Text to Speech

If you are building a Flutter app, the Dart code above works directly in your app. Store the API key encrypted rather than in plain text — Flutter apps can be decompiled and secrets extracted from the binary.

For Flutter-specific HTTP calls, you can substitute the http package (package:http) for dart:io if your project already depends on it. The API request is identical — POST text, receive audio bytes.

Controlling Voice and Audio Output

The endpoint URL determines the audio format, and query parameters control the voice. Mix and match these to get the output you need.

Audio format — change the path segment in the URL:

  • /text-to-speech/mp3 — compressed, good quality, smallest file
  • /text-to-speech/m4a — compressed, best balance of size and quality
  • /text-to-speech/wav — uncompressed 16-bit PCM, highest quality (requires polling API)

Voice selection — append ?voice=mickey to pick from 900 voices across 100 languages. Browse all options at Text to Speech Voices.

Speed and volume — add &voice-speed=1.2 for 20% faster reading, or &voice-volume=soft for quieter output. Speed values below 1.0 slow the voice down.

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

Dart TTS Without External Packages

A common question is whether a dedicated Dart TTS package exists. With Narakeet, the standard library covers everything — dart:io for HTTP, dart:convert for UTF-8 encoding. There is no pub package to install, no native plugin to compile, and no platform channel to maintain. The API accepts plain text and returns audio bytes over HTTPS, which any Dart HTTP client can handle.