🍉Unit 5: Hello World!
In this unit, you will take the first step towards understanding the program structure of FastAPI and learning how to run your program.
Let's create a new main.py
file and insert the following code into it:
To run the above program, open a CMD/Terminal and execute the command below:
The command uvicorn main:app
refers to:
main
: the filemain.py
(the Python "module").app
: the object created inside ofmain.py
with the lineapp = FastAPI()
.--reload
: make the server restart after code changes. Only use for development.
If your program is error-free, below is the result when executing the above command:
Open your browser at http://127.0.0.1:8000
and you will get a JSON response as:
To test the say_hello()
function, change the address to http://127.0.0.1:8000/hello/<name>
where <name>
is a variable.
Interactive API docs
FastAPI automatically generates an API Docs for your project, provided by Swagger UI. The API Docs is available at http://127.0.0.1:8000/docs
, as shown in Fig. 1.
Besides API Docs provided by Swagger UI, FastAPI also generate another API Docs provided by ReDoc, available at http://127.0.0.1:8000/redoc
, as shown in Fig. 2.
To ensure compatibility with standard HTTP APIs, FastAPI uses the OpenAPI standard for defining APIs.
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs. This allows both humans and computers to discover and understand the capabilities of a service without requiring access to source code, additional documentation, or inspection of network traffic. When properly defined via OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.
OpenAPI documents describe API services and are represented in YAML or JSON formats. These documents may be produced and served statically or generated dynamically from an application.
For our Hello World program, you can see openapi.json
directly at http://127.0.0.1:8000/openapi.json
.
Summary
The Hello World
program can best describe the procedure for programming your first APIs with FastAPI, which includes several important steps as follows:
Import FastAPI module
Create a FastAPI instance, for example,
app = FastAPI()
. You should note that theapp
variable is the same one referred byuvicorn
in the execution command.Create functions in conjunction with path operations.
Path refers to the last part of the URL starting from the first
/
, for example, given an URL ashttps://example.com/items/foo
, the path would be/items/foo
.Operation refers to the HTTP methods such as
POST
,GET
,PUT
,DELETE
.Define a path operation decorator as
@app.get("/")
. The@app.get("/")
tells FastAPI that the function right below is in charge of handling requests that go to the path/
using aGET
operation.
Define the path operation function that is specified below the decorator, for example,
async def root()
.Return the content such as a
dict
,list
, singular values asstr
,int
, etc. There are many other objects and models that will be automatically converted to JSON (including ORMs, etc).
A "path" is also commonly called an "endpoint" or a "route".
Last updated