API

Overview

Get started with the API.

TurboStarter is designed to be a scalable and production-ready fullstack starter kit. One of the core features is a dedicated and extendable API layer. To enable this in a type-safe manner, we chose tRPC as the API server and client library.

Why tRPC?

tRPC is a modern RPC framework for TypeScript and gives you a way to define your API in a type-safe way. It also has a lot of features like caching, batching, authorization and more. It also has a wide range of extensions like tRPC OpenAPI which you can use to generate an OpenAPI endpoint for your API.

All API endpoints and their resolvers are defined in the packages/api/ library. Here you will find a modules folder which contains the different feature modules of the API. Each module has its own folder and is exporting all the different resolvers in an index.ts file.

For each module we then create a separate tRPC router in the packages/api/router.ts file and aggregate all sub-routers into one main router.

The API is then exposed as a route handler that will be provided in a Next.js API route:

apps/web/src/app/api/trpc/[trpc]/route.ts
const handler = async (req: NextRequest) => {
  const response = await fetchRequestHandler({
    endpoint: "/api/trpc",
    req,
    router: appRouter,
    createContext: () =>
      createTRPCContext({
        headers: req.headers,
        auth: auth(),
      }),
    onError:
      env.NODE_ENV === NodeEnv.DEVELOPMENT
        ? ({ path, error }) => {
            console.error(
              `❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
            );
          }
        : ({ error }) => console.error(error),
  });
 
  setCorsHeaders(response);
  return response;
};
 
export { handler as GET, handler as POST };

API availability

The API is a part of web, serverless Next.js app. It means that you must deploy it to use the API in other apps (e.g. mobile app, browser extension), even if you don't need web app itself. It's very simple, as you're just deploying the Next.js app and the API is just a part of it.

Learn more about API in the following sections:

Last updated on

On this page

No Headings
Ship your startup everywhere. In minutes.