question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does FastAPI support subdomains like Flask?

See original GitHub issue

First 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:open
  • Created a year ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kieledcommented, Dec 16, 2022

Hi. I am aware it exists in Flask, but I am asking when it will be implemented in FastAPI.

Understand that it will never be implemented. This is not a responsibility that any web framework should have. There are already reverse proxies for this, such as traefik or nginx

Understand that when doing it this way (e.g. using URI paths for different subdomains in nginx/traefik config), all routes from the same web app will become available on both the main domain and any other subdomains you have set up this way. This is the whole reason why almost all other frameworks have support for this.

I don’t understand why you are talking down this feature when all the major and widely used frameworks have this implemented. It has existed for years.

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.

1reaction
JarroVGITcommented, Aug 18, 2022

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 to api.example.com/router/ and example.com to api.example.com. You just have to make sure that all your subdomain paths are properly mapped, so I would suggest you would use a separate APIRouter with a fixed prefix=.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found