I had the same problem to solve. After a bit of digging, I got a bellow solution worked in singe gunfire.
Make sure the required node module is installed :
npm install @google-cloud/vision
const vision = require('@google-cloud/vision');
const {GoogleAuth, grpc} = require('google-gax');
const apiKey = '**********';
function getApiKeyCredentials() {
const sslCreds = grpc.credentials.createSsl();
const googleAuth = new GoogleAuth();
const authClient = googleAuth.fromAPIKey(apiKey);
const credentials = grpc.credentials.combineChannelCredentials(
sslCreds,
grpc.credentials.createFromGoogleCredential(authClient)
);
return credentials;
}
async function main(fileName) {
const sslCreds = getApiKeyCredentials();
const client = new vision.ImageAnnotatorClient({sslCreds});
const [result] = await client.faceDetection(fileName);
const faces = result.faceAnnotations;
//your custom code
}
Code Sample
Question originally answered here
NodeJS Reference for Vision API