Deploy your frontend
A production build of your app needs two values: your deployment’s API URL and its anon key. This page shows how each host gets them, and what you see when one is missing.
Pre-1.0: APIs and config may change between releases.
What a build needs
supatype init, supatype dev and supatype keys write a local stack’s URL and anon key to .env.development.local. Vite, Next.js and Expo read that file in development only, so a production build never ships localhost, and gets neither value from it. A production build gets them in one of three ways:
| Way | Use it when |
|---|---|
supatype build | You run the build command, on any host. It gives the build both values under every framework’s name, and refuses to start without them. |
supatype keys --print frontend | You paste the values into a host’s settings. It prints each name and value as a NAME=value line. |
<script src="/supatype-env.js" vite-ignore> | Supatype serves the app, or it is a Next.js app with the route below. The page reads both values from the server when it loads, so one build runs on every deployment that serves it. |
Each framework reads its own names. supatype build and keys --print frontend give all of them:
| Framework | API URL | Anon key |
|---|---|---|
| Vite | VITE_SUPATYPE_URL | VITE_SUPATYPE_ANON_KEY |
| SvelteKit, Astro | PUBLIC_SUPATYPE_URL | PUBLIC_SUPATYPE_ANON_KEY |
| Next.js | NEXT_PUBLIC_SUPATYPE_URL | NEXT_PUBLIC_SUPATYPE_ANON_KEY |
| Expo | EXPO_PUBLIC_SUPATYPE_URL | EXPO_PUBLIC_SUPATYPE_ANON_KEY |
Pass the values straight to createClient, as Client: Create the client does, with no check of your own in front of it. A bundler that knows a name is unset can turn such a check into a throw that hides the reason. The client checks for itself, and names what is missing.
supatype build
$ supatype build # runs the package.json build script$ supatype build -- vite build # or the command after --$ supatype build --env staging -- npm run build # for one linked environmentKeep the real build command, such as vite build, in package.json’s build script. A build script that is itself supatype build is refused, since a bare supatype build runs that script: it stops and says the build script calls supatype build itself, so running it again would never end. A command after -- runs even inside another build.
It runs your build with the deployment’s API URL and anon key under all four prefixes. It writes no file: the values live only in the build’s environment. The exit code is the build’s own, so CI behaves as it did before. Each value is looked for on its own, in this order:
| Source | Where it looks |
|---|---|
| The environment | SUPATYPE_URL and SUPATYPE_ANON_KEY, or any of the names above. A host’s own settings always win. |
| The server the URL names | For the anon key only, when the environment holds a URL: <URL>/supatype-env.json, the key that server publishes. HTTPS for an address that is not local, a local one only with --allow-local. A 503 there reads the server at <url> is not set up to publish its anon key: and the setting the server names. |
| The linked environment | The one --env names, else the link’s default. Supatype Cloud: the environment’s address, and its anon key from the control plane, which needs supatype login (or SUPATYPE_ACCESS_TOKEN) as an owner or admin. Self-host: the link’s address, and the anon key the server publishes at /supatype-env.json, fetched only over HTTPS for an address that is not local. |
.env | PUBLIC_SUPATYPE_URL or API_EXTERNAL_URL, and ANON_KEY, as the server’s own .env holds them. |
The first source holding a value decides it. A local address there, such as localhost or 127.0.0.1, stops the build rather than being passed over, because a production build would ship it. Pass --allow-local to build against a local stack anyway. A service role key offered as the anon key stops the build too: the service role key never reaches a build. An anon key counts only for the URL being built for: with SUPATYPE_URL naming one deployment and a link to another, the link’s key is listed as skipped ... not the URL being built for, never paired with the wrong address.
.supatype/link.json holds secrets and is never committed, so a CI host has no link. There, set SUPATYPE_URL in the host’s settings: supatype build fetches the anon key from <SUPATYPE_URL>/supatype-env.json, copies both to every framework’s names, and stops the build when one is missing. Set SUPATYPE_ANON_KEY as well only when that server does not publish it (server.public_env: false, or a plain http:// address). supatype deploy builds your app with the same values, and takes --allow-local too.
Supatype hosts it
A missing page on a plain static site serves index.html, on a self-hosted stack and on Supatype Cloud alike, so a single-page app’s routes load on a refresh. Set spa: false in the build section of supatype.config.ts to answer 404 instead, and spa: true to turn the fallback on for a framework whose Cloud default is off. supatype deploy sends the setting to Cloud and supatype self-host compose gives it to the self-hosted server. With nothing set, both hosts take the same default from the framework: on for a plain static site, Vite and Remix SPA, off for Next.js, Astro, SvelteKit and Nuxt, whose pages are pre-rendered. A deploy with the fallback on refuses when the build has no index.html at its root: add one, or set spa: false.
When a self-hosted stack serves your app (app.mode: "static"), its server answers /supatype-env.js with its own address and anon key:
$ curl https://example.com/supatype-env.jswindow.__SUPATYPE_ENV__ = {"url":"https://example.com","anonKey":"eyJhbGciOi..."};supatype init puts the tag in the head of the index.html it writes for a static app, so it runs before the app’s own script. It never changes an index.html that is already there, such as the one in a Vite app made before init ran: it prints skipped index.html (already exists) and adds no tag. Until you add the tag, a build made without the values throws MissingClientValuesError on its first request. For such an app, or one made another way, add it yourself, before the app’s own script:
<script src="/supatype-env.js" vite-ignore></script><script type="module" src="/src/main.ts"></script>Load it as a plain script, without async or defer, before the app’s own script. A plain script in the head has always run before any module script, wherever the app puts its own; with async it can run after it. When it has not run by the client’s first request, that request throws, saying the page loads the script but it set no window.__SUPATYPE_ENV__ in time.
vite-ignore tells Vite to leave the tag as it is. The project has no such file, since the server answers it, so without the attribute every vite build warns <script src="/supatype-env.js"> in "/index.html" can't be bundled without type="module" attribute. Vite drops the attribute from the page it builds, and a browser ignores it, so the same tag does for a page with no build step. Do not make it type="module": Vite then tries to bundle it, and the build fails with Failed to resolve /supatype-env.js.
The client reads window.__SUPATYPE_ENV__ for any value the build did not carry, at its first request. It never guesses the address from the page. A value the build did carry always wins. So a build made with neither value, by a plain npm run build, runs on every deployment that serves it. Served from the API’s own origin, it needs no allowed origin either. To build on the server instead:
$ supatype app add --static ./dist # once, then commit supatype.config.ts$ supatype build # reads API_EXTERNAL_URL and ANON_KEY from .env$ supatype self-host compose up -dNext.js
A Next.js app does not need its values at build time. next build prerenders pages without them, and the route app/supatype-env.js/route.ts serves them to each page load from SUPATYPE_URL and SUPATYPE_ANON_KEY in the server’s environment. createServerClient reads the same two, for Server Components, Route Handlers and middleware. supatype init writes the route and the root layout’s script for an App Router app; Frameworks: Next.js shows both, and the server client. So one build runs on every environment: set the two variables where the app runs. In development the route answers too: supatype dev, init and keys --write put the local stack’s SUPATYPE_URL and SUPATYPE_ANON_KEY in .env.development.local, which next dev reads.
| Host | What to set |
|---|---|
| Vercel | SUPATYPE_URL and SUPATYPE_ANON_KEY under the project’s Environment Variables, for each environment. The build command stays next build. |
| Netlify | The same two, under the site’s Environment variables. |
| Your own Node server | The same two, in the environment of the process that runs next start. |
$ npm run build$ SUPATYPE_URL=https://api.example.com SUPATYPE_ANON_KEY=eyJhbGciOi... npm startsupatype keys --print frontend prints the values to copy. The route is read on every page load and the server client on every call, so the same build started with another deployment’s two values runs against that one. A value fixed into the build under NEXT_PUBLIC_SUPATYPE_URL or NEXT_PUBLIC_SUPATYPE_ANON_KEY still wins over the route’s, so leave those unset in production. A Next.js app on its own host or server is on another origin from the API: add its address to the API’s allowed origins, as in Allow your app’s origin. The server client uses them only where SUPATYPE_URL or SUPATYPE_ANON_KEY is unset.
A static export (output: "export" in next.config) has no server to run the route, so init writes no route, no layout script and no @supatype/ssr for it, and prints the two ways instead: build with the values, supatype build -- next build, or have Supatype host the export (supatype app add --static ./out), whose server answers /supatype-env.js itself; add the <Script> tag to the root layout for that.
A Pages Router app gets nothing written by init, which prints these steps instead: create the same route file under app/supatype-env.js/, beside pages/; in pages/_document.tsx, import Script from next/script and put <Script src="/supatype-env.js" strategy="beforeInteractive" /> inside <Head>; add @supatype/ssr to the app’s dependencies, and set the two variables where it runs.
Expo
An Expo Router app has no index.html. Its web page is rendered from app/+html.tsx, or src/app/+html.tsx when the routes are in src/app. supatype init puts the script first in that file’s <head>, and when the app has no such file, makes it from the template the installed Expo ships, as npx expo customize +html.tsx does. A web build then reads both values from the server that hosts it when the page loads, so npx expo export --platform web needs neither. The tag is left out of development pages: expo start --web serves no such file, and a development build reads its values from .env.development.local. In an app init did not set up, add the tag yourself:
<head> {__DEV__ ? null : <script src="/supatype-env.js" />} <meta charSet="utf-8" />Expo renders +html.tsx only when web.output in app.json is "static" or "server", as a new app’s is. With "single", put <script src="/supatype-env.js" vite-ignore></script> in the head of public/index.html instead. A "static" export is plain files: have Supatype host them with supatype app add --static ./dist. A "server" export, the default from SDK 58, renders each page in a Node server (npx expo serve); the Supatype server in front of it answers /supatype-env.js itself, whatever the app mode. On any other host, build with the values instead.
A native build, for iOS or Android and on EAS, has no page, so it takes its values at build time under EXPO_PUBLIC_SUPATYPE_URL and EXPO_PUBLIC_SUPATYPE_ANON_KEY. expo export and EAS builds run in production mode, which never reads .env.development.local, where supatype dev and keys put the local stack’s values. Give them your deployment’s: on a linked machine, build through supatype build; for an EAS build, set the two names in the project’s EAS environment variables, with the values supatype keys --print frontend --env production prints.
$ supatype build -- npx expo export --platform androidAn Expo page with neither value makes the client’s first request throw, naming app/+html.tsx and the build-time names rather than index.html.
Vercel
Print the values for the deployment on your own machine, where the project is linked:
$ supatype keys --print frontend --env productionVITE_SUPATYPE_URL=https://api.example.comPUBLIC_SUPATYPE_URL=https://api.example.comNEXT_PUBLIC_SUPATYPE_URL=https://api.example.comEXPO_PUBLIC_SUPATYPE_URL=https://api.example.comVITE_SUPATYPE_ANON_KEY=eyJhbGciOi...PUBLIC_SUPATYPE_ANON_KEY=eyJhbGciOi...NEXT_PUBLIC_SUPATYPE_ANON_KEY=eyJhbGciOi...EXPO_PUBLIC_SUPATYPE_ANON_KEY=eyJhbGciOi...In the Vercel project’s settings, under Environment Variables, add SUPATYPE_URL with that value, for each environment you deploy. supatype build fetches the anon key from the server it names; add SUPATYPE_ANON_KEY too if that server does not publish it. Then have Vercel build through the wrapper:
{ "buildCommand": "npx supatype build"}supatype build copies the two values to NEXT_PUBLIC_, VITE_ and the other names, then runs your build script. A Next.js app needs no wrapper on Vercel: see Next.js.
Then allow the site on the API: add its *.vercel.app address and any custom domain to the allowed origins, as in Allow your app’s origin.
Netlify
Under the site’s configuration, in Environment variables, add SUPATYPE_URL, and SUPATYPE_ANON_KEY only if the server does not publish its key, with the values supatype keys --print frontend prints. Then set the build command:
[build] command = "npx supatype build" publish = "dist"publish is your build’s output directory: dist for Vite, build for SvelteKit’s static adapter. Then add the site’s *.netlify.app address and any custom domain to the API’s allowed origins, as in Allow your app’s origin.
Cloudflare Pages
In the Pages project’s settings:
| Setting | Value |
|---|---|
| Build command | npx supatype build |
| Build output directory | Your build’s output, such as dist |
| Variables and Secrets | SUPATYPE_URL, from supatype keys --print frontend, and SUPATYPE_ANON_KEY only if the server does not publish its key |
Then add the project’s *.pages.dev address and any custom domain to the API’s allowed origins, as in Allow your app’s origin.
GitHub Actions
Store SUPATYPE_URL as a repository variable, under Settings, Secrets and variables, Actions. supatype build fetches the anon key from the server it names, so a self-hosted deployment needs nothing more: no link and no service role key in CI. Where the server does not publish the key, store SUPATYPE_ANON_KEY too; it is public, so a variable, not a secret, will do.
name: Build
on: push: branches: [main]
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 22 - run: npm ci - run: npx supatype build env: SUPATYPE_URL: ${{ vars.SUPATYPE_URL }} # Only where the server does not publish its key: # SUPATYPE_ANON_KEY: ${{ vars.SUPATYPE_ANON_KEY }}Publish the output however you deploy it, and add the address it is served from to the API’s allowed origins (Allow your app’s origin). To give every later step the values under every framework’s name instead, run npx supatype keys --print frontend >> "$GITHUB_ENV" with the same env:. To deploy to your own server over SSH and build there, follow Deploy to a VM with GitHub Actions.
Your own server
On the server that runs the stack, supatype build needs nothing more: its .env records the deployment’s API_EXTERNAL_URL and ANON_KEY.
$ cd /opt/my-app$ git pull$ npm install$ supatype push$ supatype build$ supatype self-host compose up -dTo build somewhere else, such as your laptop, set SUPATYPE_URL to the deployment’s address, or link the deployment and name it:
$ supatype link --env production --url https://api.example.com --token "$SERVICE_ROLE_KEY"$ supatype build --env productionThen copy the output to whatever serves it, and add that server’s address to the API’s allowed origins (Allow your app’s origin); built on the stack’s own server and served by it, the app needs nothing. For a self-hosted link the anon key comes from https://api.example.com/supatype-env.json. It is fetched over HTTPS only, so a link to a plain http:// address that is not local gives no key; set SUPATYPE_ANON_KEY instead. A Supatype Cloud link reads the key from the control plane.
Allow your app's origin
A self-hosted API answers browsers only from the origin of its SITE_URL and the origins you list. An app served from anywhere else, such as Vercel, Netlify, Cloudflare Pages, a Next.js server of its own or a dev server on another port, is refused: the preflight gets 403, and the browser reports Failed to fetch. When Supatype serves the app, it is on the API’s own origin and needs nothing here.
List the app’s origin in supatype.config.ts, or in .env as SUPATYPE_CORS_ALLOW_ORIGINS, comma-separated. Each entry is scheme://host[:port], with no path:
// supatype.config.ts (excerpt)api: { corsOrigins: ["https://my-app.vercel.app", "https://www.example.com"] },SUPATYPE_CORS_ALLOW_ORIGINS=https://my-app.vercel.app,https://www.example.com$ supatype self-host compose restart serverThe restart renders the compose file again and recreates the server with the new list. Owners, admins and developers can instead add the origin in Studio, on the Settings page’s CORS tab, which needs no restart; a list saved there replaces the default until it is reset, and the site URL stays allowed whatever is saved. On Supatype Cloud, add it there. Each preview deployment with its own address is another origin. supatype dev allows every origin, so a local stack never shows this. The rest of the rules are under Self-hosting, in Allowed origins.
When a value is missing
At build time, supatype build stops before the build starts, exits 1, and lists every source it tried for each value. With nothing set, in a project that is not linked:
$ supatype build✗ No API URL and no anon key for a production build of the front end. not set API URL from the environment: none of SUPATYPE_URL, VITE_SUPATYPE_URL, PUBLIC_SUPATYPE_URL, NEXT_PUBLIC_SUPATYPE_URL, EXPO_PUBLIC_SUPATYPE_URL is set not set anon key from the environment: none of SUPATYPE_ANON_KEY, VITE_SUPATYPE_ANON_KEY, PUBLIC_SUPATYPE_ANON_KEY, NEXT_PUBLIC_SUPATYPE_ANON_KEY, EXPO_PUBLIC_SUPATYPE_ANON_KEY is set not set anon key from the server SUPATYPE_URL names: no API URL in the environment to ask not set API URL from the linked environment: no .supatype/link.json (run `supatype link`) not set anon key from the linked environment: no .supatype/link.json (run `supatype link`) not set API URL from .env: no PUBLIC_SUPATYPE_URL or API_EXTERNAL_URL not set anon key from .env: no ANON_KEY
Run `supatype build --env <name>` for a linked deployment, or set SUPATYPE_URL and SUPATYPE_ANON_KEY.The first line starts with ✗ in a terminal, and with [supatype] in a CI log or anywhere else the output is not a terminal. With SUPATYPE_URL set it starts No anon key for a production build of the front end., and the line for the server the URL names says what that server answered.
supatype keys --print frontend reads the same sources, refuses the same way, and names itself as the command to run. It takes --allow-local too, for printing a local stack’s values: supatype keys --print frontend --allow-local. At run time, creating the client never throws, so next build can prerender a page that creates one at module scope. The first request a client makes without the anon key throws a MissingClientValuesError instead, so the browser console says what is wrong rather than the page going blank. The error starts Supatype client has no anon key. createClient was given none, and the page had set no window.__SUPATYPE_ENV__ by its first request. and goes on to name the build-time variables, supatype build, the script tag, and for Next.js the route and the server’s SUPATYPE_URL and SUPATYPE_ANON_KEY. With no address either, it starts Supatype client has no API address and anon key. A page that already loads /supatype-env.js is not told to add it. When the server could not give the values, the script sets error in their place, and the first request quotes it and sends nothing, on every path: tables, rpc, functions, GraphQL, auth, realtime and storage. When the page has the script element but no window.__SUPATYPE_ENV__ at all, the script did not run: it was blocked, failed to load, ran after the app’s own script, or came from a server too old to serve it. The error says so, and to open the script in the browser to see what it answers.
The anon key is public
The anon key is not a secret. Every browser that loads your app holds it, which is why the server can publish it and a CI host can keep it as a plain variable. What protects your data is the access rules in your schema, which every request made with the anon key goes through.
The service role key passes every rule, so it never goes near a build. supatype build and keys --print frontend never output it, and refuse a service role key offered as the anon key. The server never serves it.
/supatype-env.json and /supatype-env.js are on by default. For an API-only project, with no frontend for the server to serve, turn them off:
// supatype.config.ts (excerpt)server: { public_env: false,},The next supatype self-host compose up -d gives the server SUPATYPE_PUBLIC_ENV=false, and both addresses answer 404. A deployment that means to publish and cannot (no anon key, no URL, or a service role key in SUPATYPE_ANON_KEY) answers /supatype-env.json with 503 instead, with no caching and a body naming every setting to fix, and /supatype-env.js with 200, not cached, setting only error to the same text, which the browser client throws on its first request. supatype dev does the same. Turning them off does not make the anon key secret: every client of the API still holds it. It only stops the server publishing it, and supatype build for that deployment then needs SUPATYPE_ANON_KEY set.