You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The FastAPI package is supported in Python Workers.
15
15
16
-
FastAPI applications use a protocol called the [Asynchronous Server Gateway Interface (ASGI)](https://asgi.readthedocs.io/en/latest/). This means that FastAPI never reads from or writes to a socket itself. An ASGI application expects to be hooked up to an ASGI server, typically [uvicorn](https://uvicorn.dev/).
16
+
FastAPI applications use a protocol called the [Asynchronous Server Gateway Interface (ASGI)](https://asgi.readthedocs.io/en/latest/).
17
+
This means that FastAPI never reads from or writes to a socket itself. An ASGI application expects to be hooked up to an ASGI server,
18
+
typically [uvicorn](https://uvicorn.dev/).
17
19
The ASGI server handles all of the raw sockets on the application’s behalf.
18
20
19
-
The Python Workers provides[an ASGI server](https://github.com/cloudflare/workers-py/blob/main/packages/runtime-sdk/src/asgi.py)
21
+
The Python Workers provide[an ASGI server](https://github.com/cloudflare/workers-py/blob/main/packages/runtime-sdk/src/asgi.py)
20
22
that you can use directly in your Python Worker, which lets you use FastAPI in Python Workers.
21
23
22
-
## Get Started
24
+
## Quick Start
23
25
24
-
Clone the `cloudflare/python-workers-examples` repository and run the FastAPI example:
26
+
To get started with FastAPI in Python Workers, follow these steps:
25
27
28
+
2. Create a `src/main.py` file with your FastAPI application:
29
+
```python
30
+
from fastapi import FastAPI
31
+
32
+
app = FastAPI()
33
+
34
+
@app.get("/")
35
+
defread_root():
36
+
return {"Hello": "World"}
37
+
38
+
import asgi
39
+
from workers import WorkerEntrypoint
40
+
41
+
classDefault(WorkerEntrypoint):
42
+
asyncdeffetch(self, request):
43
+
returnawait asgi.fetch(app, request, self.env)
44
+
```
45
+
46
+
3. Create a `wrangler.jsonc` file to configure your Worker:
47
+
48
+
<WranglerConfig>
49
+
50
+
```jsonc
51
+
{
52
+
"name":"my-fastapi-app",
53
+
"main":"src/main.py",
54
+
"compatibility_date":"$today",
55
+
"compatibility_flags": ["python_workers"],
56
+
}
57
+
```
58
+
</WranglerConfig>
59
+
60
+
4. Create a `pyproject.toml` file to manage your dependencies:
You can serve a single-page application (SPA) or any static frontend alongside your FastAPI backend by using [Workers Static Assets](/workers/static-assets/).
85
+
86
+
This is equivalent to FastAPI's native [`app.frontend()`](https://fastapi.tiangolo.com/tutorial/frontend/) method, which serves a static build directory as low-priority routes so that API path operations are checked first. The difference is where the files live: `app.frontend()` reads files from the local filesystem, while on Workers the static assets are served from Cloudflare's globally distributed asset store through the `ASSETS` binding. This means your frontend files are not bundled inside the Worker itself, keeping the bundle small.
87
+
88
+
Place your frontend build output (for example, HTML, CSS, and JavaScript files) in a directory such as `./public/`. Then configure your Wrangler file with an `assets` block that includes a `binding` and sets `run_worker_first` to `true`. This ensures every request reaches your FastAPI Worker first, so your API routes take priority over static files.
89
+
90
+
Add a catch-all route at the end of your FastAPI app that proxies unmatched requests to the assets binding:
91
+
92
+
<WranglerConfig>
93
+
94
+
```jsonc
95
+
{
96
+
"name":"my-fastapi-app",
97
+
"main":"src/worker.py",
98
+
"compatibility_date":"$today",
99
+
"compatibility_flags": ["python_workers"],
100
+
"assets": {
101
+
"directory":"./public/",
102
+
"binding":"ASSETS",
103
+
"run_worker_first":true
104
+
}
105
+
}
106
+
```
107
+
</WranglerConfig>
108
+
109
+
Be sure to create a `pyproject.toml` file to manage your dependencies:
110
+
111
+
```toml
112
+
[project]
113
+
name = "my-fastapi-app"
114
+
version = "0.1.0"
115
+
requires-python = ">=3.13"
116
+
dependencies = [
117
+
"fastapi",
118
+
]
119
+
120
+
[dependency-groups]
121
+
dev = [
122
+
"workers-py",
123
+
"workers-runtime-sdk"
124
+
]
125
+
```
126
+
127
+
Then write your worker:
128
+
129
+
```python title="src/worker.py"
35
130
from workers import WorkerEntrypoint
36
131
from fastapi import FastAPI, Request
37
-
frompydanticimportBaseModel
132
+
fromfastapi.responsesimportResponse
38
133
import asgi
39
134
40
135
classDefault(WorkerEntrypoint):
@@ -43,33 +138,34 @@ class Default(WorkerEntrypoint):
43
138
44
139
app = FastAPI()
45
140
46
-
@app.get("/")
47
-
asyncdefroot():
48
-
return {"message": "Hello, World!"}
49
-
50
-
@app.get("/env")
51
-
asyncdefroot(req: Request):
52
-
env = req.scope["env"]
53
-
return {"message": "Here is an example of getting an environment variable: "+ env.MESSAGE}
You can run this worker locally using `uv run pywrangler dev`.
158
+
159
+
With this setup, a request to `/api/hello` is handled by FastAPI, while a request to `/index.html` or any other path is served from the `./public/` directory through the assets binding.
160
+
161
+
For more information on configuring static assets, refer to the [Workers Static Assets documentation](/workers/static-assets/).
162
+
163
+
## More examples
164
+
165
+
Clone the `cloudflare/python-workers-examples` repository and run the FastAPI examples there:
0 commit comments