Beta
Search
CTRLK

Next.js Adapter

Use Filenest with the Next.js app router

Add Filenest to your Next.js API

This guide assumes you have already instantiated a provider. If not, check out this section.

Install dependencies

If you have already installed @filenest/adapter-nextjs, you can skip this step.

npm i @filenest/adapter-nextjs

Initialize the adapter

Create the file route.ts at /app/api/filenest/[...handler]/route.ts.

If you like, you can choose a different directory.
Just make sure that you place the route.ts file in a folder called [...handler].

This example is using the uploadthing provider as the data source.

/app/api/filenest/[...handler]/route.ts
import { initNextjsAdapter } from "@filenest/adapter-nextjs"
import { UploadThing } from "@filenest/provider-uploadthing"
 
const provider = new UploadThing({
  UPLOADTHING_TOKEN: process.env.UPLOADTHING_TOKEN!,
})
 
export const { GET, POST } = initNextjsAdapter(provider).create()

Middleware

The use of middleware is strongly recommended to prevent unauthorized access to your provider.

export const { GET, POST } = initNextjsAdapter(provider)
  .use(async (req) => {
    const isAuthed = req.cookies.get("authed")
    if (!isAuthed?.value) {
      return NextResponse.json("Unauthorized", { status: 401 })
    }
  })
  .create()
  • Return any HTTP error to prevent further code execution
  • Execution will continue if you either return nothing or a NextResponse with a status of 200-299

On this page