API Routes

Build type-safe APIs with Next.js App Router and optional tRPC integration.

API Architecture

Two Approaches Available

Next.js App Router API Routes

Standard REST API endpoints using Next.js 15 App Router with full TypeScript support.

tRPC Integration (Optional)

End-to-end type safety with tRPC for seamless client-server communication.

Next.js API Routes

Basic API Route Structure

The went framework provides a complete API structure using Next.js 15 App Router. API routes are located in the `src/app/api` directory and follow the standard Next.js route handler pattern with full TypeScript support.

Each route handler exports named functions (GET, POST, PUT, DELETE) that receive a Request object and return a Response. The went framework includes built-in integration with Prisma for database operations and NextAuth.js for authentication.

Database Integration

API routes in went framework come pre-configured with Prisma ORM for type-safe database operations. The framework includes a centralized database client that can be imported and used across all your API routes.

Dynamic route parameters are fully supported with TypeScript inference, allowing you to build RESTful APIs with proper type safety throughout your application.

API Best Practices

Type Safety

  • • Define interfaces for all request/response types
  • • Use Zod for runtime validation
  • • Leverage Prisma types for database operations
  • • Consider tRPC for end-to-end type safety

Security

  • • Always validate and sanitize input data
  • • Implement proper authentication checks
  • • Use CORS headers appropriately
  • • Rate limit your API endpoints

Performance

  • • Use database indexes for frequently queried fields
  • • Implement pagination for large datasets
  • • Cache responses when appropriate
  • • Use select to limit returned fields

Error Handling

  • • Return consistent error response formats
  • • Use appropriate HTTP status codes
  • • Log errors for debugging and monitoring
  • • Never expose sensitive information in errors

Next Steps

Continue your journey with Went: