Svelte SDK
@tts2go/svelte provides a store-based TTS API with createTTS and a DOM-based createTTSButton helper.
Installation
npm install @tts2go/svelteSetup
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
| Property | Type | Description |
|---|---|---|
status | Readable<TTSStatus> | Current playback status |
url | Readable<string | null> | CDN URL once generated |
error | Readable<string | null> | Error message if any |
play | () => Promise<void> | Start playback |
stop | () => void | Stop playback |
pause | () => void | Pause playback |
destroy | () => void | Clean 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
| Option | Type | Description |
|---|---|---|
content | string | Text to convert to speech (required) |
voiceId | string | Voice identifier (required) |
container | HTMLElement | DOM element to mount the button into (required) |
TypeScript
import type {
TTSStore,
TTSButtonOptions,
TTS2GoConfig,
TTSStatus,
Voice,
} from "@tts2go/svelte";