Documentation
Api

Use an API route

We will be using swr to fetch data from the api. If you want you can use fetch or axios too.

Define a fetcher function

Define a fetcher function that will be used to fetch data from the api.

const getFolderFetcher = async (url: string) => {
  const base_url = process.env.NEXT_PUBLIC_APP_URL;
 
  const response = await fetch(`${base_url}${url}`, {
    method: "GET",
    headers: {
      "Content-Type": "application/json",
    },
  });
 
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.message || "An unexpected error occurred");
  }
 
  return response.json();
};

Use swr to fetch data from the api.

const { data, error, isLoading } = useSWR(`/api/folder-name`, getFolderFetcher);

On this page