From 7eec1fe294cfd2a6b37cbd7bd92abd12bd110558 Mon Sep 17 00:00:00 2001 From: luke-speechify <289678208+luke-speechify@users.noreply.github.com> Date: Fri, 21 Aug 2026 00:36:15 +0100 Subject: [PATCH 1/2] feat: add react-tts-component demo A drop-in <100-line React component (SpeechifyVoice) that speaks any text via the Speechify API, with the key held server-side in a route handler. Hosted under /react-tts-component, gated with the shared Turnstile helper. --- README.md | 1 + demos/react-tts-component/.env.example | 1 + demos/react-tts-component/.gitignore | 5 + demos/react-tts-component/README.md | 62 +++++++++ .../app/api/speak/route.ts | 34 +++++ demos/react-tts-component/app/globals.css | 121 ++++++++++++++++++ demos/react-tts-component/app/layout.tsx | 21 +++ .../react-tts-component/app/lib/turnstile.ts | 37 ++++++ demos/react-tts-component/app/page.tsx | 94 ++++++++++++++ .../components/SpeechifyVoice.tsx | 78 +++++++++++ demos/react-tts-component/demo.json | 6 + demos/react-tts-component/next.config.ts | 13 ++ demos/react-tts-component/package.json | 26 ++++ demos/react-tts-component/tsconfig.json | 41 ++++++ pnpm-lock.yaml | 28 ++++ pnpm-workspace.yaml | 1 + site/public/index.html | 4 +- vercel.json | 8 ++ 18 files changed, 579 insertions(+), 2 deletions(-) create mode 100644 demos/react-tts-component/.env.example create mode 100644 demos/react-tts-component/.gitignore create mode 100644 demos/react-tts-component/README.md create mode 100644 demos/react-tts-component/app/api/speak/route.ts create mode 100644 demos/react-tts-component/app/globals.css create mode 100644 demos/react-tts-component/app/layout.tsx create mode 100644 demos/react-tts-component/app/lib/turnstile.ts create mode 100644 demos/react-tts-component/app/page.tsx create mode 100644 demos/react-tts-component/components/SpeechifyVoice.tsx create mode 100644 demos/react-tts-component/demo.json create mode 100644 demos/react-tts-component/next.config.ts create mode 100644 demos/react-tts-component/package.json create mode 100644 demos/react-tts-component/tsconfig.json diff --git a/README.md b/README.md index b23a0c8..f0d9c7b 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Demos with a **Live** link run in your browser at [demos.speechify.ai](https://d | [`demos/mastra-agent-speechify/`](./demos/mastra-agent-speechify) | TypeScript (Mastra) | | Text-in, speech-out Mastra Agent using an OpenAI LLM for replies and Speechify's simba-3.2 model for TTS via `@mastra/voice-speechify`. | | [`demos/voice-agent-showcase/`](./demos/voice-agent-showcase) | Cloudflare Workers | | One page, ten live Voice Agents API demos: calendar booking, policy-bound support, a page copilot, form intake, US outbound calls with a 5-minute cap, a voice gallery, mid-call language handoff, cross-call memory, a grounded knowledge base, and dual-control troubleshooting. | | [`demos/vercel-ai-sdk/`](./demos/vercel-ai-sdk) | TypeScript (Vercel AI SDK) | | Speechify TTS through the Vercel AI SDK's unified `generateSpeech` interface via the official `@speechify/vercel` provider — one-line swap from OpenAI/ElevenLabs, plus word-level speech marks from `providerMetadata`. | +| [`demos/react-tts-component/`](./demos/react-tts-component) | Next.js | [Open](https://demos.speechify.ai/react-tts-component) | A drop-in <100-line React component that speaks any text. Type, hit play, hear it — the API key stays server-side in a route handler. | ## Get an API key diff --git a/demos/react-tts-component/.env.example b/demos/react-tts-component/.env.example new file mode 100644 index 0000000..534cec4 --- /dev/null +++ b/demos/react-tts-component/.env.example @@ -0,0 +1 @@ +SPEECHIFY_API_KEY=your_api_key_here diff --git a/demos/react-tts-component/.gitignore b/demos/react-tts-component/.gitignore new file mode 100644 index 0000000..95d1bcb --- /dev/null +++ b/demos/react-tts-component/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +.next/ +.env +next-env.d.ts +*.tsbuildinfo diff --git a/demos/react-tts-component/README.md b/demos/react-tts-component/README.md new file mode 100644 index 0000000..ad8b273 --- /dev/null +++ b/demos/react-tts-component/README.md @@ -0,0 +1,62 @@ +# Voice in a React app + +A drop-in React component that adds a Speechify voice to any app. Give it text, +it plays that text as speech. The Speechify API key never reaches the browser — +synthesis goes through a one-route server proxy. + +Pairs with the Speechify post *Adding a voice to a React app with the Speechify +SDK*. It complements (doesn't repeat) +[Building an AI Voice Cloning Web App with Next.js and Speechify](https://speechify.ai/blog/building-an-ai-voice-cloning-web-app-with-nextjs-and-speechify) +— read that one for the full cloning app. + +## What you get + +- **[`components/SpeechifyVoice.tsx`](./components/SpeechifyVoice.tsx)** — the + whole point. Under 100 lines. Props: `text`, optional `voiceId`, `endpoint`, + `label`, and a `getToken` hook for abuse-gated deployments. Copy it into your + own app. +- **[`app/api/speak/route.ts`](./app/api/speak/route.ts)** — a Next.js route + handler that calls `client.audio.speech(...)` server-side and returns base64 + MP3, so `SPEECHIFY_API_KEY` stays on the server. +- A small page (`app/page.tsx`) that wires the component to a textarea and a + voice picker. + +## Run it yourself + +```bash +cp .env.example .env # paste your Speechify API key +pnpm install +pnpm dev # http://localhost:8767/react-tts-component +``` + +Get an API key at [platform.speechify.ai/api-keys](https://platform.speechify.ai/api-keys). + +## Use the component in your app + +```tsx +import { SpeechifyVoice } from "./components/SpeechifyVoice"; + +; +``` + +The component POSTs `{ text, voiceId }` to `endpoint` (default `/api/speak`), +expects `{ audio }` (base64 MP3) back, and plays it. Point `endpoint` at your +own proxy route in any framework — the component doesn't care what's behind it. + +## Where the code came from + +Built on the [`@speechify/api`](https://www.npmjs.com/package/@speechify/api) +TTS client — one `client.audio.speech({ input, voice_id, audio_format, model })` +call. Model `simba-3.2`, MP3 output. Browse voices at +[platform.speechify.ai](https://platform.speechify.ai). + +## Abuse protection (hosted) + +The hosted build gates `/api/speak` with Cloudflare Turnstile via the shared +[`app/lib/turnstile.ts`](./app/lib/turnstile.ts) helper. It fail-opens when +`TURNSTILE_SECRET_KEY` is unset, so local dev and forks work with zero config. + +## Prerequisites + +- Node 20+. +- A Speechify API key (the free tier covers this demo). diff --git a/demos/react-tts-component/app/api/speak/route.ts b/demos/react-tts-component/app/api/speak/route.ts new file mode 100644 index 0000000..7148bc4 --- /dev/null +++ b/demos/react-tts-component/app/api/speak/route.ts @@ -0,0 +1,34 @@ +import { NextResponse } from "next/server"; +import { SpeechifyClient, SpeechifyError } from "@speechify/api"; +import { verifyTurnstile } from "../../lib/turnstile"; + +export const runtime = "nodejs"; + +const client = new SpeechifyClient({ token: process.env.SPEECHIFY_API_KEY }); + +export async function POST(req: Request) { + if (!(await verifyTurnstile(req))) { + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); + } + + const { text, voiceId } = await req.json(); + + if (typeof text !== "string" || !text.trim()) { + return NextResponse.json({ error: "text is required" }, { status: 400 }); + } + + try { + const speech = await client.audio.speech({ + input: text.slice(0, 2000), + voice_id: typeof voiceId === "string" && voiceId ? voiceId : "geffen_32", + audio_format: "mp3", + model: "simba-3.2", + }); + return NextResponse.json({ audio: speech.audio_data }); + } catch (err) { + if (err instanceof SpeechifyError) { + return NextResponse.json({ error: err.message }, { status: err.statusCode ?? 500 }); + } + throw err; + } +} diff --git a/demos/react-tts-component/app/globals.css b/demos/react-tts-component/app/globals.css new file mode 100644 index 0000000..3268cf2 --- /dev/null +++ b/demos/react-tts-component/app/globals.css @@ -0,0 +1,121 @@ +:root { + color-scheme: light dark; + --fg: #111; + --bg: #fff; + --muted: #666; + --border: #ddd; + --surface: #f6f6f6; + --accent: #111; + --accent-fg: #fff; +} + +@media (prefers-color-scheme: dark) { + :root { + --fg: #f2f2f2; + --bg: #0c0c0c; + --muted: #999; + --border: #2a2a2a; + --surface: #161616; + --accent: #f2f2f2; + --accent-fg: #0c0c0c; + } +} + +* { + box-sizing: border-box; +} + +body { + margin: 0; + padding: 2rem 1rem; + background: var(--bg); + color: var(--fg); + font-family: ui-sans-serif, system-ui, -apple-system, sans-serif; + line-height: 1.5; +} + +main { + max-width: 34rem; + margin: 0 auto; + display: flex; + flex-direction: column; + gap: 1.5rem; +} + +h1 { + font-size: 1.6rem; + font-weight: 500; + margin: 0 0 0.5rem; +} + +.lead { + color: var(--muted); + margin: 0; +} + +code { + font-family: ui-monospace, SFMono-Regular, Menlo, monospace; + background: var(--surface); + padding: 0.1em 0.35em; + border-radius: 4px; + font-size: 0.9em; +} + +.step { + border: 1px solid var(--border); + border-radius: 0.6rem; + padding: 1.1rem; + background: var(--surface); +} + +.step h2 { + font-size: 0.72rem; + text-transform: uppercase; + letter-spacing: 0.12em; + color: var(--muted); + margin: 0 0 0.75rem; +} + +textarea, +select { + width: 100%; + font: inherit; + color: var(--fg); + background: var(--bg); + border: 1px solid var(--border); + border-radius: 0.4rem; + padding: 0.6rem 0.7rem; + resize: vertical; +} + +.play { + display: flex; + justify-content: center; +} + +.sv-btn { + font: inherit; + font-weight: 500; + cursor: pointer; + color: var(--accent-fg); + background: var(--accent); + border: none; + border-radius: 999px; + padding: 0.7rem 2rem; +} + +.sv-btn:disabled { + opacity: 0.6; + cursor: default; +} + +footer { + color: var(--muted); + font-size: 0.9rem; + border-top: 1px solid var(--border); + padding-top: 1rem; +} + +footer a { + color: inherit; +} diff --git a/demos/react-tts-component/app/layout.tsx b/demos/react-tts-component/app/layout.tsx new file mode 100644 index 0000000..c099c8e --- /dev/null +++ b/demos/react-tts-component/app/layout.tsx @@ -0,0 +1,21 @@ +import type { Metadata } from "next"; +import type { ReactNode } from "react"; +import Script from "next/script"; +import "./globals.css"; + +export const metadata: Metadata = { + title: "Voice in a React app with Speechify", + description: + "A drop-in React component that speaks any text with the Speechify API, key held server-side.", +}; + +export default function RootLayout({ children }: { children: ReactNode }) { + return ( + + + + @@ -368,7 +368,7 @@

FAQ