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 variables | Secrets | |
|---|---|---|
| Use for | Configuration: ids, URLs, feature flags | Credentials: API keys, passwords, tokens |
| Stored | As plain text | Encrypted with the secret module key |
| Readable over the API | Yes | Never. Only a salted hash is returned. |
| Visible in the panel | Yes | No |
Exported by spica fetch | Yes, including values | Only 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
| Action | Allows |
|---|---|
env-var:index, env-var:show, env-var:create, env-var:update, env-var:delete | Managing environment variables. |
secret:index, secret:show, secret:create, secret:update, secret:delete | Managing secrets. |
function:env-var:inject, function:env-var:eject | Attaching and detaching environment variables. |
function:secret:inject, function:secret:eject | Attaching and detaching secrets. |