Browse documentation
Agent docs/start/server-route
recipe

Create an externally callable Server Route

Build a native HTTP endpoint for third-party callers with explicit status, caching, and safe responses.

View raw Markdown
CONTRACT

Execution contract

Use when
Use for webhooks, external clients, or integrations requiring a standard HTTP endpoint.
Avoid when
Prefer a Server Function for app-internal calls that need end-to-end types.
Preconditions
Define method, path, input, status codes, response schema, authentication, and idempotency requirements.
Verification
Use an independent HTTP client to test success, unauthorized, invalid-input, and not-found responses.
Failure mode
HTML or 200 error payloads indicate missing explicit Response status and Content-Type handling.
Security
External endpoints must handle identity, authorization, input size, and replay risk without relying on browser same-origin behavior.
01

Selection contract

Use a Server Function for in-app, same-origin RPC with end-to-end types. Use a Server Route for webhooks, mobile clients, third-party integrations, or an independent HTTP contract.

02

Minimal implementation

Return a Response from file-route server.handlers. Every failure branch chooses an explicit status; page-level notFound() control flow is not a Server Route 404 response.

src/routes/api/health[.]json.tsts
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/api/health.json')({
  server: {
    handlers: {
      GET: async () => Response.json(
        { ok: true },
        { headers: { 'Cache-Control': 'no-store' } },
      ),
    },
  },
})
03

Security and validation

Treat params, search, headers, and body as unknown. Validate and enforce authentication, authorization, and rate limits before writes. Return stable error objects without internal exceptions. Identity-dependent responses use no-store or private, never public shared caching.

PRIMARY SOURCEShttps://tanstack.com/start/latest/docs/framework/react/guide/server-routeshttps://tanstack.com/start/latest/docs/framework/react/guide/server-functions
TanStack Atlas

Original bilingual knowledge · verified against primary sources

Friend linksGitHub