Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2.2K
Views
Next.js with Swagger

Is there a way to have a swagger documentation for NEXT.js API routes? I'm using Next.js for both front-end and back-end development and I want to have a swagger documentation for the APIs I develop with Next.js.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use the following project to bootstrap the integration between Next.js and Swagger

  • https://github.com/jellydn/next-swagger-doc
  • https://www.npmjs.com/package/next-swagger-doc
  • Demo
yarn add next-swagger-doc swagger-ui-react
import { GetStaticProps, InferGetStaticPropsType } from 'next';

import { createSwaggerSpec } from 'next-swagger-doc';
import SwaggerUI from 'swagger-ui-react';
import 'swagger-ui-react/swagger-ui.css';

const ApiDoc = ({ spec }: InferGetStaticPropsType<typeof getStaticProps>) => {
  return <SwaggerUI spec={spec} />;
};

export const getStaticProps: GetStaticProps = async ctx => {
  const spec: Record<string, any> = createSwaggerSpec({
    title: 'NextJS Swagger',
    version: '0.1.0',
  });
  return { props: { spec } };
};

export default ApiDoc;

The actual definition of a Next.js API route endpoint looks like this:

/**
 * @swagger
 * /api/hello:
 *   get:
 *     description: Returns the hello world
 *     responses:
 *       200:
 *         description: hello world
 */
const handler = (_req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).json({
    result: 'hello world',
  });
};
over 4 years ago · Santiago Trujillo Report

0

I made a little npm library that auto-generates Swagger docs from your NextJS project. No user input needed!

Still very Beta but hope it's useful. https://www.npmjs.com/package/nextjs-routes-docs

Just run

npx nextjs-routes-docs [dir]
over 4 years ago · Santiago Trujillo Report

0

You got to have the swagger json files or specification for the APIS and can use libs such as Swagger UI or Redoc

Using Redoc you can do a /doc/[slug].js and do dynamic fetching on the .json or yaml files for your documentation (or swagger ui)

This website: https://openapi.tools/ , has a lot of tools for React and OpenAPI in general that you can use it.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!