Skip to content

Variables

Forge Variables are key-value pairs that store environment-specific configuration for your app — such as external URLs, feature flags, or display settings. Variables are managed separately from the app manifest and can be updated without redeploying.


Overview

Each app has two independent variable sets: one for dev and one for live. This allows you to use different endpoints or settings per environment without changing your code.

Secrets are encrypted and write-only

A variable that holds a key, token or password can be stored as a secret. Secret values are encrypted, never returned by any read, and reach your code only as $variables at run time. Every other variable is stored and returned as plain text, so use secret:: or secretgen:: for anything sensitive.

Variables are:

  • Not part of the manifest — they are managed through the API or SDK, not in ptkl.config.js
  • Per-environment — dev and live have completely separate values
  • Injected at runtime — available as a global inside services and lifecycle scripts

Secret Variables

Variables that must not be readable, such as a merchant's API key, an SMTP password or a session key, are written with a prefix. The prefix is part of the value you send:

You write Meaning Reads back as
secret::<value> Stores <value> as a secret for the environment of the request, replacing the value there. secretref::
secretgen::random(n[, alnum\|alpha\|hex\|digits\|base64url]) Generated by the platform, only if the key has no secret yet. An existing value is kept. secretref::
secretref:: Keeps the secret already stored at this key. secretref::
literal::<value> A plain value that starts with a reserved prefix. literal::<value> when the value starts with a reserved prefix
curl -X PATCH https://api.ptkl.app/v1/forge/variables \
  -H "X-Project-Env: dev" \
  -H "Authorization: Bearer <app-token>" \
  -H "Content-Type: application/json" \
  -d '{
    "WEBHOOK_URL": "https://staging.example.com/hook",
    "STRIPE_KEY": "secret::sk_test_x1234567",
    "SESSION_KEY": "secretgen::random(64)"
  }'

Reading the variables back returns secretref:: for both secrets and the plain value as it is:

{
    "WEBHOOK_URL": "https://staging.example.com/hook",
    "STRIPE_KEY": "secretref::",
    "SESSION_KEY": "secretref::"
}

Secrets work at any depth, inside nested objects and arrays, and they are set per environment: dev and live hold separate values, and a secretref:: cannot keep the other environment's value.

Write-only

No route returns a secret value, for any caller, including the app's own token. Every read of the variables, in the API, the SDK and the dashboard, shows secretref:: in its place. Only your services and lifecycle scripts receive the real value, through $variables.

If you forget a value, replace it with secret::<new value>, or regenerate it by removing the key and sending secretgen:: again.

The dashboard shows a stored secret as "Set, hidden" with the hint "Cannot be read back. Replace it or regenerate it." Changing a variable needs manage forge, or manage_variables forge for the app's own variables, and the dashboard says so when you lack it. See the security model for what you are responsible for and what the platform guarantees: anyone who can deploy the app's code can read the secrets it receives.

Keeping a secret when you write

Because PATCH replaces the whole environment, a write that leaves a secret key out removes it. To change other variables and keep a secret, send its secretref:: back for that key:

{
    "WEBHOOK_URL": "https://example.com/hook",
    "STRIPE_KEY": "secretref::",
    "SESSION_KEY": "secretref::"
}

Sending the set you just read, unchanged, therefore keeps every secret. secretref:: for a key with no stored secret is refused with 422.

Generated secrets

secretgen:: creates a value once and keeps it afterwards. It is not regenerated when you send it again, when you reinstall the app (?reinstall=<version>) or when you upgrade it. The generator is the same as for component secrets: n is at most 4096, and a generator that gives less than 128 bits of entropy is refused with 422. uuid is not a generator.

Plain values that start with a reserved prefix

secret::, secretgen::, secretref::, literal:: and enc:: are reserved. To store a plain value that begins with one of them, prefix it with literal:::

{ "NOTE": "literal::secret::this is not a secret" }

The value reads back as literal::secret::this is not a secret, so sending a read back unchanged stores the same value and never triggers generation. A value starting with enc:: cannot be stored as plain text, with or without literal::; it is refused with 422.

Limits

Limit Value
A secret:: value 8 characters to 32 KiB
One environment's variables 64 KiB, measured as stored after encryption
Nesting 32 levels and 10 000 values per environment

A secret value outside its limits is refused with 422. A write that takes an environment over 64 KiB gets 413 and changes nothing, and a PATCH body over 64 KiB is refused with 413. Variables that were stored before these limits keep working, but your next write to them must fit.

Where secrets come from at install

  • Marketplace installs can supply secret:: and secretgen:: values in the install payload, per environment. Installing an app that already exists ignores the incoming variables, except secretgen:: keys that have no value yet, which are generated. Change the variables of an installed app through the variables API.
  • Templates can carry plain values and secretgen:: generators, and the value is generated once per environment when the template installs. A template cannot carry secret:: values, secretref:: or enc::: it is refused when it is published and again when it is installed or updated.

For secrets that belong to a component's functions rather than to the app, see component secrets.


Managing Variables

Your own variables

An app reads its own variables with no argument and no permission — every app can always do this. The platform resolves which app is asking from the app's own token, so there is nothing to name and nothing to grant.

Requires @ptkl/sdk/beta

The no-argument forms are v0.10 and up. The @ptkl/sdk entry point resolves to v0.9, which only has the forms that take an app ref.

