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:
export async function GET(req: NextRequest, context: { params: { id: string } }) {
❌ Still getting the “invalid GET export” error.
export async function GET(req: NextRequest, context: { params: Record<string, string> }) {
❌ No luck.
export async function GET(req: NextRequest, context: { params?: { id?: string } }) {
❌ Still getting the same error.
"target": "esnext",
"module": "esnext",
"strict": true,
• No improvements.
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! 🙏
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