mirror of
https://github.com/fastapi/fastapi.git
synced 2026-06-16 03:19:51 -04:00
422 lines
13 KiB
Markdown
422 lines
13 KiB
Markdown
# First Steps { #first-steps }
|
|
|
|
The simplest FastAPI file could look like this:
|
|
|
|
{* ../../docs_src/first_steps/tutorial001_py310.py *}
|
|
|
|
Copy that to a file `main.py`.
|
|
|
|
Run the live server:
|
|
|
|
<div class="termy">
|
|
|
|
```console
|
|
$ <font color="#4E9A06">fastapi</font> dev
|
|
|
|
<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.
|
|
```
|
|
|
|
</div>
|
|
|
|
In the output, there's a line with something like:
|
|
|
|
```hl_lines="4"
|
|
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
|
```
|
|
|
|
That line shows the URL where your app is being served on your local machine.
|
|
|
|
### Check it { #check-it }
|
|
|
|
Open your browser at [http://127.0.0.1:8000](http://127.0.0.1:8000).
|
|
|
|
You will see the JSON response as:
|
|
|
|
```JSON
|
|
{"message": "Hello World"}
|
|
```
|
|
|
|
### Interactive API docs { #interactive-api-docs }
|
|
|
|
Now go to [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs).
|
|
|
|
You will see the automatic interactive API documentation (provided by [Swagger UI](https://github.com/swagger-api/swagger-ui)):
|
|
|
|

|
|
|
|
### Alternative API docs { #alternative-api-docs }
|
|
|
|
And now, go to [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc).
|
|
|
|
You will see the alternative automatic documentation (provided by [ReDoc](https://github.com/Rebilly/ReDoc)):
|
|
|
|

|
|
|
|
### OpenAPI { #openapi }
|
|
|
|
**FastAPI** generates a "schema" with all your API using the **OpenAPI** standard for defining APIs.
|
|
|
|
#### "Schema" { #schema }
|
|
|
|
A "schema" is a definition or description of something. Not the code that implements it, but just an abstract description.
|
|
|
|
#### API "schema" { #api-schema }
|
|
|
|
In this case, [OpenAPI](https://github.com/OAI/OpenAPI-Specification) is a specification that dictates how to define a schema of your API.
|
|
|
|
This schema definition includes your API paths, the possible parameters they take, etc.
|
|
|
|
#### Data "schema" { #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-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.
|
|
|
|
#### Check the `openapi.json` { #check-the-openapi-json }
|
|
|
|
If you are curious about how the raw OpenAPI schema looks like, FastAPI automatically generates a JSON (schema) with the descriptions of all your API.
|
|
|
|
You can see it directly at: [http://127.0.0.1:8000/openapi.json](http://127.0.0.1:8000/openapi.json).
|
|
|
|
It will show a JSON starting with something like:
|
|
|
|
```JSON
|
|
{
|
|
"openapi": "3.1.0",
|
|
"info": {
|
|
"title": "FastAPI",
|
|
"version": "0.1.0"
|
|
},
|
|
"paths": {
|
|
"/items/": {
|
|
"get": {
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful Response",
|
|
"content": {
|
|
"application/json": {
|
|
|
|
|
|
|
|
...
|
|
```
|
|
|
|
#### What is OpenAPI for { #what-is-openapi-for }
|
|
|
|
The OpenAPI schema is what powers the two 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.
|
|
|
|
### Configure the app `entrypoint` in `pyproject.toml` { #configure-the-app-entrypoint-in-pyproject-toml }
|
|
|
|
You can configure where your app is located in a `pyproject.toml` file like:
|
|
|
|
```toml
|
|
[tool.fastapi]
|
|
entrypoint = "main:app"
|
|
```
|
|
|
|
That `entrypoint` will tell the `fastapi` command that it should import the app like:
|
|
|
|
```python
|
|
from main import app
|
|
```
|
|
|
|
If your code was structured like:
|
|
|
|
```
|
|
.
|
|
├── backend
|
|
│ ├── main.py
|
|
│ ├── __init__.py
|
|
```
|
|
|
|
Then you would set the `entrypoint` as:
|
|
|
|
```toml
|
|
[tool.fastapi]
|
|
entrypoint = "backend.main:app"
|
|
```
|
|
|
|
which would be equivalent to:
|
|
|
|
```python
|
|
from backend.main import app
|
|
```
|
|
|
|
### `fastapi dev` with path or with `--entrypoint` CLI option { #fastapi-dev-with-path-or-with-entrypoint-cli-option }
|
|
|
|
You can also pass the file path to the `fastapi dev` command, and it will guess the FastAPI app object to use:
|
|
|
|
```console
|
|
$ fastapi dev main.py
|
|
```
|
|
|
|
Or, you can also pass the `--entrypoint` option to the `fastapi dev` command:
|
|
|
|
```console
|
|
$ fastapi dev --entrypoint main:app
|
|
```
|
|
|
|
But you would have to remember to pass the correct path\entrypoint every time you call the `fastapi` command.
|
|
|
|
Additionally, other tools might not be able to find it, for example the [VS Code Extension](../editor-support.md) or [FastAPI Cloud](https://fastapicloud.com), so it is recommended to use the `entrypoint` in `pyproject.toml`.
|
|
|
|
### Deploy your app (optional) { #deploy-your-app-optional }
|
|
|
|
You can optionally deploy your FastAPI app to [FastAPI Cloud](https://fastapicloud.com) with a single command. 🚀
|
|
|
|
<div class="termy">
|
|
|
|
```console
|
|
$ fastapi deploy
|
|
|
|
Deploying to FastAPI Cloud...
|
|
|
|
✅ Deployment successful!
|
|
|
|
🐔 Ready the chicken! Your app is ready at https://myapp.fastapicloud.dev
|
|
```
|
|
|
|
</div>
|
|
|
|
The CLI will automatically detect your FastAPI application and deploy it to the cloud. If you are not logged in, your browser will open to complete the authentication process.
|
|
|
|
That's it! Now you can access your app at that URL. ✨
|
|
|
|
## Recap, step by step { #recap-step-by-step }
|
|
|
|
### Step 1: import `FastAPI` { #step-1-import-fastapi }
|
|
|
|
{* ../../docs_src/first_steps/tutorial001_py310.py hl[1] *}
|
|
|
|
`FastAPI` is a Python class that provides all the functionality for your API.
|
|
|
|
/// note | Technical Details
|
|
|
|
`FastAPI` is a class that inherits directly from `Starlette`.
|
|
|
|
You can use all the [Starlette](https://www.starlette.dev/) functionality with `FastAPI` too.
|
|
|
|
///
|
|
|
|
### Step 2: create a `FastAPI` "instance" { #step-2-create-a-fastapi-instance }
|
|
|
|
{* ../../docs_src/first_steps/tutorial001_py310.py hl[3] *}
|
|
|
|
Here the `app` variable will be an "instance" of the class `FastAPI`.
|
|
|
|
This will be the main point of interaction to create all your API.
|
|
|
|
### Step 3: create a *path operation* { #step-3-create-a-path-operation }
|
|
|
|
#### Path { #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
|
|
```
|
|
|
|
/// note
|
|
|
|
A "path" is also commonly called an "endpoint" or a "route".
|
|
|
|
///
|
|
|
|
While building an API, the "path" is the main way to separate "concerns" and "resources".
|
|
|
|
#### Operation { #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".
|
|
|
|
---
|
|
|
|
When building APIs, you normally use these specific HTTP methods to perform a specific action.
|
|
|
|
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".
|
|
|
|
We are going to call them "**operations**" too.
|
|
|
|
#### Define a *path operation decorator* { #define-a-path-operation-decorator }
|
|
|
|
{* ../../docs_src/first_steps/tutorial001_py310.py hl[6] *}
|
|
|
|
The `@app.get("/")` tells **FastAPI** that the function right below is in charge of handling requests that go to:
|
|
|
|
* the path `/`
|
|
* using a <dfn title="an HTTP GET method"><code>get</code> operation</dfn>
|
|
|
|
/// note | `@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**".
|
|
|
|
///
|
|
|
|
You can also use the other operations:
|
|
|
|
* `@app.post()`
|
|
* `@app.put()`
|
|
* `@app.delete()`
|
|
|
|
And the more exotic ones:
|
|
|
|
* `@app.options()`
|
|
* `@app.head()`
|
|
* `@app.patch()`
|
|
* `@app.trace()`
|
|
|
|
/// tip
|
|
|
|
You are free to use each operation (HTTP method) as you wish.
|
|
|
|
**FastAPI** doesn't enforce any specific meaning.
|
|
|
|
The information here is presented as a guideline, not a requirement.
|
|
|
|
For example, when using GraphQL you normally perform all the actions using only `POST` operations.
|
|
|
|
///
|
|
|
|
### Step 4: define the **path operation function** { #step-4-define-the-path-operation-function }
|
|
|
|
This is our "**path operation function**":
|
|
|
|
* **path**: is `/`.
|
|
* **operation**: is `get`.
|
|
* **function**: is the function below the "decorator" (below `@app.get("/")`).
|
|
|
|
{* ../../docs_src/first_steps/tutorial001_py310.py hl[7] *}
|
|
|
|
This is a Python function.
|
|
|
|
It will be called by **FastAPI** whenever it receives a request to the URL "`/`" using a `GET` operation.
|
|
|
|
In this case, it is an `async` function.
|
|
|
|
---
|
|
|
|
You could also define it as a normal function instead of `async def`:
|
|
|
|
{* ../../docs_src/first_steps/tutorial003_py310.py hl[7] *}
|
|
|
|
/// note
|
|
|
|
If you don't know the difference, check the [Async: *"In a hurry?"*](../async.md#in-a-hurry).
|
|
|
|
///
|
|
|
|
### Step 5: return the content { #step-5-return-the-content }
|
|
|
|
{* ../../docs_src/first_steps/tutorial001_py310.py hl[8] *}
|
|
|
|
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).
|
|
|
|
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.
|
|
|
|
### Step 6: Deploy it { #step-6-deploy-it }
|
|
|
|
Deploy your app to **[FastAPI Cloud](https://fastapicloud.com)** with one command: `fastapi deploy`. 🎉
|
|
|
|
#### About FastAPI Cloud { #about-fastapi-cloud }
|
|
|
|
**[FastAPI Cloud](https://fastapicloud.com)** is built by the same author and team behind **FastAPI**.
|
|
|
|
It streamlines the process of **building**, **deploying**, and **accessing** an API with minimal effort.
|
|
|
|
It brings the same **developer experience** of building apps with FastAPI to **deploying** them to the cloud. 🎉
|
|
|
|
FastAPI Cloud is the primary sponsor and funding provider for the *FastAPI and friends* open source projects. ✨
|
|
|
|
#### Deploy to other cloud providers { #deploy-to-other-cloud-providers }
|
|
|
|
FastAPI is open source and based on standards. You can deploy FastAPI apps to any cloud provider you choose.
|
|
|
|
Follow your cloud provider's guides to deploy FastAPI apps with them. 🤓
|
|
|
|
## Recap { #recap }
|
|
|
|
* Import `FastAPI`.
|
|
* Create an `app` instance.
|
|
* Write a **path operation decorator** using decorators like `@app.get("/")`.
|
|
* Define a **path operation function**; for example, `def root(): ...`.
|
|
* Run the development server using the command `fastapi dev`.
|
|
* Optionally deploy your app with `fastapi deploy`.
|