Sling Academy
Home/Next.js/Next.js API Routes: How to Get Parameters & Query String

Next.js API Routes: How to Get Parameters & Query String

Last updated: January 14, 2023

With Next.js, you can build your backend API right in the pages/api directory of your project. Suppose you want to fetch data about some fictional products from your database and send them to the client. To get a list of products, use this route:

/api/products

// add file: pages/api/products.js

If you have tons of products, it’s likely that you will need pagination. The backend will require information to paginate data; at least there should be the current page and the number of results per page ((in more complex cases, there is also sort order, sorting criteria, classification, etc.). Therefore, the client will send a GET request to /api/products with a query string like so:

/api/products?page=1&limit=10

To get page (the current page) and limit (the number of results per page), you can do as follows:

// pages/api/products.js
export default async function products(req, res) {
    const query = req.query;
    const { page, limit } = query;

    // use the information from the query to get the products
    // then send the data back to the client
}

To get detailed information about a single product, we use this dynamic route:

/api/products/[productId]

// add file: pages/api/products/[productId].js

To extract productId, you can do this:

// pages/api/products/[productId].js
export default async function poduct(req, res){
    const query = req.query;
    const { productId } = query;
    
    // use productId to retrieve the product from your database
    // then send this data back to the client
}

Make sure the file structure in your pages directory is similar to this:

The tutorial ends here. If you have any questions about what we’ve discussed, please leave a comment. Happy coding and have a nice day!

Next Article: Next.js 14: Extracting Request Body in Route Handlers

Previous Article: Next.js: How to Get User’s IP Address

Series: Route Handlers and API Routes in Next.js

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported