Blog / Build with AI /
Voice input in your own tool
Typing on a phone is a chore. Dictating a note is quick. So when you build a tool where people enter text, such as a notes app, a to-do list or a search box, a microphone button can make it feel far more natural.
The good news is that adding voice input can be small. Browsers include speech recognition features, and AI can write the glue code in a few lines. The less good news: it has quirks, and a few privacy points need thought before you ship.
The finished thing, in one line
A microphone button next to a text box: press it, speak, and the words appear in the box for the user to check and edit.
Two ways to do it
1. The browser’s built-in speech recognition
Many browsers offer a speech recognition feature in JavaScript. You start it, the user talks, and you receive text. No extra service, no key, no cost on your side.
The catch is that support varies by browser and device, and how it works behind the scenes is not always visible to you. In some browsers, the audio is sent to a remote service to be turned into text. That is a privacy point to disclose, and I’ll come back to it.
2. A speech-to-text service
You record audio in the page, send it to a service, and receive a transcript. It is more work and may cost money, but you control which service is used, you can usually pick languages and models, and it behaves the same across browsers. Speech-to-text in your own tool goes through that route in more detail, and AI for subtitles and transcripts covers a related use.
For a small tool, start with the browser feature. If you find it unreliable for your audience, move to a service.
Step 1: describe what you want
Add a microphone button next to the text area with id
note. Use the browser’s built-in speech recognition (the Web Speech API). When the user clicks the button, start listening and append recognised text to the text area. Show a clear “Listening…” state and change the button’s label. Clicking again stops it. If the browser does not support speech recognition, hide the button. If microphone permission is denied, show a short friendly message. Use plain JavaScript and add comments.
Look at what the prompt covers: where it attaches, what it does, a visible state, a stop action and two failure cases. The failure cases are the part beginners forget. Making AI features fail gracefully is worth reading with this.
Step 2: what the code looks like
A trimmed example, so you can read the shape of it:
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
const button = document.querySelector('#mic');
const box = document.querySelector('#note');
if (!SpeechRecognition) {
button.hidden = true;
} else {
const recognition = new SpeechRecognition();
recognition.lang = 'en-GB';
recognition.interimResults = false;
let listening = false;
button.addEventListener('click', () => {
if (listening) {
recognition.stop();
} else {
recognition.start();
}
});
recognition.onstart = () => {
listening = true;
button.textContent = 'Stop';
};
recognition.onend = () => {
listening = false;
button.textContent = 'Speak';
};
recognition.onresult = (event) => {
box.value += ' ' + event.results[0][0].transcript;
};
recognition.onerror = () => {
button.textContent = 'Speak';
listening = false;
};
}
This is not production code. It shows the pieces: check support, set the language, start and stop, handle the result and handle errors. Test it in the browsers your users actually use.
Step 3: handle the quirks
Browser support
If the feature is missing, the button should not appear. A button that does nothing is worse than no button. Test on a phone and on a desktop browser; behaviour can differ.
Microphone permission
The browser asks the user for permission, and some people will say no, or will have said no before. Your tool should explain what happened and offer typing as the fallback. Never trap the user.
HTTPS
Microphone access generally works only on secure pages. If your test works locally but fails once deployed, check your site uses HTTPS.
Language and accents
Set the language the user is likely to speak, and consider letting them choose. Accuracy varies by language, accent, background noise and vocabulary. Names and technical terms are often wrong. Always let the user edit the result before anything is saved or sent.
Silence and cut-offs
Recognition may stop on its own after a pause. Decide whether the button should restart it, or the user must tap again. Test with real speech, including pauses.
Punctuation
Don’t expect tidy punctuation. Some systems add it, others don’t. If you need clean text, a follow-up AI step can tidy it, but consider the privacy cost of sending more data around.
Privacy: the part to take seriously
Voice is personal. It can reveal who someone is, where they are and what they say about others. Before shipping:
- Find out where the audio goes. With browser features, check how the browser vendor handles it, and say so in your privacy notice. With a service, read their data terms.
- Tell the user before the microphone starts. A small line near the button is enough: “Voice input uses your browser’s speech recognition and may send audio to your browser provider.”
- Don’t record when you don’t need to. Turn the microphone off the moment they stop.
- Don’t store audio unless you truly must. If you keep transcripts, treat them like any other personal text.
- Make voice optional. Anyone must be able to use the tool without it.
Data privacy in tools you build and privacy-first tools: keeping user data local go deeper. And if you build for businesses that handle confidential material, think hard about whether sending speech to a third party is acceptable at all.
Accessibility: where voice really shines
Voice input can help people who find typing hard because of a disability, an injury or simply a small screen. But it is an addition, not a replacement. Keep all the normal keyboard routes working. Label the button properly for screen readers, with a clear name such as “Start voice input”, not just an icon. Building accessible tools with AI covers the checks.
Where voice input suits and where it doesn’t
Good fits:
- Quick notes and journals (building a daily journal app with AI)
- To-do lists and shopping lists
- Search boxes on mobile
- Long free-text feedback forms
Poor fits:
- Passwords, payment details, anything private
- Noisy places
- Fields that need exact spelling, such as email addresses and codes
If you are unsure, put it where speaking is easier than typing, and nowhere else.
Testing checklist
- Works in the main browsers your users have
- Button hidden when unsupported
- Clear listening state
- Stops when asked
- Permission denial handled
- Typing always works
- Transcript editable
- Privacy note visible
- Tested outdoors or with background noise
Testing your AI-built tool with real users is the best way to find out whether anyone actually wants the microphone.
Key takeaways
- The browser’s speech recognition gives you voice input cheaply, with uneven support.
- Always provide typing as the fallback, and handle denied permission.
- Tell users where their audio goes.
- Let people edit the transcript before it is used.
- Add voice where speaking beats typing.
Related reading
Which text box in your tool do people complain about filling in? Start there.