API

Overview

Get started with the API.

API deployment required

To enable communication between your Expo app and the server in a production environment, the web application with tRPC API must be deployed first.

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 };

Learn more about how to use the API in your browser extension code in the following sections:

Last updated on

On this page

No Headings
Ship your startup everywhere. In minutes.Get TurboStarter