TrayPage Docs

Self-hosting on Cloudflare

Set up TrayPage on your own Cloudflare account with the Deploy to Cloudflare button, Postgres, custom domains, and email.

Run TrayPage on your own Cloudflare account, with no servers to manage. See Self-hosting for a comparison of setups.

Browser / AI agent
  → Custom domains (one for the app, one for the viewer)
  → Worker (runs close to the database)
      ├─ Hyperdrive → Postgres (PlanetScale or Neon)
      ├─ R2 (page content)
      └─ Email Service (sign-in links and invitations)

Requirements

What you needWhy
Workers Paid plan ($5/month)The free plan's CPU limit (10 ms per request) is too low for sign-in and page rendering. This is required.
PostgresPlanetScale (billed through Cloudflare, from $5/month) or Neon (has a free tier)
A GitHub or GitLab accountThe button copies the repository into your account, and pushes to that copy redeploy the Worker
An R2 subscriptionWhere page content is stored. On a new account, enable it in the dashboard under Storage & databases → R2 → Overview (there is a free usage tier). You cannot create a bucket until it is enabled
A domain managed in CloudflareUsed for the two custom domains (app and viewer) and as the email sender
Permissions on the target accountYou must be able to create and configure Workers, R2, Hyperdrive, domains (DNS), and Email Service

As a rough cost, expect from about $5-10 per month for Workers Paid plus the database.

Before you use the CLI (Wrangler)

If you use the npx wrangler ... commands on this page, do the following first. You do not need this if you only use the button and the dashboard.

  1. Install Node.js (22.12 or later in the 22 line, or the 24 line) and pnpm 10. Earlier 22 releases cannot build the project.

  2. Get the repository and install dependencies. After deploying with the button, get your own copy of the repository.

    git clone <URL of the TrayPage repository> traypage && cd traypage
    pnpm install --frozen-lockfile
  3. Log in to Cloudflare (a browser opens).

    npx wrangler login
  4. If your login has access to more than one Cloudflare account, set the target account. npx wrangler whoami shows the IDs.

    export CLOUDFLARE_ACCOUNT_ID=<account ID>

Run the commands that follow in this directory.

1. Create Postgres

PlanetScale

  1. In the Cloudflare dashboard, open Storage & databases → Postgres & MySQL (Hyperdrive) and choose Create PlanetScale database.
  2. Under New PlanetScale database, click Create database. You are taken to the PlanetScale site; sign in with your PlanetScale account (billing goes through Cloudflare, but the database is created on the PlanetScale screens).
  3. Choose Postgres as the engine. Pick a region close to most of your users (for example aws-ap-northeast-1 for Japan). Write down the region; you need it in step 5.
  4. Wait for the database to finish creating.

