{"id":158,"hash":"706530f207cfc44938cec3c6005e099fb7172d11019fbf27af9516000e87dbf1","pattern":"Next 13.4 Error: NEXT_REDIRECT in API routes","full_message":"My /app/api/auth/route.ts file:\n\nimport { redirect } from 'next/navigation';\n    \nexport async function GET(req: Request) {\n  try {\n    redirect('/dashboard');\n  } catch (error) {\n    console.log(error);\n    redirect('/');\n  }\n}\n\nI realized that when I do redirect in a try catch, I get the error :\n\nError: NEXT_REDIRECT\n        at getRedirectError (webpack-internal:///(sc_server)/./node_modules/next/dist/client/components/redirect.js:40:19)\n        at redirect (webpack-internal:///(sc_server)/./node_modules/next/dist/client/components/redirect.js:46:11)\n        at GET (webpack-internal:///(sc_server)/./app/api/auth/route.ts:23:66)\n        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n        at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:244:37) {\n      digest: 'NEXT_REDIRECT;replace;/dashboard'\n    }\n\nWhen I get get rid of the try catch everything works fine:\n\nexport async function GET(req: Request) {\n  redirect('/dashboard')\n}\n\nThis works as expected. I need try and catch because this is an auth route and I need some error handling because the request could fail, I have left out the auth functionalities because I realized that this happens just on a simple try and catch.\n\nOr if Next 13 has another way of error handling in /api routes please let me know.","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"When using redirect in a server component you should move it to the finally block. Here is how:\n\nexport default async function Page() {\n    let redirectPath: string | null = null\n\n    try {\n        //Rest of the code\n        redirectPath = `/dashboard`\n    } catch (error) {\n        //Rest of the code\n        redirectPath = `/`\n    } finally {\n        //Clear resources\n        if (redirectPath)\n            redirect(redirectPath)\n    }\n\n    return <>{/*Rest of JSX*/}</>\n}\n\nFrom the redirect documentation:\n\nredirect internally throws an error so it should be called outside of try/catch blocks.\n\nIn api routes you should use the NextResponse from next/server. Here is how\n\nimport { NextRequest, NextResponse } from \"next/server\";\n\nexport function GET(request: NextRequest) {\n    try {\n        //Code\n        return NextResponse.redirect(`<an absolute url>`)\n    } catch {\n        //Error handling code\n        return NextResponse.redirect(`<an absolute url>`)\n    } finally {\n        //Clear resources\n        //No redirect here\n    }\n}","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/76191324/next-13-4-error-next-redirect-in-api-routes","votes":66,"created_at":"2026-04-19T04:41:25.075024+00:00","updated_at":"2026-04-19T04:51:06.847089+00:00"}