testosaurusdocs
Browse docs
On this page

Self-hosting

Testosaurus is one Cloudflare Worker (Hono, D1, R2, Queues) plus an optional second Worker that watches it. This guide takes you from an empty Cloudflare account to a running install. docs/deployment.md is the maintainer runbook and the full reference for every variable; this guide follows the same facts and says what a new install has to change.

Read the whole guide once before you start. Several steps depend on each other: the deploy guard refuses a deploy until the secrets and migrations exist, and you cannot become an operator before the first sign-in.

What you need

  • A Cloudflare account with Workers, D1, R2, Queues and Email Sending, and a domain whose DNS is on that account. The Worker is attached to a custom_domain route, and the install has no Workers subdomain (workers_dev and preview_urls are off).
  • A plan that allows what you use. docs/deployment.md states two plan facts: Email Sending requires the Workers Paid plan, and D1 Time Travel keeps 30 days of history on Workers Paid. For every other product, check Cloudflare's current plan limits before you rely on them.
  • Node.js 20 or newer, and git.
  • Your own fork or clone of this repository whose origin remote is a repository you control and whose default branch is master. The deploy guard compares HEAD with origin/master.
  • A key for an OpenAI-compatible LLM gateway (the code defaults to the Vercel AI Gateway). Without it, reports are stored but not analyzed.
  • Two Cloudflare Turnstile widgets (created below).
  • An authenticator app that accepts a base32 secret, for the operator step-up.
  • A mailbox you read for support@ on your domain, and one for the operator notices.

Stripe is optional. You need it only to charge money for a paid service. See Billing.

1. Get the code and log in

git clone <your fork> testosaurus
cd testosaurus
npm ci
npx wrangler login

Check that git remote get-url origin is your repository and that master is the branch you work on. Everything below runs from the repository root unless a command starts with cd packages/server.

2. Create the Cloudflare resources

The Worker binds these resources. wrangler.toml ships their names; create them in your account under the same names, or change the names in wrangler.toml to match what you create.

BindingResourceCreate it with
DBD1 database testosaurusnpx wrangler d1 create testosaurus
SCREENSHOTSR2 bucket testosaurus-screenshotsnpx wrangler r2 bucket create testosaurus-screenshots
DISTILL_QUEUEqueue testosaurus-distillnpx wrangler queues create testosaurus-distill
the dead-letter queue of the consumerqueue testosaurus-distill-dlqnpx wrangler queues create testosaurus-distill-dlq
none (backups)R2 bucket testosaurus-backupsnpx wrangler r2 bucket create testosaurus-backups

d1 create prints a database_id. Keep it for the next step.

The testosaurus-backups bucket is never bound to the Worker. npm run db:backup and npm run db:migrate:remote use the fixed names testosaurus (the database) and testosaurus-backups (the bucket) in scripts/backup-d1-lib.mjs, so keep both names, or those two commands cannot find your database. Give the backup bucket a 30-day expiry as docs/deployment.md describes: npx wrangler r2 bucket lifecycle add testosaurus-backups expire-30d --expire-days 30.

Nothing else is created by hand:

  • The rate limiters are bindings that Cloudflare creates from wrangler.toml: RATE_LIMITER (60 per 60 s), MINT_LIMITER (5 per 60 s), BOOTSTRAP_LIMITER (5 per 60 s), API_V1_LIMITER (60 per 60 s), MCP_LIMITER (120 per 60 s), BILLING_LIMITER (5 per 60 s), UPLOAD_LIMITER (30 per 60 s) and SUBMIT_LIMITER (5 per 60 s). Without SUBMIT_LIMITER, POST /api/submit answers 500 and stores nothing.
  • The ASSETS binding serves packages/server/public, which the build writes.
  • The EMAIL binding (send_email) sends mail. See Email.
  • The two crons, 0 3 * * * and */5 * * * *, are scheduled by the Worker's [triggers]. The deploy guard refuses a deploy that lacks either one.
  • The [env.smoke] section is a second, local-only environment for the smoke tests. It has its own local D1 (smoke-local), bucket, queues, no routes and no crons. Leave it as it is. The guard refuses a deploy when it reuses your production database id or name, bucket, queue or route, so never copy your real ids into it.

3. Edit packages/server/wrangler.toml

Open the file and change these values. Several defaults belong to the hosted service and must not be reused.

