Answers.Body: <p>You could try declaring the variable $youtubeApiKey with no value set. Instead of using</p>
  <pre><code>public $youtubeApiKey = '$YOUTUBE_API_KEY';
  </code></pre>
  <p>try using</p>
  <pre><code>public $youtubeApiKey;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-01T15:47:04.280Z
Answers.LastActivityDate: 2021-02-01T15:47:04.280Z
Answers.UserDisplayName: Tim Strawbridge
Body: <p>I’m writing a plugin and need some of its settings to be pulled from the <code>.env</code> file. According to <a href="https://craftcms.com/docs/3.x/extend/environmental-settings.html" rel="nofollow noreferrer">the docs</a> the following should work, but the getter isn’t getting called whenever the plugin’s <code>youtubeApiKey</code> setting is requested; it just keeps returning <code>$YOUTUBE_API_KEY</code>.</p>
  <p>When I set the property visibility to <code>protected</code> instead of <code>public</code>, it <em>does</em> call the getter but also reveals the (sensitive) key in he admin settings panel.</p>
  <pre><code>class Settings extends Model
  {
      public $youtubeApiKey = '$YOUTUBE_API_KEY';
  
      public function getYoutubeApiKey(): string
      {
          return Craft::parseEnv($this-&gt;youtubeApiKey);
      }
  
      public function behaviors()
      {
          return [
              'parser' =&gt; [
                  'class' =&gt; EnvAttributeParserBehavior::class,
                  'attributes' =&gt; ['youtubeApiKey'],
              ],
          ];
      }
  
      public function rules()
      {
          return [
              [['youtubeApiKey'], 'string'],
              [['youtubeApiKey'], 'default', 'value' =&gt; ''],
          ];
      }
  }
  </code></pre>
  <p>I suppose my understanding of the settings getters is incorrect, but what I’m trying to achieve is for <code>MyPlugin::getInstance()-&gt;getSettings()-&gt;youtubeApiKey</code> to return the actual API key (not <code>$YOUTUBE_API_KEY</code>) and for the settings field (below) to display <code>$YOUTUBE_API_KEY</code> (not the actual API key).</p>
  <pre><code>{{ forms.textField({
      'id': 'youtubeApiKey',
      'label': 'YouTube API key',
      'name': 'youtubeApiKey',
      'suggestEnvVars': true,
      'value': settings.youtubeApiKey
  }) }}
  </code></pre>
  
Tags: <plugin-development><craft3><environmentvariables>
Title: Use .env values in Craft 3 plugin settings
ThreadText: # Use .env values in Craft 3 plugin settings
  
  (Asked by: username_0 on 2021-01-31)
  
  I’m writing a plugin and need some of its settings to be pulled from the `.env` file. According to [the docs](https://craftcms.com/docs/3.x/extend/environmental-settings.html) the following should work, but the getter isn’t getting called whenever the plugin’s `youtubeApiKey` setting is requested; it just keeps returning `$YOUTUBE_API_KEY`.
  
  When I set the property visibility to `protected` instead of `public`, it *does* call the getter but also reveals the (sensitive) key in he admin settings panel.
  
  ```
  class Settings extends Model
  {
      public $youtubeApiKey = '$YOUTUBE_API_KEY';
  
      public function getYoutubeApiKey(): string
      {
          return Craft::parseEnv($this->youtubeApiKey);
      }
  
      public function behaviors()
      {
          return [
              'parser' => [
                  'class' => EnvAttributeParserBehavior::class,
                  'attributes' => ['youtubeApiKey'],
              ],
          ];
      }
  
      public function rules()
      {
          return [
              [['youtubeApiKey'], 'string'],
              [['youtubeApiKey'], 'default', 'value' => ''],
          ];
      }
  }
  
  ```
  
  I suppose my understanding of the settings getters is incorrect, but what I’m trying to achieve is for `MyPlugin::getInstance()->getSettings()->youtubeApiKey` to return the actual API key (not `$YOUTUBE_API_KEY`) and for the settings field (below) to display `$YOUTUBE_API_KEY` (not the actual API key).
  
  ```
  {{ forms.textField({
      'id': 'youtubeApiKey',
      'label': 'YouTube API key',
      'name': 'youtubeApiKey',
      'suggestEnvVars': true,
      'value': settings.youtubeApiKey
  }) }}
  
  ```
  
  **Tags:** plugin-development, craft3, environmentvariables
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-02-01. Score: 1
  
  You could try declaring the variable $youtubeApiKey with no value set. Instead of using
  
  ```
  public $youtubeApiKey = '$YOUTUBE_API_KEY';
  
  ```
  
  try using
  
  ```
  public $youtubeApiKey;
  
  ```
  
  
  ---
  
  