BetterRewardsDocs
Open app
Browse guides

Deploy on Cloudflare

Run betterrewards as a Cloudflare Worker, with Postgres on Neon through Hyperdrive and email from Cloudflare. One click, or step by step.

6 min read · For companies

betterrewards also runs as a Cloudflare Worker. The app, its background jobs (after each request and once a minute) and email all run on Cloudflare; the database is a Neon Postgres that Cloudflare reaches through Hyperdrive.

You needNotes
Cloudflare Workers PaidUSD 5 a month. The free plan allows too little CPU time per request for the dashboard.
Your domain on CloudflareThe domain's DNS managed by Cloudflare, for sending email (and later the dashboard address).
A Neon databaseneon.tech. The free tier is enough to start.
A Stripe accountConnected after the first sign-in, from the admin.

One click

The button copies the repository to your GitHub account and deploys it to your Cloudflare account. Have these ready:

  • A Neon database and its direct connection string (in Neon's Connect dialog, turn Connection pooling off), for the Hyperdrive step.
  • AUTH_SECRET: a long random value (openssl rand -base64 32). Keep a copy.
  • OWNER_EMAIL: your email address. Your first sign-in with it makes you the owner.
  • EMAIL_FROM: the sender of sign-in emails, for example Rewards <rewards@example.com>, on a domain you onboard in Cloudflare Email Service (step 4 below).
  • Workers Paid on the account. On the free plan the dashboard runs out of CPU time, and Email Service only delivers to addresses you verified, so partners would get no sign-in email.

Deploy to Cloudflare

After the deploy, turn off Hyperdrive's query caching (step 3 below explains why): npx wrangler hyperdrive update <id> --caching-disabled, or in the dashboard under Hyperdrive → your configuration → Settings. Until you do, the app shows a page asking for it. Then open the workers.dev address the deploy shows and sign in with OWNER_EMAIL. The first visit creates the database tables, so it takes a few seconds.

Your own domain needs no code change. In Workers → your Worker → Settings → Domains & Routes, add a custom domain such as rewards.example.com, then open the app there once. The app uses the address it is opened at, and from then on emails and short links use your domain instead of workers.dev. Add the domain before connecting Stripe if you can; if Stripe was connected first, save the key again in Integrations → Stripe and the webhook moves with it.

Other settings, such as TRACKING_DOMAIN or GOOGLE_CLIENT_ID, can be added in the dashboard under Settings → Variables and Secrets: deploys keep them. Background jobs start a few minutes after the first deploy, once Cloudflare activates the cron trigger.

The steps below do the same by hand, from your computer with Cloudflare's wrangler tool. Plan for about 45 minutes.

1. Get the code

You need Node 24 (then run corepack enable for pnpm) and git.

git clone https://github.com/betterrewards/betterrewards.git
cd betterrewards
corepack enable && pnpm install
cd apps/app
npx wrangler login           # opens the browser to sign in to Cloudflare

Run the commands in the next steps from apps/app.

2. Create the database

At neon.tech, create a project in a region close to your users. Copy the direct connection string: in Connect, turn Connection pooling off. It looks like postgres://user:password@ep-….neon.tech/neondb?sslmode=require (no -pooler in the host). The Worker creates its tables itself on the first visit.

3. Connect Cloudflare to the database

Hyperdrive keeps connections to Neon open close to your Worker. Turn its query cache off: betterrewards reads back what it just wrote (sign-in links, balances, settings), and a cached answer would be out of date.

npx wrangler hyperdrive create betterrewards \
  --connection-string='postgres://…neon.tech/neondb?sslmode=require' --caching-disabled

Copy the id it prints.

4. Set up email

Sign-in is by email, so this step is required. Pick one:

  • Cloudflare Email Service: in the Cloudflare dashboard, Compute → Email Service → Email Sending → Onboard Domain, for the domain you send from. Cloudflare adds the DNS records. Keep EMAIL_TRANSPORT=cloudflare in the next step.
  • Resend: verify your domain at resend.com and create an API key. In the next step, set EMAIL_TRANSPORT=resend and delete the send_email block.

5. Configure the Worker

cp wrangler.example.jsonc wrangler.jsonc

Open wrangler.jsonc and change:

WhereValue
hyperdrive → idThe id from step 3
vars → APP_URLOptional. Without it the app uses the address it is opened at, preferring your custom domain over workers.dev
vars → EMAIL_FROMRewards <rewards@example.com>, on the domain from step 4
vars → EMAIL_TRANSPORTcloudflare or resend
routes → patternrewards.example.com (your dashboard host, on your Cloudflare zone)
Short links (optional)Uncomment TRACKING_DOMAIN in vars and the second route, with go.example.com

Leave triggers as it is: the every-minute trigger runs the background jobs.

6. Add the secrets

openssl rand -base64 32       # copy the output, keep a copy somewhere safe
npx wrangler secret put AUTH_SECRET          # paste it when asked
npx wrangler secret put OWNER_EMAIL          # your email address
npx wrangler secret put RESEND_API_KEY       # only with Resend

If wrangler asks whether to create the Worker, answer yes. Secrets are stored encrypted by Cloudflare and never appear in wrangler.jsonc. Keep AUTH_SECRET stable: changing it signs everyone out and makes saved Stripe keys and payout details unreadable.

7. Deploy and sign in

pnpm deploy:cf

This builds the Worker and uploads it. Cloudflare creates the DNS record and the certificate for the route; the first time can take a minute or two. Open your dashboard address, enter OWNER_EMAIL and click the link you receive. The first visit creates the database tables, so it takes a few seconds. The setup wizard then walks you through your company, Stripe, a first program and the tracking snippet; the Getting started guide for product owners explains each step.

Check that it works

  • The sign-in email arrives within a minute, in the inbox.
  • npx wrangler tail streams the logs. Within a few minutes, [jobs] tick lines show the background jobs running.
  • Integrations → Tracking & links: the tracking check turns green once the snippet is on your site.

Update to a new version

cd betterrewards
git pull && pnpm install
cd apps/app && pnpm deploy:cf

The first request after the deploy applies any database changes. Keep wrangler.jsonc: it is not part of the repository, so git pull leaves it alone. Compare it with wrangler.example.jsonc when the changelog mentions a change. With the button, pull the changes into your copy on GitHub instead; Cloudflare deploys them.

Troubleshooting

What you seeWhat to do
A page says Hyperdrive query caching must be turned offRun npx wrangler hyperdrive update <id> --caching-disabled (or use the dashboard), wait a minute and reload.
Error 1102, or Worker exceeded resource limitsThe account is on the free plan. Upgrade to Workers Paid.
hyperdrive create fails to connectUse the direct string (no -pooler) with sslmode=require, and check the password was copied whole.
Deploy fails on the routeThe host must be on a zone in the same Cloudflare account, and no other DNS record may use it.
No sign-in emailnpx wrangler tail shows the error. With Cloudflare Email Service, EMAIL_FROM must be on the onboarded domain; with Resend, on a verified one.
The email link opens the wrong addressIf you set APP_URL, it is wrong: fix it in wrangler.jsonc (or remove it, so the app uses the address it is opened at) and deploy again.
Commissions never get approvedBackground jobs are not running: check that triggers is still in wrangler.jsonc and that wrangler tail shows [jobs] lines.