Posted on: 24/04/2026
Approaches to fetching data API layer APIs are an intermediary layer btw your application code and database. Cases when u might use an API: a third party service that provides an API when fetching data from the client, u want to have an API layer that runs on the server to avoid exposing db secrets to the client In Next.js u create API endpoints using Route Handlers Database queries In a fullstack application, u also need to write logic to interact with ur database. In relational databases like Postgres, u can do with SQL or with an ORM. Cases when u write database queries: when creating ur API endpoints, u need to write logic to interact with the db when using React Server components (fetching data on the server) u can skip the API layer, and query ur database directly without risking to expose db secrets to the client. Note: when fetching data on the client, you should not query your database directly, as this would expose your database secrets.