Skip to content
Discussion options

You must be logged in to vote

Let's break these down because they are fundamentally different concepts. First the APIRouter. The APIRouter just provides a way to breakup the application without having to share a global FastAPI instance around. In a small app you add routes like this:

from fastapi import FastAPI

app = FastAPI()

@app.get('/users')
def list_users():
    return

@app.get('/posts')
def list_posts():
    return

But now pretend you want to keep adding routes and you want to separate your user routes and your posts routes. The following 3 modules create an app that is functionally identical to the single file app above.

The posts route file posts.py

from fastapi import APIRouter

router = APIRouter()

@rout…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@nesbyte
Comment options

@r-bar
Comment options

Answer selected by nesbyte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants