<p>You can insert whatever settings you need either in your config/general.php file, config/pluginhandle.php, or the plugin/config.php file. </p>

<p>You can have different value for your setting in each of the environments you have defined. If you call your setting <code>myplugin_stripe_key</code>, you'd put something like this in your config file:</p>

<pre><code>return array(
    '*' =&gt; array(
      ...
    ),

    'local' =&gt; array(
        'myplugin_stripe_key' =&gt; 'sk_test_xxxxxxxxxxxxxxxxxx',
        ...
    ),

    'stage' =&gt; array(
        'myplugin_stripe_key' =&gt; 'sk_stage_xxxxxxxxxxxxxxxxxx',
        ...
    ),

    'prod' =&gt; array(
        'myplugin_stripe_key' =&gt; 'sk_prod_xxxxxxxxxxxxxxxxxx',
        ...
    )
);
</code></pre>

<p>In your plugin:</p>

<pre><code>private function _saveStripeCustomer($account, $token = NULL)
{
  \Stripe::setApiKey(craft()-&gt;config-&gt;get('myplugin_stripe_key'));
  // ...
}
</code></pre>

<p>If you use this several places in your service, I'd fetch all settings in the constructor and save them in an array:</p>

<pre><code>class MypluginService extends BaseApplicationComponent
{
    var $settings = array();

    public function __construct($urlRecord = null)
    {
        $this-&gt;settings = $this-&gt;_initSettings();
    }

    private function _initSettings()
    {
        $settings = array();
        $settings['myplugin_stripe_key'] = craft()-&gt;config-&gt;get('myplugin_stripe_key');
        return $settings;
    }
}
</code></pre>

<p>Then you can access them later like this:</p>

<pre><code>\Stripe::setApiKey($this-&gt;settings['myplugin_stripe_key']);
</code></pre>

<p>If you need this key in javascript, you could create a craft variable that gets the config setting and output it to a javascript variable somewhere in your twig templates. You'd basically use the same way to get the config variable inside the craft variable method.</p>

CC BY-SA 3.0
2015-02-06T02:56:42.930Z
If you have a sec... I meant to ask, what is `$urlRecord` in `__construct($urlRecord = null)` for?
Douglas McDonald
CC BY-SA 3.0
2015-02-07T20:07:12.507Z
@DouglasMcDonald In this case, nothing. :) Just forgot to remove it. Just something that was left from a previous plugin. The point of sending in a record to the service's constructor, is to be able to write tests for it. But I'm no expert on that, so you should seek advice from someone else if you want to learn more about it. ;)
André Elvan
CC BY-SA 3.0
2015-02-07T20:42:03.250Z
Got it. By the way, this all works great. Thanks again, André.
Douglas McDonald
CC BY-SA 3.0
2014-10-09T07:57:02.230Z
2014-10-09T07:57:02.230Z
André Elvan
<p>What is the recommended way to configure public api keys for multi-environment configurations (i.e. local, dev, stage, production)? Is there a way to set these in either the <code>craft/config/general.php</code> or in <code>plugin/config.php</code> files?</p>

<p>Currently I am having to include these in each of my service methods (and something similar again in my javascript files):</p>

<pre><code>private function _saveStripeCustomer($account, $token = NULL)
{
    \Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxx");
    // ...
}
</code></pre>

<p>I am generally following the configuration method shown <a href="http://straightupcraft.com/configuration" rel="nofollow noreferrer">here</a> (both multi-environment and multi-locale). And a plugin config.php as described <a href="https://craftcms.stackexchange.com/questions/1998/how-does-a-plugin-include-its-own-config-options">here</a>.</p>

<plugin-development><config>
Recommended way to include API keys in a multi-environment configuration?
# Recommended way to include API keys in a multi-environment configuration?

(Asked by: username_0 on 2014-10-09)

What is the recommended way to configure public api keys for multi-environment configurations (i.e. local, dev, stage, production)? Is there a way to set these in either the craft/config/general.php or in plugin/config.php files?
Currently I am having to include these in each of my service methods (and something similar again in my javascript files):
private function _saveStripeCustomer($account, $token = NULL)
{
    \Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxx");
    // ...
}

I am generally following the configuration method shown here (both multi-environment and multi-locale). And a plugin config.php as described here.

**Tags:** plugin-development, config

---

## 1 answers

### Answer by username_1 on 2014-10-09. Score: 5 | Accepted answer

You can insert whatever settings you need either in your config/general.php file, config/pluginhandle.php, or the plugin/config.php file. 
You can have different value for your setting in each of the environments you have defined. If you call your setting myplugin_stripe_key, you'd put something like this in your config file:
return array(
    '*' => array(
      ...
    ),

    'local' => array(
        'myplugin_stripe_key' => 'sk_test_xxxxxxxxxxxxxxxxxx',
        ...
    ),

    'stage' => array(
        'myplugin_stripe_key' => 'sk_stage_xxxxxxxxxxxxxxxxxx',
        ...
    ),

    'prod' => array(
        'myplugin_stripe_key' => 'sk_prod_xxxxxxxxxxxxxxxxxx',
        ...
    )
);

In your plugin:
private function _saveStripeCustomer($account, $token = NULL)
{
  \Stripe::setApiKey(craft()->config->get('myplugin_stripe_key'));
  // ...
}

If you use this several places in your service, I'd fetch all settings in the constructor and save them in an array:
class MypluginService extends BaseApplicationComponent
{
    var $settings = array();

    public function __construct($urlRecord = null)
    {
        $this->settings = $this->_initSettings();
    }

    private function _initSettings()
    {
        $settings = array();
        $settings['myplugin_stripe_key'] = craft()->config->get('myplugin_stripe_key');
        return $settings;
    }
}

Then you can access them later like this:
\Stripe::setApiKey($this->settings['myplugin_stripe_key']);

If you need this key in javascript, you could create a craft variable that gets the config setting and output it to a javascript variable somewhere in your twig templates. You'd basically use the same way to get the config variable inside the craft variable method.

> **Comments:**
> **username_0**: If you have a sec... I meant to ask, what is `$urlRecord` in `__construct($urlRecord = null)` for?  
> **username_1**: @DouglasMcDonald In this case, nothing. :) Just forgot to remove it. Just something that was left from a previous plugin. The point of sending in a record to the service's constructor, is to be able to write tests for it. But I'm no expert on that, so you should seek advice from someone else if you want to learn more about it. ;)  
> **username_0**: Got it. By the way, this all works great. Thanks again, André.  

---

