Naming Functions Related to Routes

YASE - Yet Another Software Engineer

Naming Functions Related to Routes

The most common practice, often seen in FastAPI’s official documentation, is to use a verb_resource or action_resource pattern, where:

  1. verb / action: Describes the operation performed by the route, often aligned with CRUD verbs (Create, Read, Update, Delete) or specific actions.
    • read_: for GET operations (reading one or more items).
    • create_: for POST operations (creating a new item).
    • update_: for PUT or PATCH operations (updating an existing item).
    • delete_ (or remove_): for DELETE operations (deleting an item).
    • Other descriptive verbs for specific actions (e.g., search_, upload_, login_, process_).
  2. resource: describes the entity or resource the route operates on (generally singular for operations on a single item, plural for operations on collections).
    • item, items
    • user, users
    • order, orders
    • product, products

Practical Examples:

Additional rules and good practices:

In conclusion: the “rule” to follow is the verb_action_resource convention in snake_case, applied with consistency, because it maximizes the readability and maintainability of your FastAPI code.