id
stringlengths
16
145
text
stringlengths
1
179k
title
stringclasses
1 value
fastapi_security_advanced_user_guide/fastapipythonwebapis_216_0.txt
A **request body** is data sent by the client to your API. A **response body** is the data your API sends to the client. Your API almost always has to send a response body. But clients don’t necessarily need to send request bodies all the time.
fastapi_security_advanced_user_guide/configuration_46_0.txt
##### Network
fastapi_security_advanced_user_guide/fastapipythonwebapis_160_0.txt
@app.get("/items/{item_id}") async def read_item(item_id): return {"item_id": item_id}
fastapi_security_advanced_user_guide/fastapipythonwebapis_264_0.txt
In your editor, inside your function, you will get type hints and completion everywhere. This wouldn’t happen if you received a ` dict ` instead of a pydantic model:
fastapi_security_advanced_user_guide/fastapipythonwebapis_169_0.txt
You can declare the type of a path parameter in the function using standard Python type hints:
fastapi_security_advanced_user_guide/anintroductiontopyth_35_0.txt
After successfully running the code, the app will run on port 8000. Open the URL [ **http://127.0.0.1:8000** ](http://127.0.0.1:8000/) in your web browser. You should see a screen like the image below.
fastapi_security_advanced_user_guide/fastapipythonwebapis_26_0.txt
[ Share ](https://twitter.com/intent/tweet/?text=Check out this %23Python tutorial: Using%20FastAPI%20to%20Build%20Python%20Web%20APIs by @realpython&url=https%3A//realpython.com/fastapi-python-web-apis/) [ Share ](https://facebook.com/sharer/sharer.php?u=https%3A//realpython.com/fastapi- python-web-apis/) [ Email ](ma...
fastapi_security_advanced_user_guide/fastapipythonwebapis_187_0.txt
{ "detail": [ { "loc": [ "path", "item_id" ], "msg": "value is not a valid integer", "type": "type_error.integer" } ] }
fastapi_security_advanced_user_guide/anintroductiontopyth_64_0.txt
](/?source=post_page-----fb68d8861fad--------------------------------)
fastapi_security_advanced_user_guide/fastapipythonwebapis_263_0.txt
### Editor Support, Autocompletion, and Type Checks
fastapi_security_advanced_user_guide/fastapipythonwebapis_3_0.txt
* More
fastapi_security_advanced_user_guide/fastapi_security_11_23.txt
, "disabled": True, }, } app = FastAPI() def fake_hash_password(password: str): return "fakehashed" + password oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") class User(BaseModel): username: str email: Union[str, ...
fastapi_security_advanced_user_guide/fastapi_security_11_7.txt
sabled": False, }, "alice": { "username": "alice", "full_name": "Alice Wonderson", "email": "alice@example.com", "hashed_password": "fakehashedsecret2", "disabled": True, }, } app = FastAPI() def fake_hash_pas...
fastapi_security_advanced_user_guide/fastapipythonwebapis_343_0.txt
[ Level Up Your Python Skills » ](/account/join/?utm_source=rp_article_footer&utm_content=fastapi-python-web- apis)
fastapi_security_advanced_user_guide/fastapipythonwebapis_205_0.txt
Because path operations are evaluated in order, you need to make sure that the path for ` /users/me ` is declared before the one for ` /users/{user_id} ` :
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_2_2.txt
tartup - shutdown ](../testing-events/) * [ Testing Dependencies with Overrides ](../testing-dependencies/) * [ Testing a Database ](../testing-database/) * [ Async Tests ](../async-tests/) * [ Settings and Environment Variables ](../settings/) * [ OpenAPI Callbacks ](../openapi-call...
fastapi_security_advanced_user_guide/fastapi_security_9_3.txt
://github.com/tiangolo/fastapi "github.com") [ ](https://discord.gg/VQjSZaeJmf "discord.gg") [ ](https://twitter.com/fastapi "twitter.com") [ ](https://www.linkedin.com/in/tiangolo "www.linkedin.com") [ ](https://dev.to/tiangolo "dev.to") [ ](https://medium.com/@tiangolo "medium.com") [ ](https://tiangolo.com "tiangolo...
fastapi_security_advanced_user_guide/fastapipythonwebapis_108_0.txt
Copied!
fastapi_security_advanced_user_guide/fastapipythonwebapis_37_0.txt
**Free Bonus:** [ 5 Thoughts On Python Mastery ](https://realpython.com/bonus/python-mastery-course/) , a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
fastapi_security_advanced_user_guide/configuration_7_0.txt
Accelerate API development with quality and consistency across OpenAPI and AsyncAPI.
fastapi_security_advanced_user_guide/fastapipythonwebapis_282_0.txt
Copied!
fastapi_security_advanced_user_guide/fastapipythonwebapis_287_0.txt
Python
fastapi_security_advanced_user_guide/fastapipythonwebapis_366_0.txt
![](https://www.facebook.com/tr?id=2220911568135371&ev=PageView&noscript=1)
fastapi_security_advanced_user_guide/anintroductiontopyth_27_0.txt
* The second one is the **ASGI server** for deploying the application in production.
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_8_6.txt
"/items/{item_id}", response_model=Item, responses={**responses, 200: {"content": {"image/png": {}}}}, ) async def read_item(item_id: str, img: Union[bool, None] = None): if img: return FileResponse("image.png", media_type="image/png") else: return...
fastapi_security_advanced_user_guide/fastapipythonwebapis_268_0.txt
[ ![Editors provide error checks for pydantic models](https://files.realpython.com/media/fastapi-body- image04.aa1cc274a9a2.png) ](https://files.realpython.com/media/fastapi-body- image04.aa1cc274a9a2.png)
fastapi_security_advanced_user_guide/fastapipythonwebapis_276_0.txt
# main.py
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_8_2.txt
[ Templates ](../templates/) * [ WebSockets ](../websockets/) * [ Lifespan Events ](../events/) * [ Testing WebSockets ](../testing-websockets/) * [ Testing Events: startup - shutdown ](../testing-events/) * [ Testing Dependencies with Overrides ](../testing-dependencies/) * [ ...
fastapi_security_advanced_user_guide/fastapi_security_12_4.txt
None] = None def fake_decode_token(token): return User( username=token + "fakedecoded", email="john@example.com", full_name="John Doe" ) async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): user = fake_decode_token(token) ret...
fastapi_security_advanced_user_guide/configuration_31_0.txt
For readability, parameters are grouped by category and sorted alphabetically.
fastapi_security_advanced_user_guide/fastapipythonwebapis_202_0.txt
You can use the same type declarations with ` str ` , ` float ` , ` bool ` and many other complex data types.
fastapi_security_advanced_user_guide/fastapipythonwebapis_333_0.txt
[ ![Jacob Schmitt](/cdn- cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/profile- small_js.2f4d0d8da1ca.jpg) ](/team/jschmitt/)
fastapi_security_advanced_user_guide/fastapipythonwebapis_168_0.txt
### Path Parameters With Types
fastapi_security_advanced_user_guide/anintroductiontopyth_79_0.txt
Privacy
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_6_5.txt
.png) ## Available responses ¶ Here are some of the available responses. Keep in mind that you can use ` Response ` to return anything else, or even create a custom sub-class. Technical Details You could also use ` from starlette.responses import HTMLResponse ` . **FastAPI** provides the same ` starlette.respon...
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_8_0.txt
Skip to content [ Follow **@fastapi** on **Twitter** to stay updated ](https://twitter.com/fastapi) [ Subscribe to the **FastAPI and friends** newsletter 🎉 ](https://fastapi.tiangolo.com/newsletter/) [ You can now sponsor **FastAPI** 🍰 ](https://github.com/sponsors/tiangolo) [ sponsor ![](/img/sponsors/cryptapi-...
fastapi_security_advanced_user_guide/fastapipythonwebapis_138_0.txt
# main.py
fastapi_security_advanced_user_guide/fastapipythonwebapis_347_0.txt
[ LinkedIn ](https://www.linkedin.com/sharing/share- offsite/?url=https%3A%2F%2Frealpython.com%2Ffastapi-python-web-apis%2F) [ Twitter ](https://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20Using%20FastAPI%20to%20Build%20Python%20Web%20APIs&url=https%3A%2F%2Frealpython.com%2Ff...
fastapi_security_advanced_user_guide/anintroductiontopyth_80_0.txt
](https://policy.medium.com/medium-privacy- policy-f03bf92035c9?source=post_page----- fb68d8861fad--------------------------------)
fastapi_security_advanced_user_guide/anintroductiontopyth_42_0.txt
### Very easy to learn the framework and develop the code Easy to convert the code from Flask to FastAPI The framework is…
fastapi_security_advanced_user_guide/configuration_84_0.txt
Swagger Open Source
fastapi_security_advanced_user_guide/fastapipythonwebapis_241_0.txt
A JSON object that omits the default values is also valid.
fastapi_security_advanced_user_guide/fastapipythonwebapis_149_0.txt
@app.get("/") async def root(): return {"message": "Hello World"}
fastapi_security_advanced_user_guide/thebenefits_26_0.txt
You can also find a wealth of resources online, including tutorials, blogs, and examples of how to use these tools with FastAPI and other Python web frameworks.
fastapi_security_advanced_user_guide/configuration_23_0.txt
* __ * [ Sign In ](https://app.swaggerhub.com/login?channel=direct "Sign In") * [ Try Free ](/tools/swaggerhub/why-swaggerhub)
fastapi_security_advanced_user_guide/fastapipythonwebapis_246_0.txt
from fastapi import FastAPI from pydantic import BaseModel
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_6_4.txt
ad_items(): return ORJSONResponse([{"item_id": "Foo"}]) Info The parameter ` response_class ` will also be used to define the "media type" of the response. In this case, the HTTP header ` Content-Type ` will be set to ` application/json ` . And it will be documented as such in OpenAPI. Tip The ` ORJS...
fastapi_security_advanced_user_guide/fastapipythonwebapis_357_0.txt
[ Continue » ](/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Ffastapi- python-web-apis%2F)
fastapi_security_advanced_user_guide/fastapipythonwebapis_129_0.txt
Python
fastapi_security_advanced_user_guide/fastapipythonwebapis_314_0.txt
Send Me Python Tricks »
fastapi_security_advanced_user_guide/configuration_95_0.txt
* [ SoapUI ](https://www.soapui.org/?utm_medium=referral&utm_source=swagger.io&utm_campaign=footer) * [ Swagger ](/tools/open-source/) * [ SwaggerHub ](/tools/swaggerhub/) * [ SwaggerHub Explore ](/tools/swaggerhub-explore/) * [ TestComplete ](https://smartbear.com/product/testcomplete/overview/?utm_medium=referral&utm...
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_7_1.txt
* [ Body - Fields ](../../tutorial/body-fields/) * [ Body - Nested Models ](../../tutorial/body-nested-models/) * [ Declare Request Example Data ](../../tutorial/schema-extra-example/) * [ Extra Data Types ](../../tutorial/extra-data-types/) * [ Cookie Parameters ](../../tutorial/cookie-par...
fastapi_security_advanced_user_guide/configuration_9_0.txt
* [ __ Developer Portal Improve API discoverability and developer experience in a fully customizable portal. ](https://swagger.io/tools/swaggerhub/features/swaggerhub-portal/) * [ __ Exploration Evaluate the functionality of any API instantly, regardless of protocol. ](https://swagger.io/tools/swaggerhub-explore/) ...
fastapi_security_advanced_user_guide/anintroductiontopyth_41_0.txt
## GitHub - AyselAydin/fastAPI: This repo contains FastAPI example that a Web framework for developing…
fastapi_security_advanced_user_guide/fastapipythonwebapis_122_0.txt
You can also use the other operations mentioned above:
fastapi_security_advanced_user_guide/configuration_89_0.txt
* [ SwaggerHub ](/tools/swaggerhub/) * [ SwaggerHub Explore ](/tools/swaggerhub-explore/) * [ SwaggerHub Enterprise ](/tools/swaggerhub/enterprise/) * [ SwaggerHub vs OSS ](/open-source-comparison/) * [ SwaggerHub Integrations ](/tools/swaggerhub/integrations/)
fastapi_security_advanced_user_guide/fastapi_security_12_8.txt
e_token(token): return User( username=token + "fakedecoded", email="john@example.com", full_name="John Doe" ) async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): user = fake_decode_token(token) return user @app.get("/users/me...
fastapi_security_advanced_user_guide/fastapi_advanced_user_guide_4_2.txt
kets/) * [ Testing Events: startup - shutdown ](../testing-events/) * [ Testing Dependencies with Overrides ](../testing-dependencies/) * [ Testing a Database ](../testing-database/) * [ Async Tests ](../async-tests/) * [ Settings and Environment Variables ](../settings/) * [ Op...
fastapi_security_advanced_user_guide/thebenefits_31_0.txt
Jose Diaz Jose Diaz Head of IT @ IMCW. +15 years coding and leading teams.
fastapi_security_advanced_user_guide/fastapi_security_11_21.txt
urrent_user(token: Annotated[str, Depends(oauth2_scheme)]): user = fake_decode_token(token) if not user: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication credentials", headers={"WWW-Authenticate": "B...
fastapi_security_advanced_user_guide/configuration_18_0.txt
](/tools/swagger-ui/)
fastapi_security_advanced_user_guide/fastapipythonwebapis_83_0.txt
You’ll see the alternative automatic documentation provided by [ ReDoc ](https://github.com/Redocly/redoc) :
fastapi_security_advanced_user_guide/fastapipythonwebapis_219_0.txt
To declare a request body, you use pydantic models, with all their power and benefits. You’ll learn more about them below.
fastapi_security_advanced_user_guide/fastapi_security_5_4.txt
## Items You can **read items**. ## Users You will be able to: * **Create users** (_not implemented_). * **Read users** (_not implemented_). """ app = FastAPI( title="ChimichangApp", description=description, summary="Deadpool's favori...
fastapi_security_advanced_user_guide/anintroductiontopyth_53_0.txt
[ API ](https://medium.com/tag/api?source=post_page-----fb68d8861fad ---------------api-----------------)
fastapi_security_advanced_user_guide/fastapi_security_0_2.txt
ariables ](/advanced/settings/) * [ OpenAPI Callbacks ](/advanced/openapi-callbacks/) * [ OpenAPI Webhooks ](/advanced/openapi-webhooks/) * [ Including WSGI - Flask, Django, others ](/advanced/wsgi/) * [ Generate Clients ](/advanced/generate-clients/) * [ Deployment ](/deployment/) De...
fastapi_security_advanced_user_guide/fastapipythonwebapis_177_0.txt
Declaring the type of a path parameter will give you editor support inside of your function, with error checks, completion, and so on.
fastapi_security_advanced_user_guide/fastapipythonwebapis_310_0.txt
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: [ **Python REST APIs With FastAPI** ](/courses/python-rest- apis-with-fastapi/)
fastapi_security_advanced_user_guide/fastapipythonwebapis_184_0.txt
### Data Validation
fastapi_security_advanced_user_guide/fastapi_security_5_0.txt
Skip to content [ Follow **@fastapi** on **Twitter** to stay updated ](https://twitter.com/fastapi) [ Subscribe to the **FastAPI and friends** newsletter 🎉 ](https://fastapi.tiangolo.com/newsletter/) [ You can now sponsor **FastAPI** 🍰 ](https://github.com/sponsors/tiangolo) [ sponsor ![](/img/sponsors/cryptapi-...
fastapi_security_advanced_user_guide/anintroductiontopyth_56_0.txt
\--
fastapi_security_advanced_user_guide/fastapipythonwebapis_312_0.txt
Get a short & sweet **Python Trick** delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.
fastapi_security_advanced_user_guide/anintroductiontopyth_82_0.txt
](https://policy.medium.com/medium-terms-of- service-9db0094a1e0f?source=post_page----- fb68d8861fad--------------------------------)
fastapi_security_advanced_user_guide/configuration_102_0.txt
* * *
fastapi_security_advanced_user_guide/anintroductiontopyth_14_0.txt
Jan 7, 2023
fastapi_security_advanced_user_guide/fastapipythonwebapis_178_0.txt
### Data Conversion
fastapi_security_advanced_user_guide/fastapipythonwebapis_189_0.txt
This is because the path parameter ` item_id ` has a value of ` "foo" ` , which is not an ` int ` .
fastapi_security_advanced_user_guide/fastapipythonwebapis_135_0.txt
This function will be called by FastAPI whenever it receives a request to the specified URL ( ` / ` ) using a ` GET ` operation. In this case, it is an [ ` async ` function ](https://realpython.com/async-io-python/) .
fastapi_security_advanced_user_guide/fastapipythonwebapis_203_0.txt
### Order Matters: Put Fixed Paths First
fastapi_security_advanced_user_guide/fastapipythonwebapis_40_0.txt
* **Fast to run** : It offers very high performance, on par with **NodeJS** and **Go** , thanks to [ Starlette ](https://www.starlette.io/) and [ pydantic ](https://pydantic-docs.helpmanual.io/) . * **Fast to code** : It allows for significant increases in development speed. * **Reduced number of bugs** : It reduces th...
fastapi_security_advanced_user_guide/fastapipythonwebapis_365_0.txt
© 2012–2024 Real Python ⋅ [ Newsletter ](/newsletter/) ⋅ [ Podcast ](/podcasts/rpp/) ⋅ [ YouTube ](https://www.youtube.com/realpython) ⋅ [ Twitter ](https://twitter.com/realpython) ⋅ [ Facebook ](https://facebook.com/LearnRealPython) ⋅ [ Instagram ](https://www.instagram.com/realpython/) ⋅ [ Python Tutorials ](/) ⋅ [ S...
fastapi_security_advanced_user_guide/fastapipythonwebapis_300_0.txt
But there’s a lot more that you could learn:
fastapi_security_advanced_user_guide/configuration_48_0.txt
##### Macros
fastapi_security_advanced_user_guide/fastapi_security_6_6.txt
redefined ` responses ` . * All these _path operations_ will have the list of ` dependencies ` evaluated/executed before them. * If you also declare dependencies in a specific _path operation_ , **they will be executed too** . * The router dependencies are executed first, then the [ ` dependencies ` in the...
fastapi_security_advanced_user_guide/fastapipythonwebapis_291_0.txt
class Item(BaseModel): name: str description: Optional[str] = None price: float tax: Optional[float] = None
fastapi_security_advanced_user_guide/anintroductiontopyth_43_0.txt
github.com
fastapi_security_advanced_user_guide/fastapipythonwebapis_5_0.txt
* [ Search ](/search "Search")
fastapi_security_advanced_user_guide/anintroductiontopyth_58_0.txt
\--
fastapi_security_advanced_user_guide/fastapi_security_11_22.txt
if not hashed_password == user.hashed_password: raise HTTPException(status_code=400, detail="Incorrect username or password") return {"access_token": user.username, "token_type": "bearer"} @app.get("/users/me") async def read_users_me( current_user: Annotated[User, D...
fastapi_security_advanced_user_guide/fastapipythonwebapis_191_0.txt
Also notice that the error clearly states the exact point where the validation didn’t pass. This is incredibly helpful while developing and debugging code that interacts with your API.
fastapi_security_advanced_user_guide/fastapipythonwebapis_86_0.txt
### The First API, Step by Step
fastapi_security_advanced_user_guide/fastapi_security_5_1.txt
els/) * [ Declare Request Example Data ](../schema-extra-example/) * [ Extra Data Types ](../extra-data-types/) * [ Cookie Parameters ](../cookie-params/) * [ Header Parameters ](../header-params/) * [ Response Model - Return Type ](../response-model/) * [ Extra Models ](../ext...
fastapi_security_advanced_user_guide/configuration_91_0.txt
* [ OpenAPI Specification ](/specification/) * [ Resources ](/resources/) * [ Open Source Docs ](/docs/swagger-tools/) * [ SwaggerHub Explore Docs ](https://support.smartbear.com/swaggerhub-explore/) * [ SwaggerHub Docs ](https://support.smartbear.com/swaggerhub/docs/)
fastapi_security_advanced_user_guide/anintroductiontopyth_72_0.txt
](https://medium.statuspage.io/?source=post_page----- fb68d8861fad--------------------------------)
fastapi_security_advanced_user_guide/fastapipythonwebapis_137_0.txt
Python
fastapi_security_advanced_user_guide/configuration_104_0.txt
* +1 617-684-2600 USA |
fastapi_security_advanced_user_guide/fastapipythonwebapis_186_0.txt
JSON
fastapi_security_advanced_user_guide/thebenefits_6_0.txt
Swagger Editor for designing API specifications
fastapi_security_advanced_user_guide/thebenefits_33_0.txt
Experience leading software development teams and building applications from scratch to deploy them to production environments.
fastapi_security_advanced_user_guide/fastapipythonwebapis_307_0.txt
You’re now ready to start creating your own highly performant APIs for your projects. If you want to dive deeper into the world of FastAPI, then you can follow the official [ User Guide ](https://fastapi.tiangolo.com/tutorial/) in the FastAPI documentation.