No, if you are using tRPC, you would generally not call fetch(/api/abc) for your tRPC procedures.
Let's clarify the roles of your files and how tRPC works with Next.js:
-
api/abc/route.ts (Next.js App Router API Route):
- This defines a traditional REST-like API endpoint at the path
/api/abc.
- If you create this file, you would make
fetch requests directly to /api/abc (e.g., fetch('/api/abc', { method: 'POST', body: JSON.stringify({ /* ... */ }) })).
- This is separate from tRPC.
-
server/ (tRPC Definitions):
- This directory (e.g.,
server/routers/, server/trpc.ts, server/index.ts) is where you define your tRPC routers, procedures (queries, mutations), and your context.
- These files do not directly create HTTP endpoints. They define the logic and types of your API.
-
The tRPC Catch-All API Route (api/trpc/[...trpc]/route.ts):
- This is the single Next.js API route that acts as the entry point for all your tRPC calls.
- It typically looks like this (simplified):
// app/api/trpc/[...trpc]/route.ts
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
import { appRouter } from '@/server/index'; // Your main tRPC router definition
const handler = (req: Request) =>
fetchRequestHandler({
endpoint: '/api/trpc', // This is the base path for all tRPC calls
req,
router: appRouter,
createContext: () => ({}), // Your tRPC context
});
export { handler as GET, handler as POST };
How tRPC Client Calls Work:
When you use the tRPC client on your frontend (e.g., within React components), it generates a proxy that makes internal HTTP requests to your single tRPC catch-all endpoint.
Instead of fetch('/api/abc'), you would use the tRPC client like this:
// On your client-side component or utility file:
import { trpc } from '@/utils/trpc'; // Assuming you set up your tRPC client here
async function fetchData() {
try {
// If you have a 'posts' router with a 'getById' query in your tRPC setup
const post = await trpc.posts.getById.query({ id: 123 });
console.log(post);
// If you have a 'users' router with a 'createUser' mutation
const newUser = await trpc.users.createUser.mutate({ name: 'Alice', email: 'alice@example.com' });
console.log(newUser);
} catch (error) {
console.error('Failed to fetch data:', error);
}
}
// Call it in a useEffect or on an event
// useEffect(() => { fetchData(); }, []);
Summary:
- You define your tRPC procedures in
server/.
- You have one Next.js API route (
api/trpc/[...trpc]/route.ts) that handles all tRPC requests.
- On the client, you interact with tRPC using the generated type-safe client (e.g.,
trpc.yourRouter.yourProcedure.query()), not direct fetch calls to individual endpoint paths.
Can you have both?
Yes! You can perfectly have a mixed architecture:
- Traditional Next.js API routes (
app/api/my-legacy-endpoint/route.ts) for things that might be consumed by non-TypeScript clients, external services, or for specific file uploads/downloads that might not fit tRPC's paradigm perfectly.
- tRPC (
app/api/trpc/[...trpc]/route.ts) for all your internal, type-safe API interactions within your Next.js application.
So, if you called fetch(/api/abc) before, and you still have app/api/abc/route.ts defined, it will still work as a separate, traditional API route. But it will not be handled by your tRPC setup in server/.