2018-12-14 14:27:02 +04:00
The simplest FastAPI file could look like this:
``` Python
2018-12-21 16:22:33 +04:00
{ ! . / src / first_steps / tutorial001 . py ! }
2018-12-14 14:27:02 +04:00
```
Copy that to a file `main.py` .
Run the live server:
``` bash
2019-03-09 22:10:25 +04:00
uvicorn main:app --reload
2018-12-14 14:27:02 +04:00
```
!!! note
The command `uvicorn main:app` refers to:
* `main` : the file `main.py` (the Python "module").
* `app` : the object created inside of `main.py` with the line `app = FastAPI()` .
2019-03-09 22:10:25 +04:00
* `--reload` : make the server restart after code changes. Only use for development.
2018-12-14 14:27:02 +04:00
You will see an output like:
```hl_lines="4"
INFO: Started reloader process [17961]
INFO: Started server process [17962]
INFO: Waiting for application startup.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
` ``
That last line shows the URL where your app is being served, in your local machine.
### Check it
2020-01-17 16:24:05 +01:00
Open your browser at <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>.
2018-12-14 14:27:02 +04:00
You will see the JSON response as:
` ``JSON
2019-10-04 02:57:49 +03:00
{"message": "Hello World"}
2018-12-14 14:27:02 +04:00
` ``
### Interactive API docs
2020-01-17 16:24:05 +01:00
Now go to <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
2018-12-14 14:27:02 +04:00
2020-01-17 16:24:05 +01:00
You will see the automatic interactive API documentation (provided by <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">Swagger UI</a>):
2018-12-14 14:27:02 +04:00

### Alternative API docs
2020-01-17 16:24:05 +01:00
And now, go to <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>.
2018-12-14 14:27:02 +04:00
2020-01-17 16:24:05 +01:00
You will see the alternative automatic documentation (provided by <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">ReDoc</a>):
2018-12-14 14:27:02 +04:00

2018-12-28 16:32:03 +04:00
### OpenAPI
**FastAPI** generates a "schema" with all your API using the **OpenAPI** standard for defining APIs.
#### "Schema"
A "schema" is a definition or description of something. Not the code that implements it, but just the abstract description.
#### API "schema"
In this case, OpenAPI is a specification that dictates how to define a schema of your API.
2019-03-02 19:54:52 +04:00
This OpenAPI schema would include your API paths, the possible parameters they take, etc.
2018-12-28 16:32:03 +04:00
#### Data "schema"
The term "schema" might also refer to the shape of some data, like a JSON content.
In that case, it would mean the JSON attributes, and data types they have, etc.
#### OpenAPI and JSON Schema
OpenAPI defines an API schema for your API. And that schema includes definitions (or "schemas") of the data sent and received by your API using **JSON Schema**, the standard for JSON data schemas.
2020-01-17 16:24:05 +01:00
#### Check the ` openapi.json`
2018-12-28 16:32:03 +04:00
2018-12-14 17:10:08 +04:00
If you are curious about how the raw OpenAPI schema looks like, it is just an automatically generated JSON with the descriptions of all your API.
2020-01-17 16:24:05 +01:00
You can see it directly at: <a href="http://127.0.0.1:8000/openapi.json" class="external-link" target="_blank">http://127.0.0.1:8000/openapi.json</a>.
2018-12-14 17:10:08 +04:00
It will show a JSON starting with something like:
` ``JSON
{
"openapi": "3.0.2",
"info": {
2020-01-18 19:07:42 +01:00
"title": "FastAPI",
2018-12-14 17:10:08 +04:00
"version": "0.1.0"
},
"paths": {
"/items/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
...
` ``
2020-01-17 16:24:05 +01:00
#### What is OpenAPI for
2018-12-28 16:32:03 +04:00
This OpenAPI schema is what powers the 2 interactive documentation systems included.
And there are dozens of alternatives, all based on OpenAPI. You could easily add any of those alternatives to your application built with **FastAPI**.
You could also use it to generate code automatically, for clients that communicate with your API. For example, frontend, mobile or IoT applications.
2018-12-14 14:27:02 +04:00
## Recap, step by step
### Step 1: import ` FastAPI`
` ``Python hl_lines="1"
2018-12-21 16:22:33 +04:00
{!./src/first_steps/tutorial001.py!}
2018-12-14 14:27:02 +04:00
` ``
` FastAPI` is a Python class that provides all the functionality for your API.
2018-12-15 14:03:59 +04:00
!!! note "Technical Details"
` FastAPI` is a class that inherits directly from ` Starlette`.
You can use all the Starlette functionality with ` FastAPI` too.
2018-12-14 14:27:02 +04:00
### Step 2: create a ` FastAPI` "instance"
` ``Python hl_lines="3"
2018-12-21 16:22:33 +04:00
{!./src/first_steps/tutorial001.py!}
2018-12-14 14:27:02 +04:00
` ``
Here the ` app` variable will be an "instance" of the class ` FastAPI`.
2018-12-15 14:03:59 +04:00
This will be the main point of interaction to create all your API.
2018-12-14 14:27:02 +04:00
2018-12-15 20:05:24 +04:00
This ` app` is the same one referred by ` uvicorn` in the command:
2018-12-14 14:27:02 +04:00
` ``bash
2019-03-09 22:10:25 +04:00
uvicorn main:app --reload
2018-12-14 14:27:02 +04:00
` ``
If you create your app like:
` ``Python hl_lines="3"
2018-12-21 16:22:33 +04:00
{!./src/first_steps/tutorial002.py!}
2018-12-14 14:27:02 +04:00
` ``
And put it in a file ` main.py`, then you would call ` uvicorn` like:
` ``bash
2019-03-09 22:10:25 +04:00
uvicorn main:my_awesome_api --reload
2018-12-14 14:27:02 +04:00
` ``
2020-01-18 16:21:54 +01:00
### Step 3: create a *path operation*
2018-12-15 14:03:59 +04:00
#### Path
"Path" here refers to the last part of the URL starting from the first ` /`.
So, in a URL like:
` ``
https://example.com/items/foo
` ``
...the path would be:
` ``
/items/foo
` ``
!!! info
A "path" is also commonly called an "endpoint" or a "route".
2018-12-23 18:42:29 +04:00
Building an API, the "path" is the main way to separate "concerns" and "resources".
2018-12-15 14:03:59 +04:00
#### Operation
"Operation" here refers to one of the HTTP "methods".
One of:
* ` POST`
* ` GET`
* ` PUT`
* ` DELETE`
...and the more exotic ones:
* ` OPTIONS`
* ` HEAD`
* ` PATCH`
* ` TRACE`
In the HTTP protocol, you can communicate to each path using one (or more) of these "methods".
---
2018-12-23 18:42:29 +04:00
When building APIs, you normally use these specific HTTP methods to perform a specific action.
2018-12-15 14:03:59 +04:00
Normally you use:
* ` POST`: to create data.
* ` GET`: to read data.
* ` PUT`: to update data.
* ` DELETE`: to delete data.
So, in OpenAPI, each of the HTTP methods is called an "operation".
2018-12-23 18:42:29 +04:00
We are going to call them "**operations**" too.
2018-12-15 14:03:59 +04:00
2020-01-18 16:21:54 +01:00
#### Define a *path operation function*
2018-12-14 14:27:02 +04:00
` ``Python hl_lines="6"
2018-12-21 16:22:33 +04:00
{!./src/first_steps/tutorial001.py!}
2018-12-14 14:27:02 +04:00
` ``
2018-12-15 14:03:59 +04:00
The ` @app .get("/")` tells **FastAPI** that the function right below is in charge of handling requests that go to:
2018-12-14 14:27:02 +04:00
2018-12-15 14:03:59 +04:00
* the path ` /`
* using a <abbr title="an HTTP GET method"><code>get</code> operation</abbr>
2018-12-23 18:42:29 +04:00
!!! info "` @decorator ` Info"
That ` @something ` syntax in Python is called a "decorator".
You put it on top of a function. Like a pretty decorative hat (I guess that's where the term came from).
A "decorator" takes the function below and does something with it.
In our case, this decorator tells **FastAPI** that the function below corresponds to the **path** ` /` with an **operation** ` get`.
It is the "**path operation decorator**".
2018-12-15 14:03:59 +04:00
You can also use the other operations:
2018-12-14 21:35:19 +04:00
* ` @app .post()`
* ` @app .put()`
* ` @app .delete()`
2018-12-15 14:03:59 +04:00
And the more exotic ones:
2018-12-14 21:35:19 +04:00
* ` @app .options()`
* ` @app .head()`
* ` @app .patch()`
* ` @app .trace()`
2018-12-15 14:03:59 +04:00
!!! tip
You are free to use each operation (HTTP method) as you wish.
2020-01-17 16:24:05 +01:00
2018-12-15 14:03:59 +04:00
**FastAPI** doesn't enforce any specific meaning.
The information here is presented as a guideline, not a requirement.
2018-12-23 18:42:29 +04:00
For example, when using GraphQL you normally perform all the actions using only ` post`.
### Step 4: define the **path operation function**
2018-12-15 14:03:59 +04:00
2018-12-23 18:42:29 +04:00
This is our "**path operation function**":
* **path**: is ` /`.
* **operation**: is ` get`.
* **function**: is the function below the "decorator" (below ` @app .get("/")`).
2018-12-14 14:27:02 +04:00
` ``Python hl_lines="7"
2018-12-21 16:22:33 +04:00
{!./src/first_steps/tutorial001.py!}
2018-12-14 14:27:02 +04:00
` ``
2020-01-17 16:24:05 +01:00
This is a Python function.
2018-12-14 14:27:02 +04:00
2018-12-23 18:42:29 +04:00
It will be called by **FastAPI** whenever it receives a request to the URL "` /`" using ` GET`.
2018-12-14 14:27:02 +04:00
In this case, it is an ` async` function.
---
You could also define it as a normal function instead of ` async def`:
` ``Python hl_lines="7"
2018-12-21 16:22:33 +04:00
{!./src/first_steps/tutorial003.py!}
2018-12-14 14:27:02 +04:00
` ``
2018-12-23 18:42:29 +04:00
!!! note
2020-01-18 16:21:54 +01:00
If you don't know the difference, check the [Async: *"In a hurry?"*](../async.md#in-a-hurry){.internal-link target=_blank}.
2018-12-14 14:27:02 +04:00
### Step 5: return the content
` ``Python hl_lines="8"
2018-12-21 16:22:33 +04:00
{!./src/first_steps/tutorial001.py!}
2018-12-14 14:27:02 +04:00
` ``
You can return a ` dict`, ` list`, singular values as ` str`, ` int`, etc.
You can also return Pydantic models (you'll see more about that later).
2018-12-23 18:42:29 +04:00
There are many other objects and models that will be automatically converted to JSON (including ORMs, etc). Try using your favorite ones, it's highly probable that they are already supported.
## Recap
* Import ` FastAPI`.
* Create an ` app` instance.
* Write a **path operation decorator** (like ` @app .get("/")`).
* Write a **path operation function** (like ` def root(): ...` above).
2020-01-17 16:24:05 +01:00
* Run the development server (like ` uvicorn main:app --reload`).