Tip |
---|
If you are new to Search API and want to add an endpoint we recommend you start with How to Add and API Endpoint |
The API framework is entirely based on FastAPI, and it simplifies just where and how you can add new endpoints to Search API. Besides that everything is FastAPI, but using a router instead of the application directly since using routers give us the following advantages:
Modular code organization: Routers help organize code into separate modules, making it easier to manage and maintain larger API codebases.
Route prefixing: Routers allow you to group related routes together using a common prefix, providing a clean and intuitive structure for API endpoints.
Dependency injection: Routers support dependency injection, allowing you to declare and reuse dependencies at the router level.
Sub-applications: Routers enable the creation of sub-applications within the main FastAPI app, facilitating the development and maintenance of complex APIs.
Route inheritance and overrides: Routers support route inheritance, allowing you to define common behavior and middleware at the router level, with the ability to override specific routes when needed.
As you can see each router, in Search API is an endpoint, and within that endpoint you can do whatever you want.
Tip | ||
---|---|---|
| ||
If you want to know more about routers and the guidelines we used for Search API visit the FastAPI documentation about Bigger Applications - Multiple Files |
Info | ||
---|---|---|
| ||
If security is enabled, by default all endpoints are cover by it, if by any change |
Code Block | ||||
---|---|---|---|---|
| ||||
from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"message": "Hello, World!"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q} |
Code Block | ||||
---|---|---|---|---|
| ||||
from fastapi import APIRouter router = APIRouter() @router.get("/") def read_root(): return {"message": "Hello, World!"} @router.get("/items/{item_id}") def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q} |
Same in both examples but in the end, the router must be included in the app
Code Block | ||||
---|---|---|---|---|
| ||||
from fastapi import FastAPI from myrouter import router app = FastAPI() app.include_router(router) |
Info | ||
---|---|---|
| ||
Search API already does this for you, only the implementation of the routes in the router is required |
Create from template | ||||||||
---|---|---|---|---|---|---|---|---|
|
Info |
---|
Interactive documentation can be found in API Documentation |
Content Report Table | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|