Your route.js / route.ts likely needs to force dynamic rendering.
export const dynamic = "force-dynamic";
Otherwise during the build process. Next will try and pre-render that page. That means it calls APIs, needs process.env, etc. It will do this even though route.* would lead you to believe it's a server side route.
Source: https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config