Cloudflare
The CloudflareVoice class in Mastra provides speech-to-text capabilities using Cloudflare Workers AI Whisper models. It can call Workers AI through the REST API or through a Workers AI binding when running inside a Cloudflare Worker.
Usage exampleDirect link to Usage example
import { createReadStream } from 'node:fs'
import { CloudflareVoice } from '@mastra/voice-cloudflare'
// Initialize with an API token
const voice = new CloudflareVoice({
listeningModel: {
apiKey: 'your-cloudflare-api-token',
},
})
// Convert speech to text. Model and account ID are per-call options.
const text = await voice.listen(createReadStream('audio.mp3'), {
model: '@cf/openai/whisper-large-v3-turbo',
account_id: 'your-cloudflare-account-id',
})
Inside a Cloudflare Worker, pass the Workers AI binding instead of an API token:
const voice = new CloudflareVoice({ binding: env.AI })
ConfigurationDirect link to Configuration
Constructor optionsDirect link to Constructor options
listeningModel?:
CloudflareListenOptions
Configuration for speech-to-text.
CloudflareListenOptions
model?:
'@cf/openai/whisper-tiny-en' | '@cf/openai/whisper' | '@cf/openai/whisper-large-v3-turbo'
Stored as the voice model name, but not used by
listen(). Pass model in the listen() options to choose a model.apiKey?:
string
Cloudflare API token with Workers AI access. Falls back to the CLOUDFLARE_AI_API_KEY environment variable. Required when no binding is provided.
account_id?:
string
Ignored by the constructor. Pass
account_id in the listen() options, or set the CLOUDFLARE_ACCOUNT_ID environment variable.binding?:
Ai
Workers AI binding. When set, requests use the binding instead of the REST API and no API token is needed.
MethodsDirect link to Methods
listen()Direct link to listen
Converts speech to text using a Cloudflare Workers AI Whisper model.
audioStream:
NodeJS.ReadableStream
Audio stream to transcribe.
options?:
CloudflareListenOptions
Per-call
model (defaults to @cf/openai/whisper-large-v3-turbo) and account_id (defaults to CLOUDFLARE_ACCOUNT_ID). These are the only place the model and account ID are read.Returns: Promise<string>
NotesDirect link to Notes
- Without a binding, the constructor throws if no API token is provided and CLOUDFLARE_AI_API_KEY isn't set
- This provider only supports speech-to-text (STT).
speak()andgetSpeakers()throw a "not yet implemented" error