Environment variables & secrets

Give functions configuration and credentials through environment variables and encrypted secrets, and reference them in trigger options.

Functions read configuration from process.env. Spica manages two kinds of values:

Environment variablesSecrets
Use forConfiguration: ids, URLs, feature flagsCredentials: API keys, passwords, tokens
StoredAs plain textEncrypted with the secret module key
Readable over the APIYesNever. Only a salted hash is returned.
Visible in the panelYesNo
Exported by spica fetchYes, including valuesOnly the key

Both are created once and then attached to the functions that need them.

Create values

Open Access Management → Secrets and Variables, or the Environment box of a function, and add a key and a value.

Attach values to a function

Terminal
curl -X PUT https://api.example.com/api/function/<functionId>/env-var/<envVarId> \
  -H "Authorization: APIKEY <key>"

curl -X PUT https://api.example.com/api/function/<functionId>/secret/<secretId> \
  -H "Authorization: APIKEY <key>"

Detach them with DELETE on the same URLs. In a function's schema.yaml the attached ids are listed under env_vars and secrets.

Read values in code

JavaScript
export default async function () {
  const bucketId = process.env.POSTS_BUCKET_ID;
  const stripe = new Stripe(process.env.STRIPE_KEY);
}

Workers are restarted with the new values when you change or attach them.

Placeholders in trigger options

Trigger options and the numeric settings timeout, warmWorkers and concurrencyPerWorker can reference attached values:

YAML
timeout: ${ENV.ORDER_TIMEOUT}
triggers:
  processOrder:
    type: rabbitmq
    active: true
    options:
      url: ${SECRET.RABBITMQ_URL}
      queue:
        name: ${ENV.ORDER_QUEUE}
  • ${ENV.KEY} resolves an attached environment variable, ${SECRET.KEY} an attached secret.
  • Only values attached to the function are resolved.
  • Numeric settings must resolve to valid numbers, otherwise the default is used and a warning is logged.

Permissions

ActionAllows
env-var:index, env-var:show, env-var:create, env-var:update, env-var:deleteManaging environment variables.
secret:index, secret:show, secret:create, secret:update, secret:deleteManaging secrets.
function:env-var:inject, function:env-var:ejectAttaching and detaching environment variables.
function:secret:inject, function:secret:ejectAttaching and detaching secrets.