APIRouter class¶
Here's the reference information for theAPIRouter class, with all its parameters, attributes and methods.
You can import theAPIRouter class directly fromfastapi:
fromfastapiimportAPIRouterfastapi.APIRouter¶
APIRouter(*,prefix="",tags=None,dependencies=None,default_response_class=Default(JSONResponse),responses=None,callbacks=None,routes=None,redirect_slashes=True,default=None,dependency_overrides_provider=None,route_class=APIRoute,on_startup=None,on_shutdown=None,lifespan=None,deprecated=None,include_in_schema=True,generate_unique_id_function=Default(generate_unique_id)) Bases:Router
APIRouter class, used to grouppath operations, for example to structurean app in multiple files. It would then be included in theFastAPI app, orin anotherAPIRouter (ultimately included in the app).
Read more about it in theFastAPI docs for Bigger Applications - Multiple Files.
Example¶
fromfastapiimportAPIRouter,FastAPIapp=FastAPI()router=APIRouter()@router.get("/users/",tags=["users"])asyncdefread_users():return[{"username":"Rick"},{"username":"Morty"}]app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
prefix | An optional path prefix for the router. TYPE: |
tags | A list of tags to be applied to all thepath operations in thisrouter. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Bigger Applications - Multiple Files. TYPE: |
default_response_class | The default response class to be used. Read more in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
responses | Additional responses to be shown in OpenAPI. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Additional Responses in OpenAPI. And in theFastAPI docs for Bigger Applications. TYPE: |
callbacks | OpenAPI callbacks that should apply to allpath operations in thisrouter. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
routes | Note: you probably shouldn't use this parameter, it is inheritedfrom Starlette and supported for compatibility. A list of routes to serve incoming HTTP and WebSocket requests. TYPE: |
redirect_slashes | Whether to detect and redirect slashes in URLs when the client doesn'tuse the same format. TYPE: |
default | Default function handler for this router. Used to handle404 Not Found errors. TYPE: |
dependency_overrides_provider | Only used internally by FastAPI to handle dependency overrides. You shouldn't need to use it. It normally points to the TYPE: |
route_class | Custom route (path operation) class to be used by this router. Read more about it in theFastAPI docs for Custom Request and APIRoute class. TYPE: |
on_startup | A list of startup event handler functions. You should instead use the Read more in theFastAPI docs for TYPE: |
on_shutdown | A list of shutdown event handler functions. You should instead use the Read more in theFastAPI docs for TYPE: |
lifespan | A Read more in theFastAPI docs for TYPE: |
deprecated | Mark allpath operations in this router as deprecated. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
include_in_schema | To include (or not) all thepath operations in this router in thegenerated OpenAPI. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994 | |
websocket¶
websocket(path,name=None,*,dependencies=None)Decorate a WebSocket function.
Read more about it in theFastAPI docs for WebSockets.
Example
Example¶
fromfastapiimportAPIRouter,FastAPI,WebSocketapp=FastAPI()router=APIRouter()@router.websocket("/ws")asyncdefwebsocket_endpoint(websocket:WebSocket):awaitwebsocket.accept()whileTrue:data=awaitwebsocket.receive_text()awaitwebsocket.send_text(f"Message text was:{data}")app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | WebSocket path. TYPE: |
name | A name for the WebSocket. Only used internally. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for WebSockets. TYPE: |
Source code infastapi/routing.py
117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242 | |
include_router¶
include_router(router,*,prefix="",tags=None,dependencies=None,default_response_class=Default(JSONResponse),responses=None,callbacks=None,deprecated=None,include_in_schema=True,generate_unique_id_function=Default(generate_unique_id))Include anotherAPIRouter in the same currentAPIRouter.
Read more about it in theFastAPI docs for Bigger Applications.
Example¶
fromfastapiimportAPIRouter,FastAPIapp=FastAPI()internal_router=APIRouter()users_router=APIRouter()@users_router.get("/users/")defread_users():return[{"name":"Rick"},{"name":"Morty"}]internal_router.include_router(users_router)app.include_router(internal_router)| PARAMETER | DESCRIPTION |
|---|---|
router | The TYPE: |
prefix | An optional path prefix for the router. TYPE: |
tags | A list of tags to be applied to all thepath operations in thisrouter. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Bigger Applications - Multiple Files. TYPE: |
default_response_class | The default response class to be used. Read more in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
responses | Additional responses to be shown in OpenAPI. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Additional Responses in OpenAPI. And in theFastAPI docs for Bigger Applications. TYPE: |
callbacks | OpenAPI callbacks that should apply to allpath operations in thisrouter. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
deprecated | Mark allpath operations in this router as deprecated. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
include_in_schema | Include (or not) all thepath operations in this router in thegenerated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
1253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499 | |
get¶
get(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP GET operation.
Example¶
fromfastapiimportAPIRouter,FastAPIapp=FastAPI()router=APIRouter()@router.get("/items/")defread_items():return[{"name":"Empanada"},{"name":"Arepa"}]app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
1501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876 | |
put¶
put(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP PUT operation.
Example¶
fromfastapiimportAPIRouter,FastAPIfrompydanticimportBaseModelclassItem(BaseModel):name:strdescription:str|None=Noneapp=FastAPI()router=APIRouter()@router.put("/items/{item_id}")defreplace_item(item_id:str,item:Item):return{"message":"Item replaced","id":item_id}app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258 | |
post¶
post(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP POST operation.
Example¶
fromfastapiimportAPIRouter,FastAPIfrompydanticimportBaseModelclassItem(BaseModel):name:strdescription:str|None=Noneapp=FastAPI()router=APIRouter()@router.post("/items/")defcreate_item(item:Item):return{"message":"Item created"}app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640 | |
delete¶
delete(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP DELETE operation.
Example¶
fromfastapiimportAPIRouter,FastAPIapp=FastAPI()router=APIRouter()@router.delete("/items/{item_id}")defdelete_item(item_id:str):return{"message":"Item deleted"}app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
2642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017 | |
options¶
options(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP OPTIONS operation.
Example¶
fromfastapiimportAPIRouter,FastAPIapp=FastAPI()router=APIRouter()@router.options("/items/")defget_item_options():return{"additions":["Aji","Guacamole"]}app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
3019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394 | |
head¶
head(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP HEAD operation.
Example¶
fromfastapiimportAPIRouter,FastAPIfrompydanticimportBaseModelclassItem(BaseModel):name:strdescription:str|None=Noneapp=FastAPI()router=APIRouter()@router.head("/items/",status_code=204)defget_items_headers(response:Response):response.headers["X-Cat-Dog"]="Alone in the world"app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776 | |
patch¶
patch(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP PATCH operation.
Example¶
fromfastapiimportAPIRouter,FastAPIfrompydanticimportBaseModelclassItem(BaseModel):name:strdescription:str|None=Noneapp=FastAPI()router=APIRouter()@router.patch("/items/")defupdate_item(item:Item):return{"message":"Item updated in place"}app.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158 | |
trace¶
trace(path,*,response_model=Default(None),status_code=None,tags=None,dependencies=None,summary=None,description=None,response_description="Successful Response",responses=None,deprecated=None,operation_id=None,response_model_include=None,response_model_exclude=None,response_model_by_alias=True,response_model_exclude_unset=False,response_model_exclude_defaults=False,response_model_exclude_none=False,include_in_schema=True,response_class=Default(JSONResponse),name=None,callbacks=None,openapi_extra=None,generate_unique_id_function=Default(generate_unique_id))Add apath operation using an HTTP TRACE operation.
Example¶
fromfastapiimportAPIRouter,FastAPIfrompydanticimportBaseModelclassItem(BaseModel):name:strdescription:str|None=Noneapp=FastAPI()router=APIRouter()@router.trace("/items/{item_id}")deftrace_item(item_id:str):returnNoneapp.include_router(router)| PARAMETER | DESCRIPTION |
|---|---|
path | The URL path to be used for thispath operation. For example, in TYPE: |
response_model | The type to use for the response. It could be any valid Pydanticfield type. So, it doesn't have tobe a Pydantic model, it could be other things, like a It will be used for:
Read more about it in theFastAPI docs for Response Model. TYPE: |
status_code | The default status code to be used for the response. You could override the status code by returning a response directly. Read more about it in theFastAPI docs for Response Status Code. TYPE: |
tags | A list of tags to be applied to thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
dependencies | A list of dependencies (using Read more about it in theFastAPI docs for Dependencies in path operation decorators. TYPE: |
summary | A summary for thepath operation. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
description | A description for thepath operation. If not provided, it will be extracted automatically from the docstringof thepath operation function. It can contain Markdown. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Path Operation Configuration. TYPE: |
response_description | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
responses | Additional responses that could be returned by thispath operation. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
deprecated | Mark thispath operation as deprecated. It will be added to the generated OpenAPI (e.g. visible at TYPE: |
operation_id | Custom operation ID to be used by thispath operation. By default, it is generated automatically. If you provide a custom operation ID, you need to make sure it isunique for the whole API. You can customize theoperation ID generation with the parameter Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
response_model_include | Configuration passed to Pydantic to include only certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude | Configuration passed to Pydantic to exclude certain fields in theresponse data. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_by_alias | Configuration passed to Pydantic to define if the response modelshould be serialized by alias when an alias is used. Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_unset | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that were not set andhave their default values. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_defaults | Configuration passed to Pydantic to define if the response datashould have all the fields, including the ones that have the same valueas the default. This is different from When Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
response_model_exclude_none | Configuration passed to Pydantic to define if the response data shouldexclude fields set to This is much simpler (less smart) than Read more about it in theFastAPI docs for Response Model - Return Type. TYPE: |
include_in_schema | Include thispath operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for Query Parameters and String Validations. TYPE: |
response_class | Response class to be used for thispath operation. This will not be used if you return a response directly. Read more about it in theFastAPI docs for Custom Response - HTML, Stream, File, others. TYPE: |
name | Name for thispath operation. Only used internally. TYPE: |
callbacks | List ofpath operations that will be used as OpenAPI callbacks. This is only for OpenAPI documentation, the callbacks won't be useddirectly. It will be added to the generated OpenAPI (e.g. visible at Read more about it in theFastAPI docs for OpenAPI Callbacks. TYPE: |
openapi_extra | Extra metadata to be included in the OpenAPI schema for thispathoperation. Read more about it in theFastAPI docs for Path Operation Advanced Configuration. TYPE: |
generate_unique_id_function | Customize the function used to generate unique IDs for thepathoperations shown in the generated OpenAPI. This is particularly useful when automatically generating clients orSDKs for your API. Read more about it in theFastAPI docs about how to Generate Clients. TYPE: |
Source code infastapi/routing.py
416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540 | |
on_event¶
on_event(event_type)Add an event handler for the router.
on_event is deprecated, uselifespan event handlers instead.
Read more about it in theFastAPI docs for Lifespan Events.
| PARAMETER | DESCRIPTION |
|---|---|
event_type | The type of event. TYPE: |
Source code infastapi/routing.py
459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626 | |







