#File download route

1 messages · Page 1 of 1 (latest)

last cosmos
#

How can I implement and API endpoint which will call external API and download a file

snow timberBOT
#

Hi @last cosmos. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To implement an API endpoint in Next.js that calls an external API and downloads a file, you can use the built-in API routes feature. You can create a new file in the pages/api directory, and this file will be treated as an API endpoint.

Here's a basic example of how you can structure your API route:

export default async function handler(req, res) {
  // Call the external API
  const externalRes = await fetch('<http://external-api.com/file'>);

  // Set the headers and status code from the external response
  res.status(externalRes.status);
  for (const [key, value] of externalRes.headers.entries()) {
    res.setHeader(key, value);
  }

  // Send the file data in the response
  res.send(await externalRes.buffer());
}

In this example, fetch is used to call the external API and download the file. The headers and status code from the external response are then set on the Next.js API response. Finally, the file data is sent in the response using res.send.

Please note that this is a basic example and you might need to adjust it based on the specifics of the external API and the file you are downloading.

For more information on API routes in Next.js, you can refer to the official documentation (https://nextjs.org/docs/api-routes/introduction).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@last cosmos kapa.ai is still learning and improving, please let us know how it did by reacting below

last cosmos
#

@snow timber I want to implement this in next 14 with APP router handlers

snow timberBOT
#

Let me check the knowledge base for you. One moment! :books:

#

In Next.js 14, you can use Route Handlers to create custom request handlers for a given route. Route Handlers are defined in a route.js|ts file inside the app directory. They can handle different HTTP methods like GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS.

Here's a basic example of how you can structure your Route Handler:

// app/api/fileDownload/route.ts
export const dynamic = 'force-dynamic' // defaults to auto

export async function GET(req, res) {
  // Call the external API
  const externalRes = await fetch('<http://external-api.com/file'>);

  // Set the headers and status code from the external response
  res.status(externalRes.status);
  for (const [key, value] of externalRes.headers.entries()) {
    res.setHeader(key, value);
  }

  // Send the file data in the response
  res.send(await externalRes.buffer());
}

In this example, the GET function is used to handle GET requests to the route. fetch is used to call the external API and download the file. The headers and status code from the external response are then set on the Next.js API response. Finally, the file data is sent in the response using res.send.

Please note that this is a basic example and you might need to adjust it based on the specifics of the external API and the file you are downloading.

For more information on Route Handlers in Next.js, you can refer to the official documentation (https://nextjs.org/docs/app/building-your-application/routing/route-handlers).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: