Skip to content

Hosting

Environment variables work out of the box on most hosts. Pantheon requires an extra step because its Secrets Manager does not expose secrets via getenv().

POSSE Post reads credentials with getenv() and does not load .env files itself. If a variable is not set in PHP's environment, the account edit page shows it as missing.

DDEV

Add credentials to .ddev/.env, which DDEV passes to the web container, then restart DDEV:

ddev dotenv set .ddev/.env --mastodon-access-token="your-token"
ddev restart
ddev exec printenv MASTODON_ACCESS_TOKEN

A .env file in the project root is not loaded unless you add a loader (see below).

Loading a .env file from settings.php

To keep credentials in a .env file at the project root on any host, add this to settings.php. It runs for web requests, Drush, and cron, and never overrides a variable that is already set.

$env_file = dirname(DRUPAL_ROOT) . '/.env';
if (is_readable($env_file)) {
  foreach (file($env_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
    $line = trim($line);
    if ($line === '' || str_starts_with($line, '#') || !str_contains($line, '=')) {
      continue;
    }
    [$key, $value] = array_map('trim', explode('=', $line, 2));
    if ($key !== '' && getenv($key) === FALSE) {
      putenv("$key=$value");
    }
  }
}

Keep .env out of git and out of the web root.

Tugboat

Add credentials as custom environment variables on the Repository Settings page in the Tugboat Dashboard. Tugboat recommends the dashboard for secrets so they are not committed to git.

Pantheon

POSSE Post reads credentials from environment variables using getenv(). On Pantheon, the Secrets Manager stores secrets as Runtime secrets accessible only via pantheon_get_secret() — not via getenv() — so a one-time bridge in settings.php is required.

Secret type and scope

When adding secrets via Terminus, use:

  • Type: runtime
  • Scope: web

The env type only applies to Integrated Composer builds, not the web runtime. The web scope makes secrets readable by the running application.

Install the Terminus Secrets Manager plugin

terminus self:plugin:install pantheon-systems/terminus-secrets-manager-plugin

Bridge secrets to environment variables

Add the following to your settings.php. It calls pantheon_get_secret() for each secret and makes the value available to getenv() for the rest of the request. The function_exists() check ensures this code is a no-op on DDEV, Tugboat, and any other environment that sets env vars directly.

// On Pantheon, the Secrets Manager stores secrets as Runtime secrets
// accessible only via pantheon_get_secret() — not via getenv().
if (function_exists('pantheon_get_secret')) {
  $posse_secrets = [
    'SEND_CROSSPOSTS',
    'MASTODON_ACCESS_TOKEN',
    'BLUESKY_APP_PASSWORD',
    'FACEBOOK_PAGE_ACCESS_TOKEN',
    'INSTAGRAM_APP_SECRET',
    'INSTAGRAM_ACCESS_TOKEN',
    'LINKEDIN_CLIENT_SECRET',
    'LINKEDIN_ACCESS_TOKEN',
  ];
  foreach ($posse_secrets as $name) {
    $value = pantheon_get_secret($name);
    if ($value !== NULL) {
      putenv("$name=$value");
    }
  }
}

Only include the variable names for the platforms you actually use.

Adding secrets via Terminus

Secrets must be created site-wide first, then overridden per environment if needed. The site-wide command establishes the default; the .env suffix sets an environment-specific override.

$PANTHEON_SITE=your-site

# Create each secret site-wide first (required before environment overrides work)
terminus secret:site:set $PANTHEON_SITE SEND_CROSSPOSTS 1 --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE MASTODON_ACCESS_TOKEN "your-token" --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE BLUESKY_APP_PASSWORD "your-password" --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE FACEBOOK_PAGE_ACCESS_TOKEN "your-token" --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE INSTAGRAM_APP_SECRET "your-secret" --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE INSTAGRAM_ACCESS_TOKEN "your-token" --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE LINKEDIN_CLIENT_SECRET "your-secret" --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE LINKEDIN_ACCESS_TOKEN "your-token" --type=runtime --scope=web
terminus secret:site:set $PANTHEON_SITE LINKEDIN_ACCESS_TOKEN_TEST "your-token" --type=runtime --scope=web

Controlling which environment sends crossposts

SEND_CROSSPOSTS is the on/off switch. If it is absent or falsy, POSSE Post will not queue or send any crossposts regardless of what other secrets are present.

After creating the site-wide default above, disable sending on dev and test so crossposts only go out from live:

terminus secret:site:set $PANTHEON_SITE.dev SEND_CROSSPOSTS 0
terminus secret:site:set $PANTHEON_SITE.test SEND_CROSSPOSTS 0

Note that Pantheon caches secret values for up to 15 minutes. After setting or changing a secret, wait 15 minutes before expecting the new value to take effect — clearing Drupal's cache does not flush the secrets cache.

To verify what secrets are set on a specific environment:

terminus secret:site:list $PANTHEON_SITE