import { Platform } from '@ptkl/sdk/beta'

const platform = new Platform()

// Read your own variables for the current environment — no permission needed
const vars = await platform.forge().getVariables()

// Replace your own variables — requires `manage_variables forge`
await platform.forge().updateVariables({
    WEBHOOK_URL: 'https://example.com/hook',
    MAX_RETRIES: 3,
    FEATURE_V2_ENABLED: true,
})

// Add or update a single one of your own variables
await platform.forge().addVariable('WEBHOOK_URL', 'https://example.com/hook')

To write its own variables an app declares manage_variables forge in its manifest — as an entitlement if it should always be able to manage its own configuration regardless of who launched it:

entitlements: ['manage_variables forge'],

That permission is self-scoped by construction: it is only honoured on the pathless route, which cannot name another app. Granting it never exposes any other app's variables.

Another app's variables

Pass an app ref to read or write a different app. This requires manage forge, which is a project-wide administrative permission — it grants read and write over every app's variables, so do not declare it just to reach your own.

const vars = await platform.forge().getVariables('my-app')

await platform.forge().updateVariables('my-app', { WEBHOOK_URL: '...' })

await platform.forge().addVariable('my-app', 'WEBHOOK_URL', '...')

Permissions at a glance

Call Permission required
Read your own variables none
Write your own variables manage_variables forge
Read or write another app's variables manage forge

Using the API

# Read your own variables (dev environment) — no permission required
curl https://api.ptkl.app/v1/forge/variables \
  -H "X-Project-Env: dev" \
  -H "Authorization: Bearer <app-token>"

# Update your own variables — requires `manage_variables forge`
curl -X PATCH https://api.ptkl.app/v1/forge/variables \
  -H "X-Project-Env: dev" \
  -H "Authorization: Bearer <app-token>" \
  -H "Content-Type: application/json" \
  -d '{"WEBHOOK_URL": "https://staging.example.com/hook", "MAX_RETRIES": 3}'

# Read another app's variables — requires `manage forge`
curl https://api.ptkl.app/v1/forge/my-app/variables \
  -H "X-Project-Env: dev" \
  -H "Authorization: Bearer <token>"

# Update another app's variables — requires `manage forge`
curl -X PATCH https://api.ptkl.app/v1/forge/my-app/variables \
  -H "X-Project-Env: dev" \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"WEBHOOK_URL": "https://staging.example.com/hook", "MAX_RETRIES": 3}'

The pathless /v1/forge/variables routes are only meaningful for an app token. Calling them with a dashboard session or a plain API token returns 400 this endpoint requires a forge app token.

PATCH replaces the whole environment

Despite the verb, PATCH is not a merge — it replaces the entire variable set for the target environment, so keys you omit are removed. The other environment is left untouched. Use addVariable to change one key while keeping the rest.

The environment is determined by the X-Project-Env header (defaults to dev).


Accessing Variables in Services

Variables are available as the $variables global inside service scripts:

import '@ptkl/components/forge-service'

const webhookUrl = $variables.WEBHOOK_URL
const maxRetries = $variables.MAX_RETRIES || 3

const result = await http.post(webhookUrl, {
    event: 'invoice.created',
    data: input,
})

response.json({ delivered: true })

The platform injects the correct variable set (dev or live) based on which environment the service is executing in. Secret variables arrive decrypted, so $variables.STRIPE_KEY is the real key. Treat them as secrets in your own code: do not return them in a response or write them to logs.

If an upstream error echoes a secret value, the platform removes it from the error and logs that go back to the caller. This is best effort against accidental leaks. It does not protect against code that returns or logs a value on purpose.


Accessing Variables in Lifecycle Scripts

Variables are also available in install and uninstall scripts:

import { Platform } from '@ptkl/sdk'

const platform = new Platform()

const endpoint = $variables.EXTERNAL_API_URL

if (endpoint) {
    await http.post(endpoint, { event: 'app_installed' })
}

return { success: true }

Install and uninstall scripts receive secret variables decrypted too. When one request changes both the variables and the app version, the scripts run with the new values, and nothing is saved if a script fails. Scripts can run again after a failure, so write them to be safe to repeat.

Testing Lifecycle Scripts Locally

ptkl forge install and ptkl forge uninstall run the script on your machine, where the app has no stored variables to read from. Supply them with --var (repeatable), --vars, or --vars-file:

ptkl forge install --path ./my-app --env dev \
  --var EXTERNAL_API_URL=https://staging.example.com \
  --var DEBUG=true

See ptkl forge install for value parsing, precedence, and the env-keyed file format.


Setting Variables During Installation

When an app is installed from the marketplace, initial variables can be provided as part of the installation payload. This allows marketplace apps to prompt for configuration during setup — for example, selecting a region or setting a default language.


Example: Environment-Specific Configuration

A common pattern is to use variables for values that differ between dev and live:

Dev variables:

{
    "WEBHOOK_URL": "https://staging.example.com/hook",
    "DEFAULT_LOCALE": "en_US",
    "DEBUG": true
}

Live variables:

{
    "WEBHOOK_URL": "https://example.com/hook",
    "DEFAULT_LOCALE": "sr",
    "DEBUG": false
}

Your service code stays the same — the platform injects the right set at runtime:

const locale = $variables.DEFAULT_LOCALE || 'en_US'

See Also