Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Next.js Tutorial
  • Next.js Components
  • Next.js Functions
  • Next.js Deployment
  • Next.js Projects
  • Next.js Routing
  • Next.js Styles
  • Next.js Server-Side Rendering
  • Next.js Environment Variables
  • Next.js Middleware
  • Next.js Typescript
  • Next.js Image Optimization
  • Next.js Data Fetching
Open In App
Next Article:
Building a Task Reminder App with Next.js
Next article icon

How to Build a REST API with Next.js 13?

Last Updated : 09 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Next.js is the most widely used React framework. Next.js 13.2 introduced a new file-based routing mechanism, called App Router, for building React frontend and serverless backend. In this article, we will be building a simple REST API using Next.js Route Handlers

Table of Content

  • Next.js Route Handlers
  • Next.js project initialization
  • Building REST API with Next.js Route Handlers
  • Conclusion

Prerequisites

  1. Next.js 13 App Router
  2. Node.js

Next.js Route Handlers

Next.js Route Handlers are used to create custom request handlers for a given route using the Web Request and Response APIs. It is replacement for API routes Pages router

Convention

Route Handlers can be defined in only route.(js | ts) files inside "app" directory which means Route Handlers are only available in App router.

You cannot define page.(js | ts) & route.(js | ts) in same route as both take over whole HTTP verbs. For example "app/login/page.ts" and "app/login/route.ts" will cause conflict.

Supported HTTP methods

Route Handlers support 7 HTTP methods which are:

  1. GET
  2. POST
  3. PUT
  4. PATCH
  5. DELETE
  6. HEAD
  7. OPTIONS

If any method, other than these is called, Next.js will return "405 Method Not Allowed".

Caching

Only GET method is cached by default. To opt out of caching in GET requests, you can use one of the following ways:

  • Using the Request(NextRequest) object.
  • Using Dynamic Functions like cookies and headers.
  • Use segment config options.

Don't worry, we will be going through code examples for all of them.

Segment Config Options

Settings that can be applied to layout, pages and route handlers. These include but nor limited to:

// Caching behavior
export const dynamic = 'auto'
export const dynamic = 'force-dynamic' //no-caching

//Revalidation
export const revalidate = false
export const revalidate = 60 //Revalidates every minute

//Runtime
export const runtime = 'nodejs' //default
export const runtime = 'edge'

//Vercel region
export const preferredRegion = 'auto'
export const preferredRegion = ['iad1', 'hnd1'];

NextRequest & NextResponse

NextRequest and NextRequest are extensions of Web Request and Response APIs. These have few useful methods which are not present in Web Request and Response functions.

import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
const body = await req.json();
console.log(body);
return new NextResponse('All ok', {
status: 200,
});
}

Few use cases for NextRequest object include:

Method

Description

request.json()

Returns a promise that resolves with the result of parsing the request body as JSON.

request.text()

Returns a promise that resolves with a text representation of the request body.

request.blob()

Returns a promise that resolves with a Blob representation of the request body.

request.nextUrl.pathname

The pathname of the URL.

request.nextUrl.searchParams

The search parameters of the URL.

request.mode

Contains the mode of request

request.cookies.set(name, value)

Given a name, set a cookie with the given value on the request.

request.cookies.get(name)

Given a name, returns the value of the cookie. If not present, return undefined.

request.cookies.getAll()

Given a cookie name, return the values of the cookie. If no name is given, return all cookies on the request.

request.cookies.delete(name)

Given a cookie name, delete the cookie from the request.

request.cookies.has(name)

Returns true if cookie exists, false if it does not

request.cookies.clear()

Clears the Set-Cookie header from the request.

request.headers.get('X-Forwarded-For')

Gets IP address of the request

request.ip

Gets IP address of the request only for Vercel hosting.

NextResponse shares all the methods for setting, retrieving and deleting cookies with NextRequest. There a few additional method specific to NextResponse which include:

Method

Description

NextResponse.redirect(new URL(url, request.url))

Produce a response that redirects to a URL

NextResponse.json(body)

Produce a response with the given JSON body.

NextResponse.next()

The next() method is useful for Middleware, as it allows you to return early and continue routing.

Dynamic functions

Next.js provide dynamic functions for accessing cookies and headers in next/header library.

import { cookies, headers } from 'next/headers'

Next.js project initialization

We will be using TypeScript and pnpm for this project but you can use whatever you are comfortable with.

