All documentation

Frontend, backend and database from separate repositories

The “Full project” wizard creates the frontend, the API and the database and links them with addresses and DATABASE_URL.

If the frontend and the backend live in different repositories, the “Full project” wizard is the easiest way: it creates three resources and links them with variables.

The “Full project” wizard

“New resource” → the “Frontend + backend + database” card. The wizard asks for:

  1. a project name, for example shop;
  2. the frontend repository link;
  3. the backend repository link;
  4. a database: PostgreSQL, MySQL or none;
  5. optionally, your own domain.

What gets created

ResourceWhat it is
shop-weban app from the frontend Git repository
shop-apian app from the backend Git repository
shop-dba managed database

All three share a project label: in the “Resources” list they are shown as one group.

The wizard sets the variables itself — as references to project resources, not as addresses:

ResourceVariables
backendDATABASE_URL=${{shop-db.DATABASE_URL}} — the database connection string; CORS_ORIGIN=${{shop-web.URLS}} and FRONTEND_URL=${{shop-web.URL}} — the frontend addresses
frontendVITE_API_URL, NEXT_PUBLIC_API_URL and REACT_APP_API_URL — ${{shop-api.URL}}, the API address

A reference is stored as a template, and the container gets the value. When the API domain, the database address or password changes, the neighbours' variables update themselves: the backend restarts, and a frontend with the reference in VITE_* is rebuilt. DATABASE_URL is filled in once the database is up.

Custom domain

If you enter the domain example.com, the frontend gets app.example.com and the API gets api.example.com. The “Domains” tab of each resource shows which DNS records to add at your registrar. See Custom domain and HTTPS.

Frontend code

Take the API address from your bundler's variable:

// Vite
const res = await fetch(`${import.meta.env.VITE_API_URL}/api/items`);

In Next.js it is process.env.NEXT_PUBLIC_API_URL, in create-react-app process.env.REACT_APP_API_URL. These variables are substituted at build time: with a reference ${{shop-api.URL}} a frontend from Git is rebuilt automatically when the API address changes, otherwise it needs a new deploy. More in React, Vue and Vite.

CORS on the backend

The frontend and the API live at different addresses, so the backend must allow requests from the frontend address.

Express:

import cors from 'cors';
app.use(cors({ origin: process.env.CORS_ORIGIN }));

Fastify:

import cors from '@fastify/cors';
await app.register(cors, { origin: process.env.CORS_ORIGIN });

Django (the django-cors-headers package):

INSTALLED_APPS += ["corsheaders"]
MIDDLEWARE.insert(0, "corsheaders.middleware.CorsMiddleware")
CORS_ALLOWED_ORIGINS = [os.environ["CORS_ORIGIN"]]

Environments and shared variables

The project page (“Projects” in the menu) shows the environments — production first — with their resources and shared variables:

  • ${{project.KEY}} — a project variable, visible to all environments;
  • ${{env.KEY}} — a variable of one environment;
  • ${{name.URL}}, ${{name.URLS}}, ${{name.HOST}}, ${{name.PORT}}, ${{name.DATABASE_URL}} — a resource of the same environment.
  • ${{name.PRIVATE_URL}} (http://address:port) and ${{name.PRIVATE_HOST}} — the resource address in the account private network: apps of an environment call each other directly, not through the public address. The address changes with a new container, and references in neighbouring resources are updated automatically.

Secret variables are stored encrypted. A reference works only inside its environment and account; an unknown or circular reference is an error under the field.

“New environment” → copy from production creates staging: copies of the resources without data, databases with new passwords, plans can be smaller, copies sleep without visitors. The price is shown before creation; copies are billed like ordinary resources and count toward the account quotas. References in the copies point to the copies, so staging never talks to the production database.

By hand, without the wizard

  1. “New resource” → “Database” → PostgreSQL or MySQL.
  2. “New resource” → “App” from the backend repository.
  3. In the database: “Connection” → choose the backend → “Connect”.
  4. “New resource” → “App” from the frontend repository.
  5. In the frontend's “Variables” tab set VITE_API_URL (or your bundler's variable) to the backend address from its card, and run a new deploy.
  6. In the backend's “Variables” tab set CORS_ORIGIN to the frontend address.
  7. Custom domains go in the “Domains” tab of each app.

Simpler: one Next.js app

If the project is just starting, the frontend and the API can live in one Next.js app — no CORS and no second deploy. See Next.js.