App environment variables
These environment variables configure the server of an app, and the Picked CLI when it deploys an app.
App server variables
readAppEnvironment from @picked/app-kit/server reads these variables. See
readAppEnvironment for its parameters and its return value.
import { readAppEnvironment } from '@picked/app-kit/server'
const port = process.env.APP_PORT ?? '4103'
const { privateKey, pickedKeys, publicUrl, dashboardOrigins } = readAppEnvironment(
process.env,
port,
)| Variable | Required | Value |
|---|---|---|
PICKED_APP_PRIVATE_KEY | Yes | The Ed25519 private key of the app, as the base64 of its PKCS#8 DER. A PKCS#8 PEM also works. |
PICKED_API_URL | See Values when a variable is not set | The origin of the Picked API that the app accepts requests from, for example https://api.picked.app. |
APP_PUBLIC_URL | No | The origin of the server of the app. The app uses it in its own URLs, for example the URL of an OAuth callback. |
PICKED_DASHBOARD_ORIGINS | No | The dashboard origins that can show the pages of the app in an iframe. A , separates each origin. |
API_PORT | No | The port of a Picked API on your computer. |
DASHBOARD_PORT | No | The port of a Picked dashboard on your computer. |
PICKED_APP_ID | No | The app ID in this Picked environment, for example app_k3v9x2m1q8zt. picked app deploy prints it. |
picked app keys generate prints a value for PICKED_APP_PRIVATE_KEY. See
CLI.
readAppEnvironment returns PICKED_APP_ID as
appId. The first origin that dashboardOrigins() gives is the dashboard that the app links
to. When you set PICKED_DASHBOARD_ORIGINS, write the main dashboard first, before an origin
with *. See the guide about
links to an installation.
The app reads the public keys of Picked from
<PICKED_API_URL>/.well-known/picked-app-keys.json. It verifies each request from Picked
with these keys. See Public keys of Picked.
Values when a variable is not set
readAppEnvironment uses the first source in each row that has a value. PICKED_API_URL is required only when no other source gives a value.
| Variable | Sources, in order |
|---|---|
PICKED_APP_PRIVATE_KEY | 1. The variable. 2. None: readAppEnvironment throws an error. |
PICKED_API_URL | 1. The variable. 2. On Vercel, the related project picked-api. 3. When API_PORT is set, http://localhost:$API_PORT. 4. None: readAppEnvironment throws an error. |
APP_PUBLIC_URL | 1. The variable. 2. On Vercel with VERCEL_ENV=production, https://$VERCEL_PROJECT_PRODUCTION_URL. 3. On Vercel in a different environment, https://$VERCEL_BRANCH_URL. 4. http://localhost:<port>. |
PICKED_DASHBOARD_ORIGINS | 1. The variable. 2. When DASHBOARD_PORT is set, http://localhost:$DASHBOARD_PORT and http://127.0.0.1:$DASHBOARD_PORT. 3. The app config of Picked. |
<port> is the second argument of readAppEnvironment.
| Error | Cause |
|---|---|
Set PICKED_APP_PRIVATE_KEY to the app's private key, from `picked app keys generate` | PICKED_APP_PRIVATE_KEY is not set, or is empty. |
Set PICKED_API_URL to the Picked API the app trusts | No source gives a value for PICKED_API_URL. |
readAppEnvironment removes each / at the end of PICKED_API_URL and APP_PUBLIC_URL.
Related projects on Vercel
On Vercel, VERCEL_RELATED_PROJECTS lists the related projects of the deployment.
readAppEnvironment reads the origin of the related project picked-api from it:
VERCEL_ENV | Origin of the related project |
|---|---|
preview | The preview deployment of the related project for the same custom environment, or for the same branch. |
production | The production alias of the related project, or its production URL. |
The dashboard origins come from the app config of Picked of that API. The app config gives the dashboard host of each client.
A deployment without the related project picked-api gets no value from it. Set
PICKED_API_URL on that deployment.
Values for each Picked environment
The server of an app accepts requests from one Picked environment. Each Picked environment gives the app a different app ID.
| Picked environment | PICKED_API_URL | PICKED_DASHBOARD_ORIGINS |
|---|---|---|
| Production | https://api.picked.app | https://dashboard.picked.app,https://*.picked.app,https://dashboard.convincely.com |
Test (picked-dev.app) | https://api.picked-dev.app | https://dashboard.picked-dev.app,https://*.picked-dev.app |
https://*.picked.app includes the dashboard host of each client, for example
https://acme.picked.app. To allow fewer hosts, write each host in the list.
The production list has no picked-dev.app origin.
You do not have to set PICKED_DASHBOARD_ORIGINS. Without it, the app uses the
app config of Picked. Set it to allow fewer origins, or an origin that
the app config does not have.
App config of Picked
The Picked API publishes the dashboard origins of its environment at
<PICKED_API_URL>/.well-known/picked-app-config.json. See
App config of Picked.
dashboardOrigins() gives frameAncestors when the environment gives a dashboard origin.
When the environment gives no dashboard origin, dashboardOrigins() reads the app config:
- It reads the app config on the first call.
- After a read that succeeds, it reads the app config again after 5 minutes or more.
- Before a read succeeds, it reads the app config again on each call.
- It stops a read after 5 seconds.
- When a read fails, it gives the origins of the last read that succeeded.
- Before the first read succeeds, it gives no origin.
The app config comes from PICKED_API_URL. The public keys of Picked come from the same API,
so the app config adds no new source.
frame-ancestors
readAppEnvironment returns the dashboard origins that the environment gives as
frameAncestors. dashboardOrigins() also gives the origins of the app config. Give
await dashboardOrigins() to page from @picked/app-kit/server. page sends the origins
in the Content-Security-Policy header of a page:
Content-Security-Policy: frame-ancestors https://dashboard.picked.app https://*.picked.app https://dashboard.convincely.comWhen the list of origins that you give page is empty, the header is
frame-ancestors 'none'. Then no site can show the page in an iframe, and the dashboard
also cannot show it. See
page.
CLI variables
picked app deploy and picked app dev read PICKED_APP_PRIVATE_KEY and
PICKED_APP_PUBLIC_KEY to register a public key with the app. See
CLI environment variables. An app holds two
public keys at most. See Public keys of an app.
Related
- How to host an app on Vercel
- How to work on an app from your machine
- How to rotate an app key
@picked/app-kit—readAppEnvironmentandpage.- About app authentication — why the app has a private key and a public key.
