Introduction
FastAPI can autogenerate your documentation when you are using FastAPI to create an API.
I am trying to insert an image in the description (markdown) of one of my endpoints, but I can't do it when the image is located in the local hardrive.
I have tried to insert it directly (view the end of this post), but it doesn't work.
I have tried to create an endpoint to serve the images, in this case it only works if the IP of the address is my public IP. It doesn't work if I put localhost
or 127.0.0.1
. I think I am missing something here.
Minimal example
Installation:
$ pip install fastapi
$ pip install "uvicorn[standard]"
File: main.py
from fastapi import FastAPI
from fastapi.responses import FileResponse
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/my-endpoint")
def example_function():
"""
Documentation for my enpoint. Insert some images
1) This online image works:

2) This local image doesn't work:

3) This local image served by the api works if the link is to the public IP:

4) This local image served by the api doesn't work because when specified as localhost:


"""
return {"This is my endpoint"}
# An endpoint to serve images for documentation
@app.get("/img/example-photo.jpg")
async def read_image():
return FileResponse("example-photo.jpg")
Execute the API with the following command:
$ uvicorn main:app --reload --host 0.0.0.0 --port 8000
You can access the automatic documentation at:
http://<you-ip>:8000/docs
Result of the example
Extra
In my real case the folder structure is the following:
/myProject/
|
|---/docs/
| |---/img/
| |---example-photo.jpg
|
|---/src/
|---/myApp/
|----main.py
If I try to insert the image directly it doesn't show anything.
