Speech to Text and Text to Speech Quickstart
Text to speech and speech to text are two halves of the same workflow: one turns text into spoken audio, the other turns audio back into text. They feed into each other, so this quickstart runs both as a round-trip. You generate speech from a sentence, then transcribe that audio and check the text comes back.
Set up AI Gateway for speech to text and text to speech. First, make sure the Vercel CLI is installed (`npm i -g vercel`). If I'm using Claude Code or Cursor, install the Vercel Plugin (`npx plugins add vercel/vercel-plugin`). For other agents, install Vercel Skills (`npx skills add vercel-labs/agent-skills`). Then: 1. Initialize a Node.js project and install ai@canary, @ai-sdk/gateway@canary, dotenv, @types/node, tsx, and typescript. 2. Save my AI_GATEWAY_API_KEY in .env.local. 3. Create an index.ts that uses the AI SDK experimental_generateSpeech with gateway.speechModel('openai/tts-1') to turn a sentence into an mp3, then experimental_transcribe with gateway.transcriptionModel('openai/whisper-1') to transcribe that same audio and log the text. 4. Run it with tsx.
Create a new directory and initialize a Node.js project:
Terminalmkdir ai-speech-demo cd ai-speech-demo pnpm initInstall the canary AI SDK, the AI Gateway provider, and development dependencies:
Terminalnpm install ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescriptTerminalyarn add ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescriptTerminalpnpm add ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescriptTerminalbun add ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescriptGo to the AI Gateway API Keys page in your Vercel dashboard and click Create key to generate a new API key.
Create a
.env.localfile and save your API key:.env.localAI_GATEWAY_API_KEY=your_ai_gateway_api_keyCreate an
index.tsfile. It generates speech from a sentence, saves the audio, then transcribes that same audio back to text:index.tsimport { experimental_generateSpeech as generateSpeech, experimental_transcribe as transcribe, } from 'ai'; import { gateway } from '@ai-sdk/gateway'; import { writeFile } from 'node:fs/promises'; import 'dotenv/config'; async function main() { const text = 'Thanks for trying out AI Gateway.'; // Text to speech const speech = await generateSpeech({ model: gateway.speechModel('openai/tts-1'), text, voice: 'alloy', outputFormat: 'mp3', }); await writeFile('speech.mp3', speech.audio.uint8Array); console.log('Saved speech.mp3'); // Speech to text: transcribe the audio we just generated const transcript = await transcribe({ model: gateway.transcriptionModel('openai/whisper-1'), audio: speech.audio.uint8Array, }); console.log('Transcript:', transcript.text); } main().catch(console.error);Run your script:
Terminalpnpm tsx index.tsYou get
speech.mp3with the spoken sentence, and the transcript prints back the text you started with.- Read the Text to Speech reference for voices, formats, and the full list of request options
- Read the Speech to Text reference for segments, timestamps, and provider options
- For live, two-way voice conversations, follow the Realtime quickstart
The two calls are independent. Use experimental_generateSpeech alone to add voiceovers or spoken responses, and experimental_transcribe alone to transcribe recordings, voice notes, or call audio. You can also call the REST endpoints directly without the AI SDK; see the Text to Speech and Speech to Text references.
Was this helpful?