1# Features 2 3## FastAPI features 4 5**FastAPI** gives you the following: 6 7### Based on open standards 8 9* <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>OpenAPI</strong></a> for API creation, including declarations of <abbr title="also known as: endpoints, routes">path</abbr> <abbr title="also known as HTTP methods, as POST, GET, PUT, DELETE">operations</abbr>, parameters, body requests, security, etc. 10* Automatic data model documentation with <a href="https://json-schema.org/" class="external-link" target="_blank"><strong>JSON Schema</strong></a> (as OpenAPI itself is based on JSON Schema). 11* Designed around these standards, after a meticulous study. Instead of an afterthought layer on top. 12* This also allows using automatic **client code generation** in many languages. 13 14### Automatic docs 15 16Interactive API documentation and exploration web user interfaces. As the framework is based on OpenAPI, there are multiple options, 2 included by default. 17 18* <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank"><strong>Swagger UI</strong></a>, with interactive exploration, call and test your API directly from the browser. 19 20![Swagger UI interaction](https://fastapi.tiangolo.com/img/index/index-03-swagger-02.png) 21 22* Alternative API documentation with <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank"><strong>ReDoc</strong></a>. 23 24![ReDoc](https://fastapi.tiangolo.com/img/index/index-06-redoc-02.png) 25 26### Just Modern Python 27 28It's all based on standard **Python 3.6 type** declarations (thanks to Pydantic). No new syntax to learn. Just standard modern Python. 29 30If you need a 2 minute refresher of how to use Python types (even if you don't use FastAPI), check the short tutorial: [Python Types](python-types.md){.internal-link target=_blank}. 31 32You write standard Python with types: 33 34```Python 35from datetime import date 36 37from pydantic import BaseModel 38 39# Declare a variable as a str 40# and get editor support inside the function 41def main(user_id: str): 42 return user_id 43 44 45# A Pydantic model 46class User(BaseModel): 47 id: int 48 name: str 49 joined: date 50``` 51 52That can then be used like: 53 54```Python 55my_user: User = User(id=3, name="John Doe", joined="2018-07-19") 56 57second_user_data = { 58 "id": 4, 59 "name": "Mary", 60 "joined": "2018-11-30", 61} 62 63my_second_user: User = User(**second_user_data) 64``` 65 66!!! info 67 `**second_user_data` means: 68 69 Pass the keys and values of the `second_user_data` dict directly as key-value arguments, equivalent to: `User(id=4, name="Mary", joined="2018-11-30")` 70 71### Editor support 72 73All the framework was designed to be easy and intuitive to use, all the decisions were tested on multiple editors even before starting development, to ensure the best development experience. 74 75In the last Python developer survey it was clear <a href="https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features" class="external-link" target="_blank">that the most used feature is "autocompletion"</a>. 76 77The whole **FastAPI** framework is based to satisfy that. Autocompletion works everywhere. 78 79You will rarely need to come back to the docs. 80 81Here's how your editor might help you: 82 83* in <a href="https://code.visualstudio.com/" class="external-link" target="_blank">Visual Studio Code</a>: 84 85![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png) 86 87* in <a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">PyCharm</a>: 88 89![editor support](https://fastapi.tiangolo.com/img/pycharm-completion.png) 90 91You will get completion in code you might even consider impossible before. As for example, the `price` key inside a JSON body (that could have been nested) that comes from a request. 92 93No more typing the wrong key names, coming back and forth between docs, or scrolling up and down to find if you finally used `username` or `user_name`. 94 95### Short 96 97It has sensible **defaults** for everything, with optional configurations everywhere. All the parameters can be fine-tuned to do what you need and to define the API you need. 98 99But by default, it all **"just works"**. 100 101### Validation 102 103* Validation for most (or all?) Python **data types**, including: 104 * JSON objects (`dict`). 105 * JSON array (`list`) defining item types. 106 * String (`str`) fields, defining min and max lengths. 107 * Numbers (`int`, `float`) with min and max values, etc. 108 109* Validation for more exotic types, like: 110 * URL. 111 * Email. 112 * UUID. 113 * ...and others. 114 115All the validation is handled by the well-established and robust **Pydantic**. 116 117### Security and authentication 118 119Security and authentication integrated. Without any compromise with databases or data models. 120 121All the security schemes defined in OpenAPI, including: 122 123* HTTP Basic. 124* **OAuth2** (also with **JWT tokens**). Check the tutorial on [OAuth2 with JWT](tutorial/security/oauth2-jwt.md){.internal-link target=_blank}. 125* API keys in: 126 * Headers. 127 * Query parameters. 128 * Cookies, etc. 129 130Plus all the security features from Starlette (including **session cookies**). 131 132All built as reusable tools and components that are easy to integrate with your systems, data stores, relational and NoSQL databases, etc. 133 134### Dependency Injection 135 136FastAPI includes an extremely easy to use, but extremely powerful <abbr title='also known as "components", "resources", "services", "providers"'><strong>Dependency Injection</strong></abbr> system. 137 138* Even dependencies can have dependencies, creating a hierarchy or **"graph" of dependencies**. 139* All **automatically handled** by the framework. 140* All the dependencies can require data from requests and **augment the path operation** constraints and automatic documentation. 141* **Automatic validation** even for *path operation* parameters defined in dependencies. 142* Support for complex user authentication systems, **database connections**, etc. 143* **No compromise** with databases, frontends, etc. But easy integration with all of them. 144 145### Unlimited "plug-ins" 146 147Or in other way, no need for them, import and use the code you need. 148 149Any integration is designed to be so simple to use (with dependencies) that you can create a "plug-in" for your application in 2 lines of code using the same structure and syntax used for your *path operations*. 150 151### Tested 152 153* 100% <abbr title="The amount of code that is automatically tested">test coverage</abbr>. 154* 100% <abbr title="Python type annotations, with this your editor and external tools can give you better support">type annotated</abbr> code base. 155* Used in production applications. 156 157## Starlette features 158 159**FastAPI** is fully compatible with (and based on) <a href="https://www.starlette.io/" class="external-link" target="_blank"><strong>Starlette</strong></a>. So, any additional Starlette code you have, will also work. 160 161`FastAPI` is actually a sub-class of `Starlette`. So, if you already know or use Starlette, most of the functionality will work the same way. 162 163With **FastAPI** you get all of **Starlette**'s features (as FastAPI is just Starlette on steroids): 164 165* Seriously impressive performance. It is <a href="https://github.com/encode/starlette#performance" class="external-link" target="_blank">one of the fastest Python frameworks available, on par with **NodeJS** and **Go**</a>. 166* **WebSocket** support. 167* In-process background tasks. 168* Startup and shutdown events. 169* Test client built on `requests`. 170* **CORS**, GZip, Static Files, Streaming responses. 171* **Session and Cookie** support. 172* 100% test coverage. 173* 100% type annotated codebase. 174 175## Pydantic features 176 177**FastAPI** is fully compatible with (and based on) <a href="https://pydantic-docs.helpmanual.io" class="external-link" target="_blank"><strong>Pydantic</strong></a>. So, any additional Pydantic code you have, will also work. 178 179Including external libraries also based on Pydantic, as <abbr title="Object-Relational Mapper">ORM</abbr>s, <abbr title="Object-Document Mapper">ODM</abbr>s for databases. 180 181This also means that in many cases you can pass the same object you get from a request **directly to the database**, as everything is validated automatically. 182 183The same applies the other way around, in many cases you can just pass the object you get from the database **directly to the client**. 184 185With **FastAPI** you get all of **Pydantic**'s features (as FastAPI is based on Pydantic for all the data handling): 186 187* **No brainfuck**: 188 * No new schema definition micro-language to learn. 189 * If you know Python types you know how to use Pydantic. 190* Plays nicely with your **<abbr title="Integrated Development Environment, similar to a code editor">IDE</abbr>/<abbr title="A program that checks for code errors">linter</abbr>/brain**: 191 * Because pydantic data structures are just instances of classes you define; auto-completion, linting, mypy and your intuition should all work properly with your validated data. 192* **Fast**: 193 * in <a href="https://pydantic-docs.helpmanual.io/#benchmarks-tag" class="external-link" target="_blank">benchmarks</a> Pydantic is faster than all other tested libraries. 194* Validate **complex structures**: 195 * Use of hierarchical Pydantic models, Python `typing`’s `List` and `Dict`, etc. 196 * And validators allow complex data schemas to be clearly and easily defined, checked and documented as JSON Schema. 197 * You can have deeply **nested JSON** objects and have them all validated and annotated. 198* **Extendible**: 199 * Pydantic allows custom data types to be defined or you can extend validation with methods on a model decorated with the validator decorator. 200* 100% test coverage. 201