# Proxy one Cloudflare Worker route to a FastAPI backend

Gregale · Updated 2026-11-30

Keep your Cloudflare Worker and public URL. Send one path to FastAPI on Gregale and verify the PDF response and origin authentication.

Yes. A Cloudflare Worker can call FastAPI over HTTPS for one route while continuing to handle every other request. Keep your public domain, routing and user-facing policies at Cloudflare. Deploy the Python endpoint separately on Gregale and have the Worker forward only that path.

In this example, `GET /api/demo.pdf` uses a Python library to generate a one-page PDF. `GET /` still runs in the Worker. No DNS change, database migration, or browser CORS setup is needed: the browser calls the Worker, and the Worker calls FastAPI.

[View the runnable example on GitHub](https://github.com/gregale-dev/cloudflare-worker-fastapi) or [download the ZIP](https://gregale.dev/examples/cloudflare-worker-fastapi.zip).

| Keep at Cloudflare                                                    | Deploy on Gregale                               |
| --------------------------------------------------------------------- | ----------------------------------------------- |
| The Worker, its URL and existing routes                               | The FastAPI route and its Python dependency     |
| Browser-facing auth, rate limits and caching policies you already use | A separate credential check on the PDF endpoint |

## 1. Run the Worker and FastAPI locally

Unzip the example and follow its README to start FastAPI on `127.0.0.1:8080` and Wrangler on `127.0.0.1:8787`. Generate a random 32-byte token, encoded as hex, for `ORIGIN_TOKEN`. The README sets it in the backend environment and the Worker's ignored `.dev.vars` file. Run `python smoke.py` in the example's Python environment.

The Worker sends its own token in `X-Origin-Token`; it doesn't forward browser headers, cookies or query parameters. It requests a fixed path at an origin you configure, and doesn't follow redirects. FastAPI rejects a missing or wrong token before generating the PDF.

## 2. Deploy the FastAPI origin

Use a **new test app**, not a production app. Install and sign in to the [Gregale CLI](https://gregale.dev/docs/quickstart.md). From the example's `backend/` directory:

```bash
APP="my-unique-worker-pdf-demo"
gregale deploy --create-only --name "$APP"
gregale env set --app "$APP" --interactive
gregale deploy --name "$APP" --wait
```

In the [secret entry prompt](https://gregale.dev/docs/cli-secrets.md), use `ORIGIN_TOKEN` and a **new** random secret, not the local token or your Gregale account API key. Save the deployment's HTTPS app origin. On this disposable app only, configure ingress so Cloudflare can reach it without a Gregale account credential. The example relies on FastAPI's token check to protect the PDF route:

```bash
gregale app update "$APP" --visibility public --public-auth open --no-require-authn
```

Before setting up the Worker, verify `/healthz` returns `200` and a direct request to `/api/demo.pdf` without the token returns `401`. If it returns a PDF, stop and check the deployed code and ingress. The health response doesn't prove the PDF route is protected.

## 3. Send only the PDF route to the origin

For a disposable hosted demo, choose a new Worker name in `worker/wrangler.jsonc`. Replace `ORIGIN_URL` with the Gregale HTTPS origin only, without a path. Don't set it to the public Worker URL, which can route back to the Worker. Keep `ORIGIN_TOKEN` out of `vars`; set the identical value as a [Cloudflare Worker secret](https://developers.cloudflare.com/workers/configuration/secrets/). Using an ignored `.env.production` file containing only that one secret, deploy from `worker/` with `npx wrangler deploy --secrets-file .env.production`. Delete the local production secret file after use.

For an existing Worker, copy the PDF branch into the existing handler after any required user authorization. Leave the original handler as the fallback for other paths. This pattern uses ordinary [Workers fetch](https://developers.cloudflare.com/workers/runtime-apis/fetch/); it doesn't require changing your DNS or moving KV, R2 or Durable Objects.

## 4. Verify both URLs

| Request                                                                      | Expected result                                                                     |
| ---------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| Worker `GET /`                                                               | `200` with the original Worker response                                             |
| Worker `GET /api/demo.pdf`                                                   | `200`, `application/pdf`, and one page reading “FastAPI PDF via Cloudflare Workers” |
| Worker `POST /api/demo.pdf`                                                  | `405`; the sample only supports GET                                                 |
| Gregale origin `GET /api/demo.pdf` without the token or with the wrong token | `401`, no PDF                                                                       |

A Worker `502` means the sample refused to return what it got from upstream or couldn't reach it. Check the origin URL, the shared token on both sides, and whether the origin returns `200 application/pdf` without a redirect. Inspect logs without printing the token.

The shared secret authenticates the **Worker to FastAPI**, not your users. The sample deliberately makes the synthetic PDF public through the Worker. For private data, enforce real user authorization before forwarding and rate-limit the public route. Keep private results out of edge caches. The sample sets `Cache-Control: private, no-store`, but check that any custom cache rules respect it.

**What was tested:** We ran the smoke test against local workerd (the Cloudflare Workers runtime) and FastAPI. It checked Worker routing, the exact PDF text, and origin credential rejection. Hosted Cloudflare-to-Gregale setup is not verified by that test. If your workload already fits comfortably inside Workers, keep it there rather than add another origin. For a broader platform decision, see [Cloudflare Workers alternatives](https://gregale.dev/articles/cloudflare-workers-alternatives).

## Questions

### Can a Cloudflare Worker call a FastAPI backend?

Yes. A Worker can fetch an HTTPS FastAPI origin for one route and keep handling all other paths itself.

### Does the browser need CORS for this setup?

Not if it calls the Worker on the same origin. The Worker-to-FastAPI request runs server-side.

Canonical URL: https://gregale.dev/articles/cloudflare-worker-proxy-fastapi