Step 1: Use one of the following commands to initialize the project.

#npm
npx create-next-app@latest

#yarn
yarn create next-app

#pnpm
pnpm create next-app

Choices:

240709_18h02m31s_screenshot

Step 2: Make a data store to perform operations and retrieve information. We will not be using any database in this demo. As Next.js is a serverless backend framework, state does not persist unlike Express.js.

Node
export const fakeUsers: TUser[] = [     {     id: 1,     name: "Sarthak Roy",     email: "[email protected]",     gender: "male",     address: "Kolkata, India",     favouriteAnime: "A Silent Voice",     },     {     id: 2,     name: "John McNormie",     email: "[email protected]",     gender: "other",     address: "Seattle, USA",     favouriteAnime: null,     },     {     id: 3,     name: "John Doe",     email: "[email protected]",     gender: "male",     address: "Springfield, USA",     favouriteAnime: "Naruto",     }, ]; 

Step 4: Start the dev server.

#npm
npm run dev

#yarn
yarn dev

#pnpm
pnpm dev

package.json

"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.5"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",fallback
"postcss": "^8",
"tailwindcss": "^3.4.1",
"eslint": "^8",
"eslint-config-next": "14.2.5"
}

Here is the code for root route. This is completely optional:

JavaScript
//app/page.tsx  export default function Page() {     return (         <main>             <h1>Next.js API routes demo</h1>         </main>     ) } 

Project Structure

240723_14h04m26s_screenshot
Folder Structure

Building REST API with Next.js Route Handlers

Get all users

As the name suggests, we will be using GET method for this operation. To get all user defile a route.ts file inside "/api/user".

Node
//app/api/user/route.ts import { fakeUsers } from "@/db/users"; import { NextResponse } from "next/server";  export async function GET() {      return new NextResponse(JSON.stringify(fakeUsers), {         status: 200,     }); } 

Now call the /api/user endpoint with GET method using Postman or Thunder Client.

Output

240723_11h40m24s_screenshot
How to Build a REST API with Next.js 13

Get specific user

Suppose you want to specific user with id. Next.js will provides Dynamic Route Segments to do this. These are similar to dynamic routes for pages. Convention is to make for square brackets and putting a route.ts file inside it.

Node
//app/api/user/[id]/route.ts import { fakeUsers } from "@/db/users"; import { NextRequest, NextResponse } from "next/server";  export async function GET(     req: NextRequest,     { params }: { params: { id: string } } ) {     const id = Number(params.id);     if (isNaN(id)) {         return new NextResponse("Invalid ID", {             status: 400,         });     }      const user = fakeUsers.find((u) => u.id === id);      if (!user) {         return new NextResponse("User not found", {             status: 404,         });     }      return new NextResponse(JSON.stringify(user), {         status: 200,     }); } 

Output:

240723_12h02m42s_screenshot
How to Build a REST API with Next.js 13

Search params

If user want to get users who have a favorite anime, we generally use search params for this.

Node
//app/api/user/route.ts import { fakeUsers } from "@/db/users"; import { NextRequest, NextResponse } from "next/server";  export async function GET(req: NextRequest) {     const searchParams = req.nextUrl.searchParams;     const hasFavoriteAnime = searchParams.get("hasFavoriteAnime");      if (Number(hasFavoriteAnime) === 1) {         const usersWithFavoriteAnime = fakeUsers.filter(             (user) => user.favouriteAnime !== null         );         return new NextResponse(JSON.stringify(usersWithFavoriteAnime), {             status: 200,         });     }      return new NextResponse(JSON.stringify(fakeUsers), {         status: 200,     }); } 

Output:

240723_12h57m22s_screenshot
How to Build a REST API with Next.js 13

Create new user

We are going to create a new user using POST. In the route.ts file in 'api/user' directory, paste the following code

Node
//app/api/user/[id]/route.ts import { fakeUsers } from "@/db/users"; import { NextRequest, NextResponse } from "next/server";  export async function POST(req: NextRequest) {     const body = await req.json();     const newUser = {         id: fakeUsers.length + 1,         ...body,     };     fakeUsers.push(newUser);     return new NextResponse(JSON.stringify(newUser), {         status: 201,     }); } 

Output:

240723_12h16m56s_screenshot
How to Build a REST API with Next.js 13

Change user details

Now lets use PUT method with Dynamic Route Segments to change user data.

JavaScript
//app/api/user/[id]/route.ts import { fakeUsers } from "@/db/users"; import { NextRequest, NextResponse } from "next/server";  export async function PUT(     req: NextRequest,     { params }: { params: { id: string } } ) {     const body = await req.json();     const id = Number(params.id);     if (isNaN(id)) {         return new NextResponse("Invalid ID", {             status: 400,         });     }      const user = fakeUsers.find((u) => u.id === id);      if (!user) {         return new NextResponse("User not found", {             status: 404,         });     }      const updatedUser = {         id: user.id,         ...body,     };      const index = fakeUsers.indexOf(user);     fakeUsers[index] = updatedUser;      return new NextResponse(JSON.stringify(updatedUser), {         status: 200,     }); } 

Output:

240723_12h29m10s_screenshot
How to Build a REST API with Next.js 13

Delete user

We are now going to delete the user using id property.

Node
//app/api/user/[id]/route.ts import { fakeUsers } from "@/db/users"; import { NextRequest, NextResponse } from "next/server";  export async function DELETE(     req: NextRequest,     { params }: { params: { id: string } } ) {     const id = Number(params.id);     if (isNaN(id)) {         return new NextResponse("Invalid ID", {             status: 400,         });     }      const user = fakeUsers.find((u) => u.id === id);      if (!user) {         return new NextResponse("User not found", {             status: 404,         });     }      const index = fakeUsers.indexOf(user);     fakeUsers.splice(index, 1);      return new NextResponse("User deleted", {         status: 200,     }); } 


Output:

240723_12h38m37s_screenshot
How to Build a REST API with Next.js 13

Next Article
Building a Task Reminder App with Next.js
author
sarthakroy107
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Next.js

Similar Reads

  • Build a Notes App with Next.js
    Note-taking App is a simple web application that allows users to create, edit, and delete text notes. The app provides a user-friendly interface for managing notes, making it easy to add new notes, update existing notes, and delete notes when they are no longer needed. The app provides a way for use
    4 min read
  • Building a Task Reminder App with Next.js
    In this article, we’ll walk through the step-by-step process of creating a basic task reminder app with Next.JS. This application will provide users with a user-friendly interface for adding the title of the task, its description, and its due date & time. The user will also be notified through a
    4 min read
  • How to use Next.js API Routes?
    Next.js API Routes are a feature of Next.js that allows you to create server-side logic and APIs within your Next.js application. These API routes are implemented using files in the `pages/api` directory of your Next.js project. When you deploy your Next.js application, these API routes are automati
    8 min read
  • How to Create A REST API With JSON Server ?
    Setting up a RESTful API using JSON Server, a lightweight and easy-to-use tool for quickly prototyping and mocking APIs. JSON Server allows you to create a fully functional REST API with CRUD operations (Create, Read, Update, Delete) using a simple JSON file as a data source. Table of Content GET Re
    4 min read
  • How To Push A Big Next.js App To GitHub?
    Managing a big Next.js and version control of the deployed Next. js application could be challenging sometimes, especially when it comes to file size and repository issues. This guide will help you no matter if you are an experienced programmer or a person who never used GitHub at all. It explains h
    3 min read
  • How to Create a New Next JS 13+ App?
    Creating a new Next.js 13+ application is a straightforward process that sets up a modern React-based framework with built-in features like server-side rendering, static site generation, and API routes. Next JS streamlines and automates the setup of essential React tooling, handling tasks such as bu
    2 min read
  • Build a Jokes Generator With Next JS and API
    Jokes generator application using Next Js is an application where we are using the external API to fetch the jokes. This application has features to copy the jokes and when the user clicks on "Get a Joke" new jokes will be generated. Preview of final output: Let us have a look at how the final outpu
    4 min read
  • How To Build Testimonial Slider with Tailwind CSS and Next.js?
    A testimonial slider is a dynamic component often used on websites to showcase customer feedback or reviews. It allows users to scroll through testimonials, typically featuring the customer's name, role, a quote about their experience, and sometimes an image. In this article, we'll build a testimoni
    5 min read
  • How to use TypeScript to build Node.js API with Express ?
    TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will
    4 min read
  • How to Add a Favicon to a Next.js Static Website?
    To add a favicon to a next.js static website we can utilize the public directory of our next js project. A favicon is a small icon that appears in the browser tab next to the page title. It helps users identify your website easily. In this article, we'll explore different approaches to adding a favi
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences