How to link to an installation from your site
This guide shows you how to send a person from your own site to the installation of your app in the Picked dashboard. A different service can also send the person. The link names a value that only one installation holds, such as the ID of an account at your service. The dashboard finds the installation and opens its page.
You need an app with a server that uses @picked/app-kit, and the private key of the app.
Build and install your first app makes these. The installation must
show a page at the app-home location. Refer to
How to add a page to the dashboard.
1. Keep the external ID in a label
When an installation connects to an account at your service, set a label that holds the ID of the account. The label must not be secret. A secret label never matches a link.
Set the label with the installation client of the request that makes the connection. An example is the callback in How to connect an external service with OAuth:
await client.labels.set([{ namespace: 'crm', key: 'account_id', type: 'text', value: 'acct_4821' }])The namespace and the key of the label each have 1 to 64 characters: lowercase letters,
digits, _ or -. A label that is expired does not match.
How to store data for an installation shows
the other ways to write labels.
2. Get the app ID
Each Picked environment gives your app a different app ID, for example app_k3v9x2m1q8zt.
Get the ID for each environment that you deploy to, in one of these ways:
- From the CLI.
picked app deployprints it at the end of a line:Deployed My App (my-app) version 3 [app_k3v9x2m1q8zt]. When the manifest has no changes, the line isMy App (my-app) is unchanged: the manifest matches version 3 [app_k3v9x2m1q8zt]. - From the dashboard. In the organization, open Apps, then open the app. App ID shows the ID. Click Copy the app ID to copy it.
Set the ID as PICKED_APP_ID in the environment of your server for that Picked environment.
readAppEnvironment returns it as appId.
3. Get the dashboard origin
dashboardOrigins() from readAppEnvironment gives the dashboard origins. The first origin is
the main dashboard of the Picked environment. Without PICKED_DASHBOARD_ORIGINS, the origins
come from the app config of Picked.
When you set PICKED_DASHBOARD_ORIGINS, write the main dashboard first, for example:
PICKED_DASHBOARD_ORIGINS=https://dashboard.picked.app,https://*.picked.app,https://dashboard.convincely.comRefer to App environment variables for the value in each Picked environment.
4. Build the link
Call installationLinkUrl with the dashboard origin, the app ID, and the label:
import { installationLinkUrl, readAppEnvironment } from '@picked/app-kit/server'
const { dashboardOrigins, appId } = readAppEnvironment(process.env, process.env.APP_PORT ?? '4103')
const [dashboardOrigin] = await dashboardOrigins()
if (dashboardOrigin === undefined || appId === null)
throw new Error('Set PICKED_APP_ID, and let the app read the dashboard origins')
const url = installationLinkUrl({
dashboardOrigin,
appId,
namespace: 'crm',
key: 'account_id',
value: 'acct_4821',
})The result is
https://dashboard.picked.app/apps/app_k3v9x2m1q8zt/open?label=crm.account_id&value=acct_4821.
The link has this shape:
<dashboard origin>/apps/<app ID>/open?label=<namespace>.<key>&value=<value>installationLinkUrl encodes the value. It throws TypeError in two conditions:
- The app ID, the namespace or the key has a format that Picked does not accept.
dashboardOriginis not a URL.
5. Send the person to the link
Put the link on your site, or redirect to it. For example, the Vercel app answers the
Configuration URL of its Vercel integration. Vercel opens that URL with a
configurationId. The app redirects to the installation that holds that ID in its
vercel.configuration_id label. When it cannot make the link, it shows a page with a link to
the dashboard. This code is a short form of it:
import { installationLinkUrl, page, readAppEnvironment } from '@picked/app-kit/server'
const { dashboardOrigins, appId } = readAppEnvironment(process.env, process.env.APP_PORT ?? '4103')
async function configure(request: Request): Promise<Response> {
const configurationId = new URL(request.url).searchParams.get('configurationId') ?? ''
const [dashboardOrigin] = await dashboardOrigins()
if (/^icfg_\w{1,64}$/.test(configurationId) && dashboardOrigin !== undefined && appId !== null) {
const location = installationLinkUrl({
dashboardOrigin,
appId,
namespace: 'vercel',
key: 'configuration_id',
value: configurationId,
})
return new Response(null, { status: 302, headers: { location, 'cache-control': 'no-store' } })
}
const home =
dashboardOrigin === undefined
? ''
: `<a href="${new URL('/', dashboardOrigin).href}">Open Picked</a>`
return page(
`<!doctype html><title>Picked</title><p>Open the app on your project in Picked. ${home}</p>`,
{
frameAncestors: [],
},
)
}6. Know what the person sees
The dashboard looks for the installations of the app that hold the label with that value.
It keeps only the installations in projects where the person has organization_apps/read.
Then it shows one of these results:
| Matches | Result |
|---|---|
| One | The dashboard opens the page of the installation, at /<organization>/<project>/apps/<installation slug>. |
| More than one | The page Choose where to open <app name> shows a list of the projects, as <organization> / <project>. The person chooses one. |
| None | The page No installation of this app that you can open matches this link, with the link Go to the dashboard. |
For one link, Picked reads 200 installations at most. When more projects match than the list shows, the page shows this message: "More projects match this link than this list shows. Open the project from the dashboard to find the rest."
The person sees no match in these conditions:
- The label is secret, is expired, or holds a different value.
- The app is installed only on projects that the person cannot open.
- The app ID is for a different Picked environment.
- The link has no
labelor novalue.
Related
@picked/app-kit— the parameters and errors ofinstallationLinkUrl.- How to store data for an installation — write the label.
- How to deploy an app — get the app ID from
picked app deploy. - Vercel app — the Configuration URL of the Vercel app.
