Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NNext.js

    Next.js foundations

    • What is Next.js?
    • The App Router: folders are routes
    • Dynamic routes
    • Client-side navigation

    Server, client and data

    • Server and Client Components
    • Fetching data on the server
    • loading, error and not-found
    • Caching and revalidation

    Mutations, APIs and security

    • Server Actions
    • Route Handlers: building an API
    • Proxy (formerly Middleware)
    • Authentication and protecting pages

    Going to production

    • Metadata and SEO
    • Images, fonts and performance
    • Environment variables and deploying to Vercel
    • Project: a course catalogue
    Next.js

    Mutations, APIs and security · Lesson 10 of 16

    Route Handlers: building an API

    route.ts, GET and POST, Request and Response, and status codes.

    10 minutes2 games

    A route.ts file in any app/ folder creates an API endpoint at that URL. You export functions named after HTTP methods (GET, POST, PATCH, DELETE); each takes a Request and returns a Response, the same Web APIs as in the browser.

    TypeScript
    // app/api/courses/route.ts  ->  GET /api/courses?track=web,  POST /api/courses
    import { NextResponse, type NextRequest } from "next/server"
    
    export async function GET(request: NextRequest) {
      const track = request.nextUrl.searchParams.get("track")
      const courses = await getCourses({ track })
      return NextResponse.json(courses)
    }
    
    export async function POST(request: Request) {
      const user = await getUser()
      if (!user) return NextResponse.json({ error: "Sign in first" }, { status: 401 })
      const body = await request.json().catch(() => null)
      if (!body?.title) return NextResponse.json({ error: "title is required" }, { status: 400 })
      const course = await createCourse(body.title, user.id)
      return NextResponse.json(course, { status: 201 })
    }
    StatusMeaning

    200 OK

    fine

    201 Created

    something was created

    400 Bad Request

    the data sent is invalid

    401 Unauthorized

    not signed in

    403 Forbidden

    signed in but not allowed

    404 Not Found

    doesn't exist

    429 Too Many Requests

    too many requests (rate limit)

    500 Internal Server Error

    our fault

    Important

    Return the right status code, not 200 with { error }: clients, monitoring and caches rely on it. Don't return internal error messages to users (they may reveal table names or paths); log them and return a generic message. For webhooks, verify the signature before trusting anything in the request.

    Quick question

    A signed-in user tries to delete a course that isn't theirs. What do you return?

    Interview question

    How do you secure an API endpoint?

    Exercise

    Write GET(request) that reads ?name= from the URL and returns JSON { "greeting": "Hello, NAME" } with status 200, or { "error": "name is required" } with status 400 when name is missing. The code below tries it twice.

    Expected output:
    200 {"greeting":"Hello, Nour"}
    400 {"error":"name is required"}
    JavaScript
    async function GET(request) {
      return new Response("todo")
    }
    
    for (const url of ["https://seacode.tech/api/hello?name=Nour", "https://seacode.tech/api/hello"]) {
      const res = await GET(new Request(url))
      console.log(res.status, await res.text())
    }

    Learn more from

    • Next.js docs: Route Handlersnextjs.org
    • MDN: HTTP response status codesdeveloper.mozilla.org
    • MDN: Responsedeveloper.mozilla.org
    • OWASP: API Security Top 10owasp.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousServer ActionsNextProxy (formerly Middleware)
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.