Articles

Deploy React and FastAPI on one domain without CORS

Deploy a React Vite frontend and a FastAPI backend separately, send /api to Python with Gregale edge routing, and verify both apps on one hostname.

Updated

Deploy React and FastAPI as two separate apps behind one origin. Gregale serves your React build at / and routes /api and its subpaths to FastAPI. The browser can keep using fetch('/api/message'), so this request does not need CORS configuration. You can update the backend without rebuilding the frontend.

View the starter on GitHub · Download the starter ZIP

Why the Vite proxy stops working after a build

Vite's server.proxy runs on the development server. It does not ship in dist/. If fetch('/api/message') works locally but returns a 404 or HTML after deploy, make production route /api to the backend instead of sending it to the static frontend.

Browser requestApp that serves it
GET / and GET /assets/…React frontend
GET /api, GET /api/message?name=GregaleFastAPI backend
POST /api/echoFastAPI backend

1. Deploy the two apps

Install the Gregale CLI and run the commands from the cloned or extracted starter. Pick unique app slugs in the same account or organization. The frontend Dockerfile builds React and serves the generated files with a small Node HTTP server. The backend Dockerfile starts FastAPI with Uvicorn.

bash
WEB=choose-a-unique-web-slug
API=choose-a-unique-api-slug

gregale deploy --source=worktree --path frontend --name "$WEB"
gregale deploy --source=worktree --path backend --name "$API"

The starter has no real data. To test it without browser credentials, open only these disposable demo apps:

bash
gregale app update "$WEB" --public-auth open --no-require-authn
gregale app update "$API" --public-auth open --no-require-authn

For your own app, keep user authentication and authorization in place. The public API hostname is still callable when the API is open. Same-origin routing does not make it private, and an account API key should never go into a React bundle.

2. Route /api to FastAPI

Use an account API key in the CLI, not a token scoped to one app. Add a Gregale route rule on the frontend's exact hostname:

bash
HOST="$WEB.gregale.app"

gregale edge rule create --app "$WEB" --kind route \
  --name react-fastapi-api --match-host "$HOST" --match-path '/api/**' \
  --route-target-slug "$API"

/api/** matches both /api and paths below it. The rule forwards the path without stripping /api and without redirecting the browser. The example's FastAPI handlers are mounted under that prefix. To use app.example.com, attach and verify the custom domain on the frontend, then add the rule for that hostname too. A rule for the default hostname does not automatically match a custom one.

3. Verify it in the browser

Open https://$HOST/ and click Check API. The page should show Hello, Gregale, from FastAPI (api-v1). In DevTools, look for a 200 JSON response from https://$HOST/api/message?name=Gregale, with no redirect. Check the response body, not just the status: an overly broad frontend fallback can return HTML with a 200.

bash
curl -i "https://$HOST/api/absent"
curl -i "https://$HOST/assets/absent.js"

Both requests should return 404. The first should be FastAPI JSON; the second should come from the frontend. If you only see frontend HTML, check the hostname, path, target slug, and the target application's authorization. When finished, remove the disposable apps or restore bearer protection.

Hosted test results

In Chromium against two disposable Gregale apps, clicking Check API returned FastAPI JSON on the React hostname with no redirect or Access-Control-Allow-Origin header. The exact /api path and a JSON POST reached FastAPI; a missing API route returned JSON 404 and a missing asset returned the frontend's 404. After promoting only the FastAPI release, the same page showed api-v2; the React deployment ID stayed the same.

This test used Gregale's default hostname on a Scale account. It did not exercise custom-domain DNS or a fresh Free account.

If your frontend needs to stay on Vercel, use the split Next.js and FastAPI guide instead. If the apps intentionally use different origins, configure CORS for the exact frontend origin.

Questions

Can I deploy React and FastAPI on one domain?

Yes. Deploy the React build and FastAPI as separate apps, then route /api and its subpaths on the frontend hostname to FastAPI. The browser keeps using that same hostname.

Why does the Vite API proxy stop working in production?

Vite server.proxy is a development server setting, not part of the built frontend. Add a production edge route for /api or configure a separate API origin and its CORS policy.

Does a same-origin edge route make a FastAPI backend private?

No. A publicly accessible FastAPI app can still be called through its own hostname. Use app authentication and authorization for protected data.