Builder15 min readBadge: Endpoint Builder

Server routes: your first API endpoint

Why secret work happens on a server, how a Next.js route handler works, and how to build one safely: check the input, keep the key in an environment variable, and return JSON with the right status.

Browser code and server code

Everything in your front end is sent to every visitor's browser, where anyone can read it. Code on a server runs on computers you control: it can hold secret keys, talk to the database with full rights and call paid APIs. That's why our guides keep saying 'do it on the server'.

A server route (also called an API route or endpoint) is a URL your own app answers, like /api/ask. The page sends a request to it, your server code runs, and a response comes back.

A route handler in Next.js

In the Next.js App Router, a file named route.ts inside the app folder becomes an endpoint, and you export one function per HTTP method. app/api/hello/route.ts with export async function GET() { return Response.json({ message: "Hello" }) } answers GET /api/hello.

A POST handler reads what the page sent with const body = await request.json(). Express servers, serverless functions and Supabase Edge Functions follow the same pattern: a request comes in, your code runs, a response goes out.

Build it safely

  • Route handlers are public: anyone can call them, not just your page. Check who's asking (sign-in) and whether they're allowed, on the server.
  • Never trust the input. Check it's the shape and size you expect, and reject anything else with status 400.
  • Read secrets from environment variables (process.env.MY_API_KEY) set in your host's settings, never from the code.
  • Return the right status: 200 OK, 400 bad input, 401 not signed in, 403 not allowed, 429 too many requests, 500 your bug.
  • Rate-limit anything that costs money, like an AI call.
  • Send back only what the page needs: no secrets, no other people's data. Log the details on the server and show users a friendly message.

Calling it from your page

The page uses fetch: const response = await fetch("/api/ask", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ question }) }). Check response.ok before reading await response.json(), and show a message if something went wrong.

Open the Network panel in your browser's developer tools to watch each request: the method, the status, what was sent and what came back.

Key terms

Route handler
Server code that answers requests to a URL in your app, like route.ts for /api/hello in Next.js.
Endpoint
One URL an API answers, like /users/octocat, each doing one job.
Server
A computer (usually in the cloud) that runs your app's back end and answers requests.
Back end
Code on a server that checks permissions and does work users mustn't tamper with.
Environment variable
A setting or secret the app reads at run time, kept outside the code.
HTTP status code
A number a server sends back: 200 OK, 401 not signed in, 403 not allowed, 404 not found, 500 server error.
HTTP method
The verb of a web request: GET reads, POST creates, PATCH or PUT updates, DELETE removes.
JSON
A simple text format for data that apps send to each other: names and values in curly braces.
Rate limit
The most requests or tokens a key may use in a period, such as per minute.
Authentication
Checking who someone is (signing in).
See the full glossary

Try it

Your first endpoint

  1. Ask your coding agent (or v0) to add app/api/hello/route.ts that returns { message: "Hello" }, then open /api/hello in your browser.
  2. Add a POST handler that takes { name } and returns a greeting. Reject a missing name, or one over 50 characters, with status 400.
  3. Call it from a page with fetch, check response.ok, and show the reply.
  4. Watch the request in the Network panel: method, status, payload and response.
  5. Ask your AI helper: 'What could a stranger do with this endpoint?' Fix what it finds.

Ask an AI tutor about this guide

Copy a prompt into ChatGPT, Claude or Gemini to go deeper. Check what it tells you against the guide.

Study with an AI tutor: copy this whole guide with tutoring instructions, then paste it into your AI assistant.
  • Explain it like I'm 12
  • Quiz me (5 questions, one at a time)
  • Show me a real example for my situation
  • What do beginners get wrong?

Knowledge check

Five questions. Get 4 right to earn the Endpoint Builder badge. You can retake it as often as you like.

Question 1 of 5

Why should a call to a paid AI API happen in a server route rather than in the browser?

Finished the How software works guides? Take the How software works track test for ten mixed questions.

My notes

Write what clicked, what confused you, and what you want to try. Saved in this browser only.

Learn it live

Digital Builders University runs small live workshops where you build something real with an instructor beside you, starting with Vibe Coding 101.

See workshops

Practise it

  • Game: Code Scramble (put the lines of a working program back in order)

Last reviewed November 25, 2026. AI tools change fast; check each tool’s own site for current features, plans and prices.