Skip to content

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.

AI Assistance

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.

Speech and transcription support in the AI Gateway provider is available on the canary releases of the AI SDK. Install them with pnpm add ai@canary @ai-sdk/gateway@canary.

  1. Create a new directory and initialize a Node.js project:

    Terminal
    mkdir ai-speech-demo
    cd ai-speech-demo
    pnpm init
  2. Install the canary AI SDK, the AI Gateway provider, and development dependencies:

    Terminal
    npm install ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescript
    Terminal
    yarn add ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescript
    Terminal
    pnpm add ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescript
    Terminal
    bun add ai@canary @ai-sdk/gateway@canary dotenv @types/node tsx typescript
  3. Go to the AI Gateway API Keys page in your Vercel dashboard and click Create key to generate a new API key.

    Create a .env.local file and save your API key:

    .env.local
    AI_GATEWAY_API_KEY=your_ai_gateway_api_key

    Instead of using an API key, you can use OIDC tokens to authenticate your requests.

  4. Create an index.ts file. It generates speech from a sentence, saves the audio, then transcribes that same audio back to text:

    index.ts
    import {
      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:

    Terminal
    pnpm tsx index.ts

    You get speech.mp3 with the spoken sentence, and the transcript prints back the text you started with.

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.

Last updated June 20, 2026

Was this helpful?

supported.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.