Api Routes

Simple API routes inside Next.js app like for form input

Just create a function inside the pages/api directory that has the following format:

/pages/api/hello.js

// req = request data, res = response data
export default (req, res) => {
  res.status(200).json({ text: 'Hello' })
}

http://localhost:3000/api/hello

They can be deployed as Serverless Functions (also known as Lambdas).

Do Not Fetch an API Route from getStaticProps or getStaticPaths, instead just run the code in them since it runs serverside anyway

Dynamic Api Routes

Last updated