2024-12-06 Web Development
Setting up Google Cloud Text to Speech App
By O. Wolfson
This article will guide you through the process of setting up a simple TTS web app using Google Cloud Text-to-Speech (TTS) and Next.js. We'll include server-side integration, client-side rendering, and Google Cloud API configuration.
Prerequisites
-
Node.js and npm installed on your system.
-
Google Cloud Project:
- Create a project on the Google Cloud Console.
- Enable the Text-to-Speech API.
- Generate and download a service account key (JSON file).
-
Next.js Project:
-
Initialize a Next.js project:
-
Install necessary dependencies:
-
Step 1: Configure Google Cloud Text-to-Speech API
-
Service Account Key: Place the downloaded service account key file (e.g.,
tts-key.json
) in a secure location in your project directory, such as./keys
. -
Environment Variables: Securely reference your key file using environment variables if deploying:
-
Add the path to
.env.local
: -
Access the variable in the code:
-
Step 2: Build the Server-Side TTS API
We’ll use a server action in Next.js to handle API requests.
-
Create a file at
app/api/tts/route.js
: -
Test the API locally: Start the Next.js development server:
Send a
POST
request to/api/tts
using a tool like Postman or curl with a JSON body:
Step 3: Build the Client-Side UI
Create a React component for interacting with the API.
-
In
app/page.js
:
Step 4: Deploy Your Application
- Deploy your Next.js app to a platform like Vercel or Netlify.
- If using Vercel, ensure you upload the
tts-key.json
file securely or replace it with an environment variable setup.
Summary
With this foundation, you can extend the application by:
- Supporting multiple languages and voices.
- Adding audio file downloads.
- Including user authentication for personalized services.