SettingWhat to do
[[routes]] patternYour host, for example feedback.example.com, with custom_domain = true. The domain must be a zone in your account.
[[d1_databases]] database_idThe id that d1 create printed.
[vars] APP_BASE_URLYour public base URL, for example https://feedback.example.com. Better Auth answers only to this host and, when the URL is https, names its cookies with the __Host- prefix. Email links and asset links are built from it too. The guard refuses a missing value or a localhost value. A URL in your shell never reaches the deployed Worker, so the committed value is what ships.
[vars] ALLOWED_ORIGINSThe platform-wide CORS list. Replace the hosted origin with your own origin. Keep the two localhost entries only if you develop locally. Each project's own allowed addresses are honoured per token, so integrators do not go here.
[vars] OPERATOR_EMAILA mailbox you read. It receives the Monday metrics digest, billing alerts, beta-expiry notices and dead-letter notices.
[vars] OPERATOR_USER_IDSSet it to an empty string for the first deploy. See Become an operator.
[vars] DASHBOARD_FEEDBACK_PROJECT_IDDelete the line. It names a project of the hosted service. When unset, the dashboard's "Report a problem" button stays off.
[vars] TURNSTILE_ACCOUNT_IDDelete the line, or set your own account id. It is read only by the legacy widget cleanup, which also needs TURNSTILE_API_TOKEN.
[vars] BILLING_MODEKeep beta. See Billing.
[vars] STRIPE_PRODUCT_PRO, STRIPE_PRICE_PRODelete these lines. They point at the hosted service's Stripe objects and do nothing while BILLING_MODE is beta.

You can also tune limits, plan allowances, the AI daily cap and more through [vars]. Every key is in the tables of docs/deployment.md.

Optional AI spend variables:

  • AI_DAILY_CAP_MICROS: the AI spend cap per workspace per UTC day. The default is 250,000 micro-EUR (0.25 EUR) and internal workspaces are exempt.
  • AI_UNBILLED_DAILY_CAP_MICROS: the daily cap, per owner, on analysis calls that failed without an answer and so were not billed. The default is 100,000 micro-EUR (0.10 EUR). It stops a failing gateway from running up unbilled cost.

Amounts are micro-EUR, where 1 EUR is 1,000,000.

Commit the change and push it to master of your repository:

git add packages/server/wrangler.toml
git commit -m "chore: configure my install"
git push origin master

The deploy guard refuses a dirty tree and a HEAD that is not the pushed origin/master, so every config change has to be committed and pushed before it can ship.

4. Email

Sign-up needs a verified email address, and the verification mail is sent through the EMAIL binding. Until mail works, nobody can sign in, so set it up before your first sign-up.

  1. Onboard your domain to Email Sending: npx wrangler email sending enable <your domain>. This adds SPF, DKIM and DMARC records and takes about 5 to 15 minutes to propagate on Cloudflare DNS. docs/deployment.md notes that Email Sending requires the Workers Paid plan.
  2. The Worker takes every sender address from the host of APP_BASE_URL. There is no variable to set:
    • alerts@<host> sends verification, password reset, invitation, usage, balance, beta and billing mail and the Monday digest.
    • reports@<host> sends report alerts to the owner and, for apps that turn on "Reporters can hear back", the confirmation, reply and fixed emails to the people who asked to hear back.
    • support@<host> is the Reply-To address of every mail and the support address that the dashboard and the mail footers show.
  3. Make sure support@<host> reaches a mailbox you read, for example with Cloudflare Email Routing.

Verification and password-reset mail are capped per address per day. A send that fails is logged as an email.failed event, with the code unbound when the binding is missing. Check delivery with a real inbox before you open sign-ups.

5. Migrations

For a new, empty database, apply the schema once from the repository root:

npx wrangler d1 migrations apply DB --remote --config packages/server/wrangler.toml

packages/server/migrations.lock.json records the migrations the repository ships; the maintainers lock each migration before they release. If the deploy guard still reports a migration that is not in the lock after you applied the schema, run npm run db:migrate:remote in packages/server (nothing is pending, so it only backs up and locks), then commit and push the lock.

After this first setup, apply new migrations only through npm run db:migrate:remote in packages/server. It refuses a dirty tree, a HEAD other than origin/master, an edited applied migration or a misnumbered new one. It then records a D1 Time Travel bookmark, stores a verified full export in the testosaurus-backups bucket, applies the migrations and appends them to migrations.lock.json. Commit and push the lock before you deploy. Applied migrations are frozen: never edit, squash or delete one. See "Backup, rollback and restore" in docs/deployment.md for the backup and restore details.

6. Secrets

Set the secrets from packages/server. wrangler secret put asks for the value; never put a secret in wrangler.toml.

