JmJ
JmJ

Reputation: 2108

Add Google API keyfile to Heroku

I'm trying to host an API on Heroku, everything is going very smooth, but I cannot complete the setup because the Google Cloud Language API SDK requires the path to a file, rather than just the values within the file.

I have successfully set environment variables for all of the services I am using. I have tried to add the contents of the file to an environment variable:

heroku config:set GOOGLE_KEY_FILE={....}

This does not work, as it expects a filepath.

Short of committing the file, which I obviously can't do, how can I get the file into Heroku's file system so that my API can read it?

Or is there another way this can be done, the following runs fine locally when it defaults to a filepath:

const initialiseLanguage = require('@google-cloud/language')
const {
  GOOGLE_PROJECT_ID: projectId,
  GOOGLE_KEY_FILE: keyFilename = './side-project-xxxxxxxxx.json'
} = process.env

const language = initialiseLanguage({
  projectId,
  keyFilename
})

Thanks in advance.

Upvotes: 0

Views: 600

Answers (1)

Felix Fong
Felix Fong

Reputation: 985

In my application uses GCS key file too, and I also set the key file JSON into a environment variable, so that's what I do:

const fs = require('fs');
const path = require('path');
const gTokenPath = path.join(`${__dirname}/gToken.json`);
fs.writeFileSync(gTokenPath , process.env.GCS_JSON_TOKEN);

const gcsKeyFile = JSON.parse(process.env.GCS_JSON_TOKEN);

const language = initialiseLanguage({
  projectId: gcsKeyFile.project_id,
  keyFilename: gTokenPath,
})

Upvotes: 2

Related Questions