Skip to content

API Routes

upadsamay387 edited this page Dec 12, 2024 · 2 revisions

Overview:

All API routes, except the chatbot, weather, and translation routes, are defined using tRPC in /src/server/api. While this page isn't comprehensive, it should give enough information to utilize these routes easily, especially with IDE-provided type hints. For the excluded routes, refer to their respective pages.

Key Features:

  • Uses tRPC v11
  • Has schema validation at runtime
  • Fully typed and ready to use
  • Compatible with Tanstack Query/React Query
  • Can be called using REST if needed via Next.js
  • Works on both the server and client
  • Manages state and caching without additional hooks

Usage:

Server component:

import { api } from "~/trpc/server";

...

api.routerName.routeName({params});

Replace routerName, routeName, and params with the appropriate values.


Client component:

import { api } from "~/trpc/react";

...

export default function Component() {
  // Query data
  const { data } = api.routerName.routeName.useQuery({params});
  
  // Mutate data
  // Create a function to run the mutation
  const { mutationFunction } = api.routerName.routeName.useMutation();
  
  function handleClick() {
    // Run the mutation when modification is needed
    mutationFunction({params});
  }
}

For more information, refer to the Tanstack Query documentation: https://tanstack.com/query/v5

Clone this wiki locally