Svelte SDK

@tts2go/svelte provides a store-based TTS API with createTTS and a DOM-based createTTSButton helper.

Installation

npm install @tts2go/svelte

Setup

Create a TTS2Go client instance, then use it to create TTS stores:

import { createTTS2GoClient, createTTS } from "@tts2go/svelte";

const client = createTTS2GoClient({
  apiKey: "tts_your_api_key",
  projectId: "your-project-id",
});

createTTS Store

Create a reactive TTS store bound to specific content and voice:

<script>
  import { createTTS2GoClient, createTTS } from "@tts2go/svelte";

  const client = createTTS2GoClient({
    apiKey: "tts_your_api_key",
    projectId: "your-project-id",
  });

  const text = "Hello from Svelte!";
  const tts = createTTS(client, text, "voice_abc123");

  // tts.status, tts.url, tts.error are Svelte readable stores
  // tts.play, tts.stop, tts.pause, tts.destroy are methods
</script>

<p>{text}</p>
<p>Status: {$tts.status}</p>

{#if $tts.error}
  <p style="color: red">{$tts.error}</p>
{/if}

<button on:click={tts.play} disabled={$tts.status === "loading"}>
  Play
</button>
<button on:click={tts.pause}>Pause</button>
<button on:click={tts.stop}>Stop</button>

Store properties

PropertyTypeDescription
statusReadable<TTSStatus>Current playback status
urlReadable<string | null>CDN URL once generated
errorReadable<string | null>Error message if any
play() => Promise<void>Start playback
stop() => voidStop playback
pause() => voidPause playback
destroy() => voidClean up resources

createTTSButton

For quick DOM integration, createTTSButton creates a self-contained button element:

import { createTTS2GoClient, createTTSButton } from "@tts2go/svelte";

const client = createTTS2GoClient({
  apiKey: "tts_your_api_key",
  projectId: "your-project-id",
});

// Mounts a TTS button into a container element
const { destroy } = createTTSButton(client, {
  content: "Text to speak",
  voiceId: "voice_abc123",
  container: document.getElementById("tts-container"),
});

// Clean up when done
// destroy();

Options

OptionTypeDescription
contentstringText to convert to speech (required)
voiceIdstringVoice identifier (required)
containerHTMLElementDOM element to mount the button into (required)

TypeScript

import type {
  TTSStore,
  TTSButtonOptions,
  TTS2GoConfig,
  TTSStatus,
  Voice,
} from "@tts2go/svelte";