Browse documentation
Lesson 08 · External HTTP boundary

Put external endpoints in a Server Route

Separate application RPC from public HTTP endpoints and use a native Response to express status, body, and cache policy.

TanStack StartApplied18 minREV 02Markdown .md
After this lesson

Choose a Server Route when appropriate and return correct HTTP responses for success and failure.

01

Start with who will call it

Prefer a Server Function when only the current Start app calls the operation and end-to-end types matter. Use a Server Route for webhooks, third-party clients, mobile apps, or a stable HTTP contract. Both run on the server, but they serve different callers.

02

Return complete HTTP semantics

A Server Route handler returns a Response directly. Set status 404 explicitly when a resource is absent instead of throwing the page-router notFound() helper. Choose no-store, private, or shared caching for successful data according to whether identity affects it.

src/routes/api/issues/$issueId.tsts
import { createFileRoute } from '@tanstack/react-router'
import { findIssue } from '~/server/issues'

export const Route = createFileRoute('/api/issues/$issueId')({
  server: {
    handlers: {
      GET: async ({ params }) => {
        const issue = await findIssue(params.issueId)
        if (!issue) {
          return Response.json({ error: 'Issue not found' }, { status: 404 })
        }

        return Response.json(issue, {
          headers: { 'Cache-Control': 'no-store' },
        })
      },
    },
  },
})
03

Treat it as a real external boundary

Path params, search params, headers, and bodies all come from untrusted callers. Validate at runtime and enforce authentication, authorization, and rate limits before writes. Keep error bodies stable and never expose stacks, SQL, or internal file paths.

PRACTICE

Turn this lesson into a verifiable skill

Completion checkpoint

An external client can call the endpoint with standard HTTP and receive explicit status, headers, and body.

Exercise

Add a GET `/api/issues/$issueId` endpoint with distinct success, invalid-parameter, and missing-resource responses.

Verification

Use curl to verify all three responses, including Content-Type, status code, and JSON shape.

Common pitfalls
  • Implementing a public external API as a server-function RPC.
  • Always returning 200 and hiding errors inside JSON fields.
SOURCE REFERENCES · CHECKED 2026-08-03https://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