Skip to content
LogoLogo

How to host an app on Vercel

This guide shows you how to serve the server of an app from one Vercel Function. Picked sends each request to the function, and Vercel serves the static files.

Before you start, you need an app directory with picked.config.ts and a handler from createAppHandler. The app section of picked.config.ts is the manifest. You also need a Vercel project for the directory, and the Vercel CLI. Build and install your first app makes the app.

When you finish, the directory has these files:

my-app/
├── api/
│   └── index.ts        the Vercel Function
├── public/             static files; the build writes app-kit/frame.js here
├── src/
│   ├── app.ts          the handler
│   └── server.ts       the server on your machine
├── package.json
├── picked.config.ts
└── vercel.json

1. Put the handler in its own module

Make the handler in src/app.ts. The function on Vercel and the server on your machine both use it.

import { createAppHandler, json, page, readAppEnvironment } from '@picked/app-kit/server'
import config from '../picked.config.js'
 
const homePage = `<!doctype html>
<html>
  <body>
    <p id="project">Connecting to Picked…</p>
    <script type="module">
      import { autoResize, connect } from '/app-kit/frame.js'
      const context = await connect()
      document.getElementById('project').textContent = 'Project ' + context.projectId
      autoResize()
    </script>
  </body>
</html>`
 
// Builds the handler of the app.
export function createHandler(
  env: Record<string, string | undefined>,
  options: { frameScript?: () => Promise<Response> } = {},
): (request: Request) => Promise<Response> {
  const { pickedKeys, frameAncestors } = readAppEnvironment(env, env.APP_PORT ?? '4103')
  return createAppHandler({
    pickedKeys,
    manifest: config.app,
    async fallback(request) {
      const { pathname } = new URL(request.url)
      if (pathname === '/') return page(homePage, { frameAncestors })
      if (pathname === '/app-kit/frame.js' && options.frameScript !== undefined) {
        return options.frameScript()
      }
      return json({ error: 'not_found' }, 404)
    },
  })
}

picked.config.ts imports defineConfig from @picked/config/define, which reads no files, so the function includes the manifest and nothing more of @picked/config.

Write each relative import with the extension .js, also for a .ts file. Vercel runs the function as a native JavaScript module. A relative path without the extension fails. Set "type": "module" in package.json.

2. Add the function

Make api/index.ts. Vercel serves each file in api/ as a function.

import { withRewrittenPath } from '@picked/app-kit/server'
import { createHandler } from '../src/app.js'
 
export default {
  fetch: withRewrittenPath(createHandler(process.env)),
}

Keep src/server.ts for your machine. It serves the same handler. It also serves /app-kit/frame.js from the package:

import { frameScript, serve } from '@picked/app-kit/node'
import { createHandler } from './app.js'
 
serve(createHandler(process.env, { frameScript }), Number(process.env.APP_PORT ?? '4103'))

3. Send each path to the function

Make vercel.json:

{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "framework": null,
  "buildCommand": "npm run build",
  "outputDirectory": "public",
  "rewrites": [
    { "source": "/", "destination": "/api?__path=/" },
    { "source": "/:path*", "destination": "/api?__path=/:path*" }
  ]
}

The rewrites send each path to the function, with the path in __path. The /:path* rule does not match /, so the first rule sends /. withRewrittenPath puts the path back in the URL of the request. So the handler gets /lifecycle, /tools/<name> and the other paths of the App protocol.

Vercel serves a file in public/ before it applies the rewrite.

4. Copy the script of the page at build time

The pages of the app import /app-kit/frame.js. The command app-kit-copy-frame from @picked/app-kit copies the script to a directory. Add it as the build script of package.json:

{
  "type": "module",
  "scripts": {
    "build": "app-kit-copy-frame public",
    "start": "tsx src/server.ts"
  }
}

Run the build one time to make sure that it works:

npm run build
Copied the app-kit frame script to /home/you/my-app/public/app-kit/frame.js

Do not commit public/app-kit/frame.js. Each build writes it again from the installed version of @picked/app-kit.

5. Set the environment variables

Set these variables on the Vercel project, for the production environment:

  • PICKED_APP_PRIVATE_KEY
  • PICKED_API_URL
  • PICKED_DASHBOARD_ORIGINS
vercel env add PICKED_APP_PRIVATE_KEY production
vercel env add PICKED_API_URL production
vercel env add PICKED_DASHBOARD_ORIGINS production

The values for each Picked environment are in App environment variables.

You do not have to set APP_PUBLIC_URL. On Vercel, the server uses the production domain of the project in production, and the branch URL in a preview. Set it if the app has a different domain. Refer to Values when a variable is not set.

6. Deploy the server

vercel deploy --prod

Make sure that the function answers. The handler serves the manifest at /manifest.json:

curl https://my-app.vercel.app/manifest.json
{"handle":"my-app","name":"My App", …}

A request that Picked did not sign gets 401:

curl -i -X POST https://my-app.vercel.app/lifecycle
HTTP/2 401
{"error":"invalid_signature"}

7. Deploy the manifest

Set the endpoint of the manifest to the production domain of the project. Then deploy the manifest. This command reads the endpoint from APP_PUBLIC_URL, as the manifest in How to deploy an app does:

APP_PUBLIC_URL=https://my-app.vercel.app npx picked app deploy --org acme
Deployed My App (my-app) version 1 [app_…]