cd packages/server
npx wrangler secret put AUTH_SECRET
npx wrangler secret put LLM_GATEWAY_KEY
npx wrangler secret put TURNSTILE_PLATFORM_WIDGETS
  • AUTH_SECRET is required. Generate it with openssl rand -base64 32. It signs sessions, derives the key that encrypts stored GitHub and Linear tokens, signs asset links, and signs the short-lived upload tickets that screenshot and replay uploads need. Without it the Worker refuses to build its auth layer, and report uploads answer with an error. Rotating it signs everyone out and invalidates the stored integration tokens, so treat it as long-lived.
  • LLM_GATEWAY_KEY is the key of an OpenAI-compatible gateway. By default the Worker calls the Vercel AI Gateway; LLM_GATEWAY_BASE_URL and LLM_DEFAULT_MODEL in [vars] change the endpoint and the default model. Without a key, analysis is skipped with the reason ai_gateway_missing and submissions are still stored.
  • TURNSTILE_PLATFORM_WIDGETS is required: the deploy guard refuses a deploy without it. Create two widgets in the Cloudflare dashboard (Turnstile, Add widget), one in Invisible mode and one in Managed mode, each with only the host of your APP_BASE_URL as hostname, because the challenge always runs on your platform host. Store both as one JSON value:
{"invisible":{"sitekey":"0x...","secret":"0x..."},"managed":{"sitekey":"0x...","secret":"0x..."}}

Without this secret and without TURNSTILE_API_TOKEN, the Worker would fall back to Cloudflare's public test keys. The guard keeps a deployed install from reaching that state.

  • OPERATOR_TOTP_SECRET comes with the operator step-up. See the next step.

GitHub and Linear are optional integrations with their own OAuth apps and secrets; see docs/deployment.md.

7. Become an operator

A platform operator can manage beta access, offers and platform-wide origins, and read the operations metrics. Operators are the user ids listed in OPERATOR_USER_IDS, and every operator power also needs a second factor: a one-time code from an authenticator app, called the step-up.

The guard blocks a deploy that lists operators in OPERATOR_USER_IDS while the OPERATOR_TOTP_SECRET secret is missing, and a deploy whose [vars] contains OPERATOR_TOTP_SECRET. That is why OPERATOR_USER_IDS stays empty for the first deploy.

The order:

  1. Deploy once with OPERATOR_USER_IDS = "" (step 8), then sign up in the dashboard at <APP_BASE_URL>/dashboard/ and verify your email.
  2. Find your user id from packages/server:
    npx wrangler d1 execute DB --remote --command "SELECT id, email FROM user"
  3. Put the id in [vars] OPERATOR_USER_IDS in wrangler.toml. A comma separates several ids.
  4. Generate the TOTP secret, which must decode to at least 16 bytes (at least 26 base32 characters):
    python3 -c "import base64,os;print(base64.b32encode(os.urandom(20)).decode())"
  5. From packages/server, store it as a secret with npx wrangler secret put OPERATOR_TOTP_SECRET, and add the same secret to your authenticator app with the settings SHA-1, 6 digits, 30 seconds. The enrolment URI is otpauth://totp/Testosaurus:operator?secret=<SECRET>&issuer=Testosaurus&algorithm=SHA1&digits=6&period=30.
  6. Commit and push the wrangler.toml change, then run npm run deploy again from packages/server.

When you open an operator tool, the dashboard asks for your authenticator code. The step-up lasts 12 hours and is bound to that browser session. Signing out, revoking other sessions, resetting the password and deleting the account clear it. Each code works once, platform-wide, and the current 30-second step and one step either side are accepted. After 5 attempts in 60 seconds, or 20 in 24 hours, the step-up answers 429 and operator tools stay locked until the window ends. Operator routes answer operator_step_up_required before a step-up and operator_step_up_unavailable when the secret is missing, not valid base32 or shorter than 16 bytes.

If you are locked out, reset your password, then clear the attempt counter from packages/server: npx wrangler d1 execute DB --remote --command "DELETE FROM operator_step_up_attempts". The "Platform operator step-up" section of docs/deployment.md has the full reference.

8. First deploy

From packages/server:

npm run deploy

It takes no arguments, because arguments would reach wrangler deploy after the guard ran and could override what it checked. It runs these steps and stops at the first failure:

  1. scripts/check-app-config.mjs, the deploy guard.
  2. npm run typecheck.
  3. npm run build -w testosaurus-sdk, then check-sdk-synced.mjs, which fails when the committed SDK bundle in packages/server/public differs from a fresh build. If you change packages/sdk or packages/shared, run npm run build and commit the regenerated files.
  4. check-public-integrity.mjs --links, which fails on a leaked key, a forbidden host or a documentation link that does not resolve.
  5. check-npm-sdk.mjs --warn, which only warns when the published npm package testosaurus-sdk is older than packages/sdk.
  6. wrangler deploy. Its [build] step runs npm run build first, so the deploy never serves a stale bundle.

