Skip to content

Instantly share code, notes, and snippets.

@macdonst
Created September 4, 2018 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macdonst/d66f4d085e8171ea86d986f7b19ff5d5 to your computer and use it in GitHub Desktop.
Save macdonst/d66f4d085e8171ea86d986f7b19ff5d5 to your computer and use it in GitHub Desktop.
MS Azure Function to call Google Translate
const fetch = require('node-fetch');
const credentials = require('./credentials');
module.exports = async function(context, req) {
if (req.query.searchTerm || (req.body && req.body.searchTerm)) {
const searchTerm = req.query.searchTerm ||
req.body.searchTerm;
let url = `https://www.googleapis.com/language/translate/v2?key=${
credentials.google
}&source=en&target=fr&q=${searchTerm}`;
await fetch(url)
.then(res => res.json())
.then(json => {
context.res = {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: json
};
context.done();
});
} else {
context.res = {
status: 400,
body: 'Please pass a name on the query string or in the request body'
};
}
context.done();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment