Tutorial - User Guide¶
This tutorial shows you how to useFastAPI with most of its features, step by step.
Each section gradually builds on the previous ones, but it's structured to separate topics, so that you can go directly to any specific one to solve your specific API needs.
It is also built to work as a future reference so you can come back and see exactly what you need.
Run the code¶
All the code blocks can be copied and used directly (they are actually tested Python files).
To run any of the examples, copy the code to a filemain.py, and startfastapi dev with:
$<fontcolor="#4E9A06">fastapi</font>dev<ustyle="text-decoration-style:solid">main.py</u> <span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀 Searching for package file structure from directories with <font color="#3465A4">__init__.py</font> files Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font> <span style="background-color:#007166"><font color="#D3D7CF"> module </font></span> 🐍 main.py <span style="background-color:#007166"><font color="#D3D7CF"> code </font></span> Importing the FastAPI app object from the module with the following code: <u style="text-decoration-style:solid">from </u><u style="text-decoration-style:solid"><b>main</b></u><u style="text-decoration-style:solid"> import </u><u style="text-decoration-style:solid"><b>app</b></u> <span style="background-color:#007166"><font color="#D3D7CF"> app </font></span> Using import string: <font color="#3465A4">main:app</font> <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000/docs</u></font> <span style="background-color:#007166"><font color="#D3D7CF"> tip </font></span> Running in development mode, for production use: <b>fastapi run</b> Logs: <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Will watch for changes in these directories: <b>[</b><font color="#4E9A06">'/home/user/code/awesomeapp'</font><b>]</b> <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <b>(</b>Press CTRL+C to quit<b>)</b> <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started reloader process <b>[</b><font color="#34E2E2"><b>383138</b></font><b>]</b> using WatchFiles <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>383153</b></font><b>]</b> <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup. <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.It isHIGHLY encouraged that you write or copy the code, edit it and run it locally.
Using it in your editor is what really shows you the benefits of FastAPI, seeing how little code you have to write, all the type checks, autocompletion, etc.
Install FastAPI¶
The first step is to install FastAPI.
Make sure you create avirtual environment, activate it, and theninstall FastAPI:
$pipinstall"fastapi[standard]"---> 100%Note
When you install withpip install "fastapi[standard]" it comes with some default optional standard dependencies, includingfastapi-cloud-cli, which allows you to deploy toFastAPI Cloud.
If you don't want to have those optional dependencies, you can instead installpip install fastapi.
If you want to install the standard dependencies but without thefastapi-cloud-cli, you can install withpip install "fastapi[standard-no-fastapi-cloud-cli]".
Advanced User Guide¶
There is also anAdvanced User Guide that you can read later after thisTutorial - User guide.
TheAdvanced User Guide builds on this one, uses the same concepts, and teaches you some extra features.
But you should first read theTutorial - User Guide (what you are reading right now).
It's designed so that you can build a complete application with just theTutorial - User Guide, and then extend it in different ways, depending on your needs, using some of the additional ideas from theAdvanced User Guide.







