top of page
Writer's pictureSagar Panwar

How to Integrate Google Speech-to-Text API into Your Application

Updated: Aug 5



Overview


Google Cloud Platform (GCP) is a popular service offering tools beyond just deployment and storage, including powerful speech recognition. You can enhance your app with real-time voice recognition by integrating the Google Cloud Speech-to-Text API. This reliable API is perfect for transcription services, voice commands, and language processing, making your app smarter and more user-friendly.


Getting Started


To integrate Google Speech-to-Text API into a web application using JavaScript, you need to follow these steps:


Step 1: Set Up Google Cloud Platform


To set up the speech-to-text we need to get the Google API key first, for that we've to go to Google Cloud Console.



  • Click on the project drop-down and select "New Project".

  • Enter a name for your project and click "Create".




  • Go to the Cloud Speech-to-Text API service page.

  • In the Cloud Console, navigate to APIs & Services > Library.

  • Search for "Speech-to-Text API" and click on it.

  • Click "Enable".


  • Go to APIs & Services > Credentials.

  • Click on "Create Credentials" and select "Service Account".

  • Fill in the required details and click "Create".

  • On the next page, assign the "Project > Editor" role and click "Continue".

  • Click "Done".

  • Click on the created service account and then "Add Key".

  • Select "JSON" and click "Create". A JSON file will be downloaded.



Step 2: Create a Backend Server


Using Node.js and Express


  1. Intialize a node js project

npm init -y
npm install express @google-cloud/speech multer

2. Create the server script (server.js):


const express = require('express');
const multer = require('multer');
const { SpeechClient } = require('@google-cloud/speech');

const fs = require('fs');

const app = express();

const upload = multer({ dest: 'uploads/' });

const client = new SpeechClient({ keyFilename: 'path/to/your/service-account-file.json' });

app.post('/upload', upload.single('audio'), async (req, res) => {

    const filePath = req.file.path;

    const file = fs.readFileSync(filePath);

    const audioBytes = file.toString('base64');

    const audio = { content: audioBytes };

    const config = { encoding: 'LINEAR16', sampleRateHertz: 16000, languageCode: 'en-US' };

    const request = { audio: audio, config: config };

    try {

        const [response] = await client.recognize(request);

        const transcription = response.results.map(result => result.alternatives[0].transcript).join('\n');

        res.send(transcription);

    } catch (error) {

        res.status(500).send(error.toString());

    } finally {

        fs.unlinkSync(filePath);

    }

});

app.listen(3000, () => console.log('Server is running on port 3000'));

3. Save the service account JSON file to your project directory (e.g., service-account-file.json).


4. Run the Server:


node server.js

Step 3: Create the Front-End


  1. Create an HTML file (index.html):

<!DOCTYPE html>
<html>
<head>
 <title>Speech-to-Text</title>
</head>

<body>

    <h1>Google Speech-to-Text Demo</h1>

    <button id="start-recording">Start Recording</button>

    <button id="stop-recording">Stop Recording</button>

    <p id="transcription"></p>

    <script src="https://cdn.jsdelivr.net/npm/recorder-js@latest/dist/recorder.js"></script>

    <script>

        const startButton = document.getElementById('start-recording');

        const stopButton = document.getElementById('stop-recording');

        const transcription = document.getElementById('transcription');

        let recorder;

        startButton.onclick = async () => {

            const stream = await navigator.mediaDevices.getUserMedia({ audio: true });

            recorder = new Recorder(stream);

            recorder.start();

        };

        stopButton.onclick = async () => {

            const { blob } = await recorder.stop();

            const formData = new FormData();

            formData.append('audio', blob, 'audio.wav');

            const response = await fetch('/upload', { method: 'POST', body: formData });

            const text = await response.text();

            transcription.textContent = text;

        };

    </script>

</body>

</html>

2. Serve the HTML file using a simple server like http-server, or integrate it into your existing server setup.


Step 4: Testing


  • Open your browser and navigate to http://localhost:3000.

  • Click "Start Recording" and speak into your microphone.

  • Click "Stop Recording" and wait for the transcription to appear.


Conclusion:


Following these instructions, you may incorporate the Google Speech-to-Text API into your application, considerably increasing its functionality with advanced speech recognition features.


FAQs


Q1: How to secure your backend server?

Limit access to approved clients. Use authentication and permission technologies like API keys, OAuth, and JWT.


Q2: What should I do if I have a CORS issue?

Configure your server with relevant CORS headers. If you are using Express, you can utilize the 'cors' middleware.


Q3: Can I use a different library to record audio?

Yes, you can use any library that meets your audio capture requirements. The example uses 'recorder.js', although other libraries, such as the 'MediaRecorder' API, can also be utilized.


Q4: How should I handle mistakes throughout the speech recognition process?

Set up suitable error handling in your backend server to detect and respond to issues. For example, log problems and deliver user-friendly error messages to clients.


Q5: How can I keep the service account key secure?

Keep the service account key secure and avoid submitting it to version control. Manage sensitive information using environment variables or secure secret management services.


Q6: Can I use this configuration for a production application?

Yes, but prioritize security, scalability, and performance in production environments.



Keep Learning, Keep Exploring…


140 views1 comment

Recent Posts

See All

How to Use Google Books API

Explore integrating the Google Books API into your application with step-by-step examples, detailed code, and expert tips.

1 Comment


This is one of the best article I've ever seen. Thanks for sharing

Like
bottom of page