Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Can the Google Translate API v2 be used in the frontend?

Hello? By any chance, can the Google Translate API v2 be used in the frontend?

Initially, I thought the Google Translate API v2 couldn't be used in the frontend due to CORS issues. However, when I input the code below into the browser, the API is being called normally.

I'm curious if it's okay to call the API directly from the browser from the beginning.

const url = 'https://translation.googleapis.com/language/translate/v2?key={{API_KEY}}';
const headers = {
  'Content-Type': 'application/json; charset=utf-8',
};

fetch(url, {
  method: 'POST',
  headers: headers,
  body: JSON.stringify({
    "q": ["Hello world", "My name is Jeff"],
    "target": "de"
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error(error));

 

0 1 2,476
1 REPLY 1

kvandres
Former Googler

Good day @cress00,

Welcome to Google Cloud Community!

As stated in the Cloud Translation API Documentation, cloud translation API can be integrated into your website or application, so this is correct. If there are CORS issues encountered, this might be an authentication problem. You can check this documentation for more information: https://cloud.google.com/translate/docs/reference/rest

Hope this helps!