Server Routes / API

Server Routes define explicit HTTP endpoints for public APIs, webhooks, health checks, and downloads.

Basic Example

import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/api/health')({
  server: {
    handlers: {
      GET: async () => Response.json({ ok: true }),
    },
  },
})

Server Route vs Server Function

ScenarioChoose
Internal component or loader callsServer Function
Third-party webhookServer Route
Public REST APIServer Route
Typed same-origin mutationServer Function
sitemap / health check / downloadServer Route

Errors

Return explicit status codes and avoid leaking internal exceptions.

return Response.json({ error: 'Unauthorized' }, { status: 401 })

Next Steps