Neon

  1. Create a project at Neon and note its region (for example aws-ap-southeast-1).
  2. Copy the connection string (postgresql://...). Hyperdrive provides its own connection pooling, so use the direct connection string, not the one that goes through Neon's connection pooling. The connection string Neon shows first is often the pooled one (the host name contains -pooler). In that case, remove -pooler from the host name (ep-xxxx-pooler.… → ep-xxxx.…). You can keep the trailing ?sslmode=require&channel_binding=require.

On the Neon free plan, the database stops after about five minutes without access. The first operation after that is slower, because it waits for the database to resume.

2. Create Hyperdrive and connect the database

Creating the database does not create a Hyperdrive configuration. The button asks you to pick an existing one, so create it first.

On the same screen (Storage & databases → Postgres & MySQL (Hyperdrive)), click Connect database to open the Create Hyperdrive Connection screen.

  • PlanetScale: choose Connect to PlanetScale database → Existing PlanetScale database and select the database from step 1.

  • Neon: choose Connect to public database and enter a name and the connection string from step 1. Turn Enable caching off. With the CLI:

    npx wrangler hyperdrive create traypage --caching-disabled --connection-string="postgresql://USER:PASSWORD@HOST/DB?sslmode=require"

    If it asks Would you like Wrangler to add it on your behalf?, answer No (wrangler.jsonc already has the HYPERDRIVE setting). You need the ID it prints in step 3 if you deploy without the button.

Turn query caching off. Hyperdrive can cache the results of read queries for a short time. With caching on, a list read right after a write can return stale results. Turning Enable caching off and passing --caching-disabled, as above, are how you set this.

To check after creating it, run npx wrangler hyperdrive get <ID> and confirm that disabled under caching is true (npx wrangler hyperdrive list shows the ID). Check this too if you connected PlanetScale, or created the configuration with an earlier version of this guide. If caching is on, turn it off with:

npx wrangler hyperdrive update <ID> --caching-disabled

3. Deploy with the button

  1. Click the Deploy to Cloudflare button in the TrayPage repository README.

  2. Fill in the form:

    FieldValue
    Git account / repository nameWhere the copy goes. You change settings in this repository from now on
    Worker nametraypage by default. It becomes the workers.dev URL
    HYPERDRIVEThe Hyperdrive configuration from step 2
    PAGE_BUCKETThe R2 bucket for page content. It is created automatically. Give it a name that only this instance uses (the default is traypage-pages; if the account already has another TrayPage, you must change it)
    BETTER_AUTH_SECRETThe output of openssl rand -hex 32. Changing it later signs everyone out
    VIEWER_TICKET_SECRETThe output of a separate openssl rand -hex 32. Do not reuse the BETTER_AUTH_SECRET value
    EMAIL_FROMRequired. The sender address (for example TrayPage <noreply@traypage.example.com>). Use an address on the domain you register with Email Service in step 6. While it is empty, no sign-in links or notifications are sent. If you have not decided yet, set it in step 6
    SIGNUP_MODE, SIGNUP_ALLOWED_EMAIL_DOMAINS, SIGNUP_ALLOWED_EMAILSWho can sign up. Set these here, before the instance is public (what the values mean: Configuration reference). With the default open, anyone who learns the URL can sign up and post pages. In our live test, automated scanners reached the instance two minutes after it became public
    Build command / Deploy commandLeave the defaults
  3. When the deploy finishes, check that https://<worker-name>.<account-subdomain>.workers.dev/health returns {"ok":true,...}.

  4. Database migrations are applied automatically by the app on the first access. Open any URL other than /health once (for example /api/organizations, which returns 401). This one request can take a few dozen seconds to respond.

At this point page content is served from the same host as the app, and email cannot be sent. Finish steps 4 to 6 before you start using it.

Without the button (deploy with Wrangler)

The button needs the URL of a public repository. If your copy of the repository is private, or you want to deploy from your own machine, you can deploy the same thing with Wrangler. Do Before you use the CLI (Wrangler) first.

  1. In wrangler.jsonc, replace the id under hyperdrive with the ID of the Hyperdrive configuration from step 2 (npx wrangler hyperdrive list also shows it). Set SIGNUP_MODE, the allowlists, and EMAIL_FROM under vars here as well (the reason is in the table above; what the values mean: Configuration reference).

  2. If the account already has another TrayPage, you must change both the Worker name (name) and the R2 bucket name (bucket_name) (for example traypage-staging and traypage-staging-pages). When a bucket with the same name already exists, Wrangler uses it instead of creating a new one. Keep the bucket, the Hyperdrive configuration, and the database separate for each instance. If they are shared, data mixes, and deleting one instance also deletes the other's pages.

  3. Build and deploy. Put the secrets in a file and pass it with --secrets-file.

    pnpm run build
    SECRETS=$(mktemp)
    printf 'BETTER_AUTH_SECRET=%s\nVIEWER_TICKET_SECRET=%s\n' "$(openssl rand -hex 32)" "$(openssl rand -hex 32)" > "$SECRETS"
    npx wrangler deploy --secrets-file "$SECRETS"
    rm "$SECRETS"
    • wrangler deploy creates the R2 bucket if it does not exist.
    • The secrets stay on the Worker. From the second time on, pnpm run build && npx wrangler deploy is enough.
  4. Do items 3 and 4 above (check /health, then make the first access).

Wherever the following steps say "push to redeploy", run pnpm run build && npx wrangler deploy instead.

4. Set up two custom domains

  1. In wrangler.jsonc in your copy of the repository, change workers_dev to false and add routes.

    "workers_dev": false,
    "routes": [
      { "pattern": "traypage.example.com", "custom_domain": true },
      { "pattern": "view.traypage.example.com", "custom_domain": true }
    ],
    • Both host names must belong to a zone in the same account. DNS records and certificates are created automatically.
    • Set workers_dev to false. If the workers.dev URL remains, it can be used to bypass the separation of the viewer host.
  2. Add the two domain lines to vars (VIEWER_HOST is not needed; it is taken from VIEWER_ORIGIN). Do not remove the lines that are already there, such as ENVIRONMENT, TRAYPAGE_EDITION, and SIGNUP_*.

    "APP_ORIGIN": "https://traypage.example.com",
    "VIEWER_ORIGIN": "https://view.traypage.example.com"

    APP_ORIGIN is the app URL used for links in emails (sign-in, sharing, invitations, notifications). Write only the origin (a trailing / is fine; a path or query is a configuration error). While it is not set, links are built from the URL the action came in on (which can be the workers.dev URL or another domain; sign-in links use the URL the sign-in started from), so always set it once you have a domain.

  3. Before you push, check that SIGNUP_MODE, SIGNUP_ALLOWED_EMAIL_DOMAINS, SIGNUP_ALLOWED_EMAILS, and EMAIL_FROM under vars have the values you chose in step 3. On every deploy, Wrangler replaces the variables with the contents of wrangler.jsonc (variables that are not in the file are removed). The values you typed on the button screen are not guaranteed to be in wrangler.jsonc in your copy of the repository, so always look at the file. If it still has the template defaults (open and empty allowlists), this deploy puts the instance back into a state where anyone can sign up.

  4. After you push, Workers Builds redeploys. Wait about a minute, then check that https://traypage.example.com/health returns 200 and https://view.traypage.example.com/ returns 404 (the viewer host only answers for shared page content and the /health check). If you access the host before its DNS record exists, your computer remembers that the name does not exist for up to 30 minutes (see Troubleshooting).

  5. If you restricted sign-up, check that the restriction is in effect. It is when signupRestricted is true in https://traypage.example.com/api/login/options. If it is false, fix vars in wrangler.jsonc and deploy again.

Put vars in wrangler.jsonc, not in the dashboard. Values changed in the dashboard revert to the contents of wrangler.jsonc on the next deploy (secrets are kept). When you edit wrangler.jsonc in steps 5 and 6, do not remove the lines that are already under vars either.

5. Match placement to the database region

If you do not set it, the Worker runs near the user and makes a round trip to the database for every query, which is slow (in testing, a Vancouver edge with a Tokyo database took 0.6 to 1.6 seconds per operation). Set the database region in wrangler.jsonc.

"placement": { "mode": "targeted", "region": "aws:ap-northeast-1" },
Database regionValue for region
PlanetScale aws-ap-northeast-1 (Tokyo)aws:ap-northeast-1
PlanetScale aws-us-east-1aws:us-east-1
PlanetScale gcp-us-central1gcp:us-central1
Neon aws-ap-southeast-1 (Singapore)aws:ap-southeast-1
Neon aws-us-east-2aws:us-east-2

The format is provider:region. After redeploying, if the cf-placement response header looks like remote-NRT (for Tokyo; remote-SIN for Singapore), the Worker is running near the database.

curl -sI https://traypage.example.com/api/organizations | grep -i cf-placement

6. Set up email (Cloudflare Email Service)

Sign-in uses an emailed link. Email is sent through Cloudflare Email Service and needs no API key.

  1. In the dashboard, open Email Service → Domains and register the domain you will send from (SPF and DKIM DNS records are added).

  2. In wrangler.jsonc, fill in EMAIL_FROM under vars (empty in the template) with an address on the domain from step 1, and push. If you entered it on the button screen, check that it is in the file.

    "EMAIL_FROM": "TrayPage <noreply@traypage.example.com>"

    EMAIL_FROM is required. The deploy succeeds while it is empty, but every email fails and npx wrangler tail shows EMAIL_FROM is required.

  3. From /login on the app domain, check that a sign-in link reaches your email address.

Note: Email Service is in public beta. Sign-in links do not arrive until the sender domain is registered. Adding allowed_sender_addresses (an allowlist of sender addresses) to send_email in wrangler.jsonc stops the Worker from sending from any address other than EMAIL_FROM.

If RESEND_API_KEY, which earlier versions documented, is set, Resend is still used in preference to Email Service (delete the secret to switch to Email Service).

Optional settings

SettingHow
Google sign-inAdd the secrets GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET, and add "NEXT_PUBLIC_GOOGLE_AUTH": "1" to vars. The redirect URI in the Google Cloud Console is https://<app-domain>/api/auth/callback/google
Sign-in with your company IdP (OIDC)Set OIDC_ISSUER, OIDC_CLIENT_ID, and OIDC_DISPLAY_NAME in vars, and add the secret OIDC_CLIENT_SECRET. The redirect URI in the IdP is https://<app-domain>/api/auth/oauth2/callback/oidc. See Security notes for the steps
Enterprise licenseThe secret TRAYPAGE_LICENSE_KEY (License and Enterprise features)
PDF exportThe BROWSER binding (Browser Run). Included in the template. "Print / save as PDF" works without it
Notify the CLI as soon as a review is finishedREVIEW_SESSIONS (Durable Objects). Included in the template. Without it, the CLI checks for the result every few seconds
Notification email intervaltriggers.crons in wrangler.jsonc. Every 5 minutes in the template. Each run queries the database once, so a database that suspends when idle (such as the Neon free tier) stays awake (Notification emails)

Add secrets in the dashboard under Workers & Pages → (your Worker) → Settings → Variables and Secrets, or with npx wrangler secret put <NAME>. Every setting is listed in the Configuration reference.

Updating

Merge upstream changes into your copy of the repository and push; Workers Builds redeploys. Database migrations are applied automatically on the first access to the new version.

git remote add upstream <URL of the TrayPage repository>
git fetch upstream && git merge upstream/main   # if wrangler.jsonc conflicts, keep your own values
git push

Deleting

When you stop using it, delete things in this order. Once the database and the R2 bucket are gone, pages and account data cannot be recovered.

First, check that what you are about to delete belongs only to this instance. If another Worker in the same account (including another TrayPage) uses the same R2 bucket, Hyperdrive configuration, or database, its data is deleted too. For each Worker listed under Workers & Pages in the dashboard, print its settings with the commands below and check that env.PAGE_BUCKET (the bucket name) and env.HYPERDRIVE (the Hyperdrive ID) do not overlap with what you are deleting.

npx wrangler deployments status --name <Worker name>         # the Version(s) line shows the ID of the running version
npx wrangler versions view <version ID> --name <Worker name> # lists what the Worker connects to and its variables

origin in npx wrangler hyperdrive get <ID> shows which database a Hyperdrive configuration points to.

  1. Worker: run npx wrangler delete in your copy of the repository. The two custom domains and the DNS records that were created for them are removed with it.
  2. R2 bucket: a bucket that still has content cannot be deleted. In the dashboard, open the bucket under Storage & databases → R2 Object Storage, and at the bottom of Settings, empty it with Empty Bucket, then delete it with Delete bucket.
  3. Hyperdrive: run npx wrangler hyperdrive delete <ID> (npx wrangler hyperdrive list shows the ID).
  4. Database: delete it on the PlanetScale or Neon screens. PlanetScale keeps billing until you delete it.
  5. Email: if nothing else uses the sender domain, remove it under Email Service → Domains.
  6. If you deployed with the button, also delete the copied Git repository if you no longer need it.
  7. Workers Paid plan: the subscription continues after the Worker is deleted. If you subscribed only for TrayPage, check that no other Worker in the account needs the paid plan, then consider downgrading under Workers plans in the dashboard.

Troubleshooting

SymptomWhat to check
Slow responses (about a second per operation)Placement in step 5, and the cf-placement header
Exceeded CPU time limit / error 1102Whether you are on Workers Paid
/health returns 200 but everything else returns 500The Hyperdrive configuration (connection string and password). Read the logs with npx wrangler tail
The deploy stops with Hyperdrive binding 'HYPERDRIVE' references config ... which was not found (code 10157)The id under hyperdrive in wrangler.jsonc is still the template default (32 zeros). Replace it with the ID of the Hyperdrive configuration from step 2
Could not resolve host right after setting up the custom domainsYou accessed the host before its DNS record existed, and your computer remembers that it does not exist. If dig +short <host name> @1.1.1.1 returns addresses, the setup is done. It clears within 30 minutes. Other devices and networks can open it right away
A page or comment you just created does not show up in lists right awayWhether Hyperdrive query caching is on (see how to check in step 2)
Only the first operation is slow after a period without useOn the Neon free plan, the database stops when there is no access. The first operation waits for it to resume
Sign-in links do not arriveStep 6 (registering the sender domain with Email Service, and EMAIL_FROM). If sign-up is restricted, check that the address is on the allowlist or has an invitation (unregistered addresses that are not allowed get no email). With npx wrangler tail, look for magic_link_delivery_failed (delivery failed; EMAIL_FROM is required means EMAIL_FROM is empty) and magic_link_suppressed (not sent because of the restriction). Only if you have been running an earlier version: a leftover RESEND_API_KEY (the note in step 6)
The app shows up on the viewer domainVIEWER_ORIGIN and routes (step 4)
Responses are 500 with the body Invalid VIEWER_HOST or Invalid VIEWER_ORIGIN (only public static files such as JS, CSS, and images are still returned)How the setting named in the body is written under vars. Write only an origin starting with https:// in VIEWER_ORIGIN (no path or query). VIEWER_HOST can be left out; if you write it, use only the same host name as VIEWER_ORIGIN (VIEWER_HOST alone is an error). The viewer host must differ from the host name of APP_ORIGIN and must not be its www. host. Fix it and redeploy