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:
HeadContent- Renders the<head>content defined in thehead()functionOutlet- Renders child routesScripts- Includes the necessary client-side JavaScriptTanStackRouterDevtools- Development tools for debugging (only in development)
2. Create the Router Configuration
Createsrc/router.tsx to configure your router:
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
Createsrc/routes/index.tsx:
- Server Functions:
getCountandupdateCountare server functions that run exclusively on the server - File System Access: We’re using Node.js
fsmodule to read/write a file—this code never runs on the client - Input Validation:
inputValidatorensures type-safety for the data sent to the server - Loader: The
loaderruns on the server during SSR and fetches the initial count - Client Interaction: Clicking the button calls the server function and invalidates the router to refetch data
4. Start the Development Server
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
Createsrc/routes/about.tsx:
6. Add Navigation to Root Route
Updatesrc/routes/__root.tsx to add a navigation menu:
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
Createsrc/routes/api/hello.ts:
http://localhost:3000/api/hello.
8. Call the API from Your Component
Updatesrc/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
Createsrc/routes/posts/$postId.tsx:
src/routes/posts/index.tsx for a list of posts:
Building for Production
When you’re ready to deploy:10. Build Your App
.output directory:
.output/client- Client-side assets (HTML, CSS, JS).output/server- Server-side code
11. Run the Production Server
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 applicationstart-counter- The counter example from this guidestart-basic-auth- Authentication with sessionsstart-basic-react-query- Integration with TanStack Querystart-clerk-basic- Authentication with Clerkstart-supabase-basic- Full-stack app with Supabase