The guard checks everything at once, prints every problem with its fix, and has no override. It blocks the deploy when:

  • the working tree is not clean, git fetch origin master fails, or HEAD is not origin/master;
  • a git-ignored file sits in packages/server/public;
  • an applied migration changed or disappeared, a new migration is numbered at or below the highest locked one, or a migration is not in the lock or still pending in production;
  • [env.smoke] shares a database, bucket, queue or route with production, or has crons;
  • [triggers] crons lacks 0 3 * * * or */5 * * * *;
  • wrangler secret list fails, or production has no AUTH_SECRET or no TURNSTILE_PLATFORM_WIDGETS;
  • OPERATOR_USER_IDS lists operators while OPERATOR_TOTP_SECRET is missing, or [vars] sets OPERATOR_TOTP_SECRET;
  • APP_BASE_URL is missing or points at localhost.

The guard checks that the secrets exist, not that their values are right. After the first deploy, open <APP_BASE_URL>/dashboard/, go to settings, install and domains, and check that bot protection shows on · Turnstile checks each report.

Two hosted-service strings remain in the static dashboard page, which Cloudflare serves without running the Worker, so no setting reaches it. Edit packages/server/public/dashboard/index.html yourself: the support@ address in the boot fallback paragraph, and the og:image and twitter:image addresses. The file is hand-maintained: the dashboard build rewrites only its modulepreload block. Commit the change before you deploy.

The hosted operator's legal details are also hard-coded. The IMPRINT constant in packages/server/src/email-template.ts prints in the footer of every email, in both the HTML and the text version, and the footer in packages/marketing/src/layouts/Base.astro names the company on the marketing site. Replace both with your own legal details before the first deploy, or your mail goes out signed with someone else's legal address. Skip the second if you do not serve the marketing site.

The marketing site and the docs site that the Worker serves at / are built from packages/marketing and packages/server/docs-src. They describe the hosted service. Edit them if you serve them under your own name, or leave them as they are.

9. Use it

  1. Sign in at <APP_BASE_URL>/dashboard/, create a project, add your site's address under allowed addresses and copy the app token.
  2. Install the widget with the snippet that the dashboard shows. Pass your own endpoint to Testosaurus.init: the SDK's default endpoint is the hosted service.
  3. To connect a coding agent, follow docs/mcp.md and replace the hosted address in its examples with <APP_BASE_URL>/mcp.

The SDK guide is docs/sdk-guide.md.

Billing

BILLING_MODE is beta in the shipped config. In beta mode every Stripe call and webhook is off, purchases answer 503, and no payment is required, so a self-hosted install for your own team needs no Stripe account.

To run a paid service, set BILLING_MODE to sandbox or live, create your own Stripe product and price, set STRIPE_PRODUCT_PRO and STRIPE_PRICE_PRO in [vars], store STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET as secrets, and follow "Stripe billing" in docs/deployment.md, which lists the key permissions, the webhook and the checks. The tax, legal and accounting duties of charging money are yours.

Watchdog (optional)

packages/watchdog is a separate Worker that polls the main Worker's /health every five minutes through a service binding, confirms a failure over three checks and 10 minutes, and emails one DOWN notice and one RECOVERED notice. It also serves a small status page. Because it is a separate failure domain, a broken main Worker cannot stop it.

Edit packages/watchdog/wrangler.toml:

  • [[kv_namespaces]] id: create a namespace with npx wrangler kv namespace create WATCHDOG from packages/watchdog and use its id.
  • [[services]] service: the name of your main Worker, which is testosaurus unless you changed name in the server config.
  • [vars] OPERATOR_EMAIL: the mailbox that receives the notices.
  • [vars] APP_BASE_URL: the same value as the server. The watchdog cannot see the server's variables. It sends from alerts@<host> and shows support@<host> on its page. Without it the watchdog sends no alert and logs watchdog.alert_unsent.
  • [vars] CHECK_TARGETS: a comma-separated list of URLs to check. An entry that starts with binding: goes through the service binding; the others are fetched publicly. Replace the hosted address with yours.

Then deploy it from packages/watchdog with npm run deploy. It needs the same Email Sending setup as the main Worker.

Backups

npm run db:backup in packages/server stores a verified export in testosaurus-backups, and db:migrate:remote runs it before every migration. Exports hold personal data and encrypted tokens, so keep them only in that bucket. D1 Time Travel is a second layer; docs/deployment.md describes how to restore into a scratch database and why you should never rehearse a restore in production.

Updating

Fetch the upstream changes into your fork, resolve conflicts in your wrangler.toml by keeping your values, push master, run npm run db:migrate:remote when the update adds migrations, commit and push the lock, then run npm run deploy. Check the update for new variables and secrets first: docs/deployment.md lists every key, and the deploy guard reports any required secret that is missing.

Support

Self-hosted installs get community support only: open a GitHub issue and we answer on a best-effort basis. See CONTRIBUTING.md. Do not report security problems in public issues; see SECURITY.md.

The Testosaurus name, logo and mascot are reserved; see TRADEMARKS.md before you publish an install under them.

Something unclear?Let us know