By default, FastAPI appears to try to bind to port 8000. This port is in use on my development server.
I am trying to run this example from the documentation homepage.
https://fastapi.tiangolo.com/#typer-the-fastapi-of-clis
# fastapi_webserver.py
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def read_root():
return {
'hello': 'world',
}
@app.get('/items/{item_id}')
def read_item(item_id: int, q: str|None = None):
return {
'item_id': item_id,
'q': q,
}
This is how I run the application from the CLI.
fastapi dev fastapi_webserver.py
How can I change the port to some other value, for example 5000
.
One possible method is to pass an additional command line argument. --port
I found this by first running fastapi dev fastapi_webserver.py --help
which prints some additional help information.
$ fastapi dev fastapi_webserver.py --port 5000
There might be some alternative methods, for example changing the port number in the Python code.