Skip to main content

Quickstart

This guide will walk you through building a simple counter app with TanStack Start. You’ll learn how to create routes, use server functions, and build a full-stack application with type-safe client-server communication.

Prerequisites

Make sure you’ve completed the Installation guide and have a TanStack Start project set up.

Creating Your First Route

Let’s start by creating the root route and a home page.

1. Create the Root Route

The root route (__root.tsx) is the layout for your entire application. It defines the HTML structure and includes essential components like HeadContent and Scripts. Create src/routes/__root.tsx:
Key components:
  • HeadContent - Renders the <head> content defined in the head() function
  • Outlet - Renders child routes
  • Scripts - Includes the necessary client-side JavaScript
  • TanStackRouterDevtools - Development tools for debugging (only in development)

2. Create the Router Configuration

Create src/router.tsx to configure your router:
The Register interface declaration enables full TypeScript support for your routes throughout your application.

Building a Counter with Server Functions

Now let’s build a counter that persists its value on the server. This demonstrates how to use server functions for type-safe client-server communication.

3. Create the Home Page

Create src/routes/index.tsx:
What’s happening here:
  1. Server Functions: getCount and updateCount are server functions that run exclusively on the server
  2. File System Access: We’re using Node.js fs module to read/write a file—this code never runs on the client
  3. Input Validation: inputValidator ensures type-safety for the data sent to the server
  4. Loader: The loader runs on the server during SSR and fetches the initial count
  5. Client Interaction: Clicking the button calls the server function and invalidates the router to refetch data

4. Start the Development Server

Visit http://localhost:3000 and you should see your counter app. Click the button and watch the count increase—the value persists even when you refresh the page because it’s stored on the server.

Adding Navigation and Multiple Pages

Let’s add another page to demonstrate routing.

5. Create an About Page

Create src/routes/about.tsx:

6. Add Navigation to Root Route

Update src/routes/__root.tsx to add a navigation menu:
The Link component provides type-safe navigation—TypeScript will error if you try to link to a route that doesn’t exist.

Creating API Routes

API routes let you create REST endpoints that can be called from anywhere, not just your React components.

7. Create an API Route

Create src/routes/api/hello.ts:
You can now access this API at http://localhost:3000/api/hello.

8. Call the API from Your Component

Update src/routes/about.tsx to fetch from the API:

Working with Dynamic Routes

Dynamic routes let you create pages with variable URL segments.

9. Create a Dynamic Route

Create src/routes/posts/$postId.tsx:
Create src/routes/posts/index.tsx for a list of posts:
Update the root navigation to include Posts:

Building for Production

When you’re ready to deploy:

10. Build Your App

This creates optimized bundles in the .output directory:
  • .output/client - Client-side assets (HTML, CSS, JS)
  • .output/server - Server-side code

11. Run the Production Server

Your app is now running in production mode.

Next Steps

You’ve built a full-stack application with TanStack Start. Here’s what you’ve learned:
  • Creating routes with file-based routing
  • Using server functions for type-safe client-server communication
  • Building API routes for REST endpoints
  • Working with dynamic routes and navigation
  • Loading data with loaders and SSR

Learn More

Explore these topics to build more advanced applications:
  • Data Loading - Learn about loaders, prefetching, and caching
  • Authentication - Implement user authentication and protected routes
  • Middleware - Add logging, authentication, and custom logic to routes
  • Streaming - Stream data to improve perceived performance
  • Deployment - Deploy to Vercel, Netlify, Cloudflare, and more

Example Projects

Check out the examples directory in the TanStack Router repository for more complete examples:
  • start-basic - A basic Start application
  • start-counter - The counter example from this guide
  • start-basic-auth - Authentication with sessions
  • start-basic-react-query - Integration with TanStack Query
  • start-clerk-basic - Authentication with Clerk
  • start-supabase-basic - Full-stack app with Supabase
Happy building with TanStack Start!