Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass API keys in environment variables to Ember CLI using process.env?

How do I pass environment variables from bashrc to Ember CLI. I imagine a situation where you need stripe api keys or pusher api-keys and you have them in your environment variables in bashrc. How do you pass the api-keys to Ember CLI.

I tried using Node.js process.env in both the brocfile.js and environment.js, but when I try to access it in the Ember JS controller, the property is null.

In my environment.js file I added,

APP: { apiKey: process.env.KEY } 

In My Ember JS controller I tried accessing it with:

import config from  '../config/environment';  

And setting the controller property lkey as shown below, which didn't work:

lkey: config.App.KEY 

Next in my brocfile.js, I added:

var limaKey = process.env.Key;  var app = new EmberApp({key: limaKey}); 

This still didn't work.

like image 399
brg Avatar asked Oct 16 '14 11:10

brg


People also ask

Are environment variables safe for API keys?

Environment variables are excellent places to store sensitive information (such as API keys), or information that needs to be globally accessible from anywhere in the app, (such as version numbers or hardcoded paths).

What is ENV in API?

Environment variables are character strings of the form "name=value". There are two types of environment variables: Job-level environment variables. The job-level environment variables are stored in an environment space outside of the program associated with the job.

What is Ember CLI?

Ember CLI, Ember's command line interface, provides a standard project structure, a set of development tools, and an addon system. This allows Ember developers to focus on building apps rather than building the support structures that make them run.


2 Answers

I finally resolved this issue. I was faced with two options. Option 1 was to use XHR to fetch the api-keys from an end-point on the server. Option 2 is get the api-key directly from environment variables using Nodejs process.env. I prefer option 2 because it saves me from doing XHR request.

You can get option 2 by using this ember-cli-addOn which depends on Nodejs Dotenv project

  • https://github.com/fivetanley/ember-cli-dotenv
  • https://github.com/motdotla/dotenv

In my case I choose to do it without any addOn.

  1. First add the api-key to your .bashrc if you are Ubuntu or the approapriate place for your own linux distro.
export API_KEY=NwPyhL5 
  1. Reload the .bashrc file, so your setting are picked up:
source ~/.bashrc 
  1. In Ember CLI add a property to the ENV object in config/environment.js. The default looks like this
module.exports = function(environment) {   var ENV = {      modulePrefix: 'rails-em-cli',      environment: environment,      baseURL: '/',      locationType: 'auto',      EmberENV: {        }    } 

Now to that ENV object, we can add a new property myApiKey like this:

module.exports = function(environment) {   var ENV = {     modulePrefix: 'rails-em-cli',     environment: environment,     baseURL: '/',     locationType: 'auto',     myApikey: null,     EmberENV: {       }     //assign a value to the myApiKey       if (environment === 'development') {         // ENV.APP.LOG_RESOLVER = true;          ENV.myApiKey = process.env.API_KEY;       }                   } 

Note that process.env.API_KEY is fetching the setting we added to .bashrc and assigning it to myApiKey. You will need to have Nodejs installed on your server for process.env to work.

Finally to access that variable in your controller you do

import config from '../config/environment'; import Ember from 'ember';  export default Ember.Controller.extend({    yourKey: config.myApikey,  }); 

That's it.

like image 176
brg Avatar answered Oct 06 '22 08:10

brg


You can also set the variables on the ENV.APP object: they will be carried by the application instance.

You can then reuse them inside initializer & so on.

This way, you won't have to import config/environment into application's code, which seems a little weird to me.

like image 32
Mike Aski Avatar answered Oct 06 '22 08:10

Mike Aski