Does FastAPI support subdomains like Flask?
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from flask import Flask
app = Flask(__name__)
# Add these definitions to the hosts file:
# local.dev 127.0.0.1
# api.local.dev 127.0.0.1
app.config["SERVER_NAME"] = "local.dev:5000"
@app.route("/")
def home():
return "Home page"
@app.route("/default-subdomain")
def default_subdomain():
return "Default subdomain"
@app.route("/", subdomain="api")
def api_home():
return "API home"
Description
Flask has subdomains for its routes: https://flask.palletsprojects.com/en/2.2.x/api/#url-route-registrations - does FastAPI have something similar? If not, and it has to be done with a Nginx reverse proxy and two separate FastAPI instances (e.g. default subdomain on port 8000 and the api subdomain on port 8001), how would I go about sharing internal state e.g. auth tokens and user id between two different FastAPI apps?
Operating System
Linux, Windows
Operating System Details
No response
FastAPI Version
0.79.0
Python Version
3.10.5
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Sub Applications - Mounts - FastAPI
If you need to have two independent FastAPI applications, with their own independent OpenAPI and their own docs UIs, you can have a...
Read more >Flask vs FastAPI? : r/Python - Reddit
One thing I like about FastAPI is the built-in support for Pydantic. I like how the docs use Pydantic models to encourage using...
Read more >Moving from Flask to FastAPI - TestDriven.io
Unlike Flask, FastAPI does not have a built-in development server, so an ASGI server like Uvicorn or Daphne is required.
Read more >Configuring CORS in FastAPI - StackHawk
So for convenience, presumably at least, FastAPI provides Starlette's CORS middleware at `fastapi.middleware.cors` , you can of course also use ...
Read more >Practical Introduction to FastAPI | by Similoluwa Okunowo
FastAPI is based on Starlette which makes it incredibly FAST, and on par with Node. · FastAPI supports robust data validation for APIs...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can do it with reverse proxy. I just think that web framework should perform only the necessary functions. A framework should be fast and lightweight. He should not become a monster trying to capture everything and everywhere, and do the best. We saw what happened to the performance of django, although it brought a lot of things. I hope the original idea of FastAPI stays with him. Why bother with routing if there are already wonderful solutions on the market.
FastAPI has no support for subdomains. You don’t necessarily have to use two FastAPI instances if you use NGINX. You could just point
subdomain.example.com
toapi.example.com/router/
andexample.com
toapi.example.com
. You just have to make sure that all your subdomain paths are properly mapped, so I would suggest you would use a separateAPIRouter
with a fixedprefix=
.It might get confusing, and also note that when people go to
example.com/router/endpoint
that it would be a valid request. This solution is just some sugarcoating in that respect.