Skip to content

Cloudflare setup

Create the Cloudflare resources for a self-hosted GoPayLocal, write wrangler.toml from the example, set secrets, and deploy the API, the dashboard and the admin app.

Page actions

This page deploys the API Worker and the dashboard (and optionally the admin app) to your Cloudflare account. Do Database and migrations first, or at least create the database, because Hyperdrive needs its connection string.

Examples use example.com; replace it with your domain. All pnpm wrangler commands run from apps/api and use the Wrangler version pinned in the repository.

Fork the repository if you want to keep your configuration in Git (recommended), then clone your fork or the original:

Terminal window
git clone https://github.com/RayLabsHQ/gopaylocal.git
cd gopaylocal
corepack enable
pnpm install

corepack enable is needed once per machine; it makes pnpm the exact version pinned in package.json. Node 26 does not include Corepack: run npm install -g corepack first.

Terminal window
cd apps/api
pnpm wrangler login

This opens a browser to authorize Wrangler for your account.

3. Create Hyperdrive and the KV namespaces

Section titled “3. Create Hyperdrive and the KV namespaces”

Start your configuration from the example. It replaces the wrangler.toml that ships in the repository, which holds the hosted service’s resource ids:

Terminal window
cp wrangler.example.toml wrangler.toml

Create a Hyperdrive configuration for your database:

Terminal window
pnpm wrangler hyperdrive create gopaylocal-db \
--connection-string="postgres://USER:PASSWORD@HOST:5432/DATABASE?sslmode=require"

Create the two KV namespaces:

Terminal window
pnpm wrangler kv namespace create CACHE
pnpm wrangler kv namespace create VPN_DATA

Each command prints an id. Paste them into wrangler.toml in place of <HYPERDRIVE_ID>, <CACHE_KV_ID> and <VPN_DATA_KV_ID>. Keep the binding names (HYPERDRIVE, CACHE, VPN_DATA) exactly as they are; the code reads them by name.

In the [vars] section:

  • API_BASE_URL: the public URL of the API, for example https://api.example.com. Sign-in callbacks and provider webhooks use it.
  • APP_BASE_URL: the public URL of the dashboard, for example https://app.example.com.
  • ADMIN_BASE_URL: the public URL of the admin app, if you deploy it.
  • SELF_HOSTED = "true": leave it as it is.

You decide these URLs now, before anything is deployed. They must be on one domain; see Domains and sign-in cookies.

Also check:

  • Rate limiters. The nine [[ratelimits]] blocks need nothing created. Their namespace_id values (1001 to 1009) must be unique within your Cloudflare account, so change them if another Worker in the account already uses those numbers.
  • Cron triggers. Keep all three. On Workers Free, the daily VPN refresh cannot finish; see Cloudflare plan and step 9.
  • Placement. The commented-out [placement] block can run the Worker near your database. Cloudflare marks host-based placement as experimental (Placement). It is optional.
  • Custom Domain. To serve the API on api.example.com, uncomment the routes line near the top. See step 10.

Configuration reference explains every entry.

Terminal window
openssl rand -base64 32 # run once per secret and paste the output when asked
pnpm wrangler secret put BETTER_AUTH_SECRET
pnpm wrangler secret put TOKEN_ENCRYPTION_KEY

The first secret put asks whether to create the Worker, because it does not exist yet. Answer yes.

Add the OAuth credentials as well; OAuth setup shows how to get them:

and/or
pnpm wrangler secret put GITHUB_CLIENT_ID
pnpm wrangler secret put GITHUB_CLIENT_SECRET
pnpm wrangler secret put GOOGLE_CLIENT_ID
pnpm wrangler secret put GOOGLE_CLIENT_SECRET

Leave the Dodo Payments secrets unset; self-hosted mode does not use them.

Apply the migrations if you have not yet (Database and migrations). Then, from the repository root:

Terminal window
pnpm build:banner
cd apps/api
pnpm run deploy

pnpm build:banner creates packages/banner/dist/banner.js. The deploy copies it into apps/api/static, which Cloudflare serves as static files (/v1/banner/script.js). Without the build, the deploy stops with a message that says to run it. Build it again after every update.

To migrate, sync admins and deploy in one step, use pnpm run deploy:full instead (details).

Check that the API answers (use the workers.dev URL Wrangler printed, or your API hostname once step 10 is done):

Terminal window
curl https://api.example.com/health
# {"status":"ok","timestamp":"..."}

The dashboard is a static app. The API URL is compiled into it, so set VITE_API_URL when you build. Without it, the build points at http://localhost:8787. From the repository root:

Terminal window
VITE_API_URL=https://api.example.com pnpm --filter @gopaylocal/dashboard build
pnpm --filter @gopaylocal/dashboard run deploy:worker

apps/dashboard/wrangler.toml needs no changes. apps/dashboard/wrangler.example.toml shows the optional Custom Domain setting.

8. Build and deploy the admin app (optional)

Section titled “8. Build and deploy the admin app (optional)”
Terminal window
VITE_API_URL=https://api.example.com pnpm --filter @gopaylocal/admin build
pnpm --filter @gopaylocal/admin run deploy:worker

Only users with the admin role can use it; see First admin user.

  • Workers Paid: the daily cron (30 9 * * *, UTC) loads and refreshes the data. Nothing to do.
  • Workers Free: let the daily GitHub Actions job do it (VPN detection: refresh from GitHub Actions), or build the dataset on your computer and upload it with Wrangler now and then (by hand).

Until the data is loaded, VPN detection only checks the visitor’s network against a built-in list of datacenter and VPN networks.

Each Worker is reachable on its workers.dev hostname after deploy. To use your domain (whose zone must be active on Cloudflare), either:

  • add a Custom Domain to each Worker in the Cloudflare dashboard (Workers & Pages > your Worker > Settings > Domains & Routes), or
  • uncomment the routes entry in that app’s wrangler.toml and deploy again.

See Custom Domains. If a hostname changes, update API_BASE_URL, APP_BASE_URL or ADMIN_BASE_URL in apps/api/wrangler.toml, redeploy the API, and rebuild the dashboard and admin app with the new VITE_API_URL.

  1. OAuth setup, if you have not created the OAuth app yet.

  2. First admin user.

  3. Create a product, a campaign and an API key in the dashboard, and add the banner to a page. In the script tag, use your own API hostname:

    <script src="https://api.example.com/v1/banner/script.js?key=YOUR_PUBLIC_KEY" async></script>

    The campaign page in the dashboard shows this snippet with your key, product id and API hostname filled in. It takes the hostname from the VITE_API_URL the dashboard was built with. See Banner installation.

    The JavaScript SDK and the React provider default to the hosted API. Pass your API URL as apiBase: new GoPayLocal({ apiKey, apiBase: 'https://api.example.com' }), or <GoPayLocalProvider apiKey={...} apiBase="https://api.example.com">. See JS SDK and React provider.