Once these commands have been executed and dependencies have been installed, create and copy in the main.py file as illustrated below:
main.py
# FastApi Setup for the Quick Start Tutorialfrom fastapi import FastAPIfrom fastapi.middleware.cors import CORSMiddlewareimport uvicorn# Initiates the FastApi instanceapp =FastAPI()# Enabling CORS to allow access for the Client-side App.app.add_middleware( CORSMiddleware,allow_origins=["*"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)# Route for our API on index page to send over a simple message.@app.get("/")asyncdefindex():return{"message":"Welcome to IndustryApps!"}
This is a minimal setup to run a FastApi stateless Restful compliant API, this simply returns a message "Welcome to IndustryApps!" when fetched later on the client-side.
The command uvicorn main:app starts the API in case you'd like to test it, which refers to:
main: the file main.py (the Python "module").
app: the object created inside of main.py with the line app = FastAPI().
By following the link which pops up on terminal you should see the message displayed on the browser window:
FastApi automatically documents its endpoints. By adding /docs endpoint to your API url, the available endpoints of the API will be visible.
GET endpoint inside /docs of FastAPI
The application will be built and run through docker-compose later in the tutorial. The next step will be building the client-side app with Vue /Nuxt js.