reactjstypescriptmongodbnext.jsecmascript-next

Next.js 15 API Route - “Invalid GET Export” Error in Dynamic Route [id]


I’m working on a Next.js 15 project using the App Router (/app/api/) and trying to implement a dynamic API route (/api/agencies/[id]) to fetch a specific agency from MongoDB.

However, during npm run build, I get this TypeScript error: src/app/api/agencies/[id]/route.ts Type error: Route "src/app/api/agencies/[id]/route.ts" has an invalid "GET" export: Type "{ params: { id: string; }; }" is not a valid type for the function's second argument.

What I Have Tried So Far:

  1. Modified the function signature for GET, PATCH, and DELETE requests:

export async function GET(req: NextRequest, context: { params: { id: string } }) {

❌ Still getting the “invalid GET export” error.

  1. Changed params type to Record<string, string>:

export async function GET(req: NextRequest, context: { params: Record<string, string> }) {

❌ No luck.

  1. Tried wrapping params in an optional object:

export async function GET(req: NextRequest, context: { params?: { id?: string } }) {

❌ Still getting the same error.

  1. Checked TypeScript compatibility in tsconfig.json:
"target": "esnext",
"module": "esnext",
"strict": true,

• No improvements.

  1. Tried awaiting params before using it:

const { id } = await context.params;

❌ Still doesn’t work.

📌 Here’s My API Route Code (/app/api/agencies/[id]/route.ts)

import { NextRequest, NextResponse } from "next/server";
import { connectToDatabase } from "@/utils/db";
import Agency from "@/models/agency";
import mongoose from "mongoose";

// Helper function for error responses
const errorResponse = (message: string, status: number) => {
  return NextResponse.json({ error: message }, { status });
};

// GET: Fetch a Single Agency by ID (Fails on Build)
export async function GET(
  req: NextRequest,
  context: { params: { id: string } }
) {
  try {
    await connectToDatabase();

    // Validate ID format
    if (!context.params || !context.params.id || !mongoose.isValidObjectId(context.params.id)) {
      return errorResponse("Invalid agency ID", 400);
    }

    const agency = await Agency.findById(context.params.id).lean();
    if (!agency) {
      return errorResponse("Agency not found", 404);
    }

    return NextResponse.json({ ...agency, id: String(agency._id) });
  } catch (error) {
    console.error("❌ Error fetching agency:", error);
    return errorResponse("Failed to fetch agency", 500);
  }
}

📌 My Environment:

Any insights would be greatly appreciated! 🙏


Solution

  • From the Nextjs Docs """ context (optional)

    params: a promise that resolves to an object containing the dynamic route parameters for the current route.

        export async function GET(
          request: Request,
          { params }: { params: Promise<{ team: string }> }
        ) {
          const team = (await params).team
        }
    

    """

    https://nextjs.org/docs/app/api-reference/file-conventions/route