How To Fetch Data From APIs In NextJS?
Last Updated :
31 Jul, 2024
Fetching data from APIs in Next.js can be done using built-in methods like getServerSideProps, getStaticProps, or client-side fetching with useEffect. This flexibility supports both server-side and static data fetching.
Prerequisites:
Approach
To fetch data From API in next.js we will simply use the JavaSript fetch method along with async and await. This method is used to get the data from the API as an asynchronous process and display it on the webpage when data is available.
For different purposes like server-side data fetching we can use it with built-in methods provided by Next.js for data fetching and use useEffect hook while working on client side.
Steps to Setup a NextJS App
Step 1: Create a NextJS application using the following command.
npx create-next-app@latest app_name
Step 2: It will ask you some questions, so choose as the following.
√ Would you like to use TypeScript? ... No
√ Would you like to use ESLint? ... Yes
√ Would you like to use Tailwind CSS? ... No
√ Would you like to use `src/` directory? ... Yes
√ Would you like to use App Router? ... Yes
√ Would you like to customize the default import alias (@/*)? ... No
Step 3: After creating your project folder, move to it using the following command.
cd app_name
Project Structure:
The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.3"
},
"devDependencies": {
"postcss": "^8",
"tailwindcss": "^3.4.1"
}
Example: Implementation to fetch data from APIs in NextJS.
JavaScript
// src/app/page.js
"use client";
import { useState } from "react";
export default function Home() {
const [data, setData] = useState("");
const fetchData = async () => {
let response = await fetch("https://catfact.ninja/fact");
response = await response.json();
setData(response);
};
console.log(data);
return (
<>
<button onClick={fetchData}>Fetch Data</button>
<p>{data.fact}</p>
</>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project
npm run dev
Output: Your project will be shown in the URL http://localhost:3000/
How To Fetch Data From APIs In NextJS?
Similar Reads
How to Load Data from a File in Next.js? Loading Data from files consists of using client-side techniques to read and process files in a Next.js application. In this article, we will explore a practical demonstration of Load Data from a File in Next.js. We will load a validated CSV input file and display its contents in tabular format. App
3 min read
How to Fetch data faster in Next.js? NextJS, a popular React framework provides various approaches to fetch data efficiently. Optimizing data fetching can significantly improve the performance and user experience of your NextJS applications. We will discuss different approaches to Fetch data faster in NextJS: Table of Content Static Ge
6 min read
How To Use JavaScript Fetch API To Get Data? The Fetch API is a modern way to make HTTP requests in JavaScript. It is built into most browsers and allows developers to make network requests (like getting data from a server) in a simple and efficient way. The Fetch API replaces older techniques like XMLHttpRequest and jQuery's AJAX methods.Synt
5 min read
How to use JavaScript Fetch API to Get Data ? The Fetch API provides a JavaScript interface that enables users to manipulate and access parts of the HTTP pipeline such as responses and requests. Fetch API has so many rich and exciting options like method, headers, body, referrer, mode, credentials, cache, redirect, integrity, and a few more. H
2 min read
How to Catch All Routes in Next.js ? To catch all routes in Next.js, you create a dynamic route file using the catch-all segments syntax. This allows you to handle various routes dynamically, enhancing routing flexibility and application structure.Catch all routesTo catch all routes in next js, We willCreate a file named [...gfg].js in
2 min read