You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Adding an API

1) Create the API package

  • Create a folder in app/api/v1
  • Inside the folder create the __init__.py and the endpoint file

    A descriptive name works the best



On the endpoint file



2) Add the imports

  • Special attention to APIRouter

from fastapi import APIRouter, Body
from fastapi.requests import Request

3) Declare the endpoint Router

  • This is essentially a small branch of the app, which will hold all our related routes
  • The prefix indicates the prefix of the endpoint

    All endpoint have the same base path, /es/api/v1, from here we attach the prefix, in this case being /Simple, so /es/api/v1/Simple

router = APIRouter(prefix='/Simple', tags=['Simple'])

4) Declare the routes

  • At this point, all the routes are pure FastAPI
  • The content of each route depends entirely on the one implementing the API

If you want more information on how to build routes, and what features are available please go to the FastAPI tutorial, whenever you see @app, just replace that with @router

@router.post('/post')
async def post_request(req: Request, body: dict = Body()):
    return body

@router.post('/put/{name}')
async def post_request(req: Request, name: str):
    return {"name": name}

@router.post('/get/{id}')
async def post_request(req: Request, id: int, param_a: int = 0):
    return f'GET Request for id {id}, with number {param_a}'

In this scenario the path for each route is:

/es/api/v1/Simple/post

/es/api/v1/Simple/put/lincon

/es/api/v1/Simple/get/42



on the __init__ file



5) Add the import for your router

  • Go to the  __init__.py file
  • Import the router from the API file

If you don’t import the router in the __init__.py, Search API won’t load it


from .simple import router

6) Verify Endpoint Status

  • Starting the server check the logs for the entry loading the endpoint
  • In case of error, the logs will show the issue
    • Either unable to load the endpoint or unable to initialize it
    • Otherwise you should see Pipeline <name> loaded
  • You can go to /es/docs or /es/redoc and check the
    API documentation

Swagger (/es/docs)

ReDoc (/es/redoc)

  • No labels