When the frontend calls the API directly
Many Nuxt projects I take over follow the same pattern. The app uses ssr: false because it is a back office or an internal tool that does not need SEO. The frontend calls the upstream API directly. Its URL sits in runtimeConfig.public, so it goes out in the JavaScript bundle sent to every user.
When I ask why the project skips Nuxt server routes, teams often say they have no SSR and assume they can do without a server. That assumption leaves one of the framework's most useful features on the table.
I now use a backend for frontend (BFF) on almost all my Nuxt projects, including client-rendered ones. It changes which data the browser receives, where I do the work, and how I organise the code.
What a BFF does
A BFF is a server layer dedicated to one frontend. The browser talks to that layer, which calls the upstream API, third-party services or a database.
Browser -> /api/... (Nuxt server / Nitro) -> upstream API, third-party services
In Nuxt, Nitro already provides this layer through the files in server/api/. The BFF ships with the frontend from the same repository and can share the same language and types.
I used this pattern in GUARDIAN, my Docker monitoring dashboard. The frontend calls Nuxt server routes. Those routes talk to the Docker daemon and keep the socket on the server.
Nitro still runs with ssr: false
Server-side rendering is one of Nitro's jobs. Setting ssr: false moves HTML rendering to the browser.
With a standard nuxt build targeting Node, the server still serves the application and handles requests to /api/*.
Static generation changes the deployment model. A site generated with nuxt generate runs from static files. Server routes need a runtime, so account for that when choosing how to host the project.
What stays on the server
Hiding the upstream API URL is a side effect of using a BFF. The upstream API still needs its own access controls. The BFF gives you a place to keep secrets and control what the browser can request and receive.
Keep credentials in private configuration. API keys, service tokens and partner credentials belong in the private runtimeConfig that Nitro reads on the server. During audits, I often find secrets that ended up in public.
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
apiBaseUrl: '', // overridden by NUXT_API_BASE_URL
apiSecret: '', // overridden by NUXT_API_SECRET
public: {} // keep secrets out of public config
}
})
Keep the session token out of frontend JavaScript. The browser sends the HttpOnly cookie automatically. The server route reads it and adds the token to the upstream request. The HttpOnly flag prevents scripts from reading the cookie directly.
// server/api/me.get.ts
export default defineEventHandler(async (event) => {
const token = getCookie(event, 'session')
if (!token) throw createError({ statusCode: 401 })
const { apiBaseUrl } = useRuntimeConfig(event)
return $fetch(`${apiBaseUrl}/me`, {
headers: { Authorization: `Bearer ${token}` }
})
})
Apply controls at a single entry point. Server routes give the frontend team a place to validate input, rate-limit requests, log events and filter responses. That can be useful when another team owns the upstream API.
Send the frontend the fields it needs
A screen shows a guest name, a date and a status. Open the network panel and you might find forty fields in the response, including email addresses, phone numbers, internal IDs and billing details. The browser receives every field regardless of which ones the page renders.
An upstream API often serves several clients. A server route can shape its response for the screen that needs it.
// server/api/bookings/[id].get.ts
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, 'id')
const { apiBaseUrl } = useRuntimeConfig(event)
const booking = await $fetch(`${apiBaseUrl}/bookings/${id}`)
return {
id: booking.id,
guestName: `${booking.guest.firstName} ${booking.guest.lastName}`,
arrival: booking.stay.checkIn,
status: booking.status
}
})
- Less sensitive data in the browser. The browser only receives the selected fields.
- Smaller payloads. The difference can matter on a mobile connection.
- End-to-end type inference. Nuxt infers the return type of the route. The component can use
useFetch('/api/bookings/42')without maintaining a duplicate response interface.
Let composables manage interface state
For a long time I put transformations in composables. They merged responses, recalculated totals, grouped results, sorted them and formatted them. In a client-rendered app, the browser downloads that code and runs it on the user's device. Anyone can also inspect the processing rules in the bundle.
After moving that work to the server routes, my composables look like this.
// app/composables/useBooking.ts
export function useBooking(id: MaybeRefOrGetter<string>) {
return useFetch(() => `/api/bookings/${toValue(id)}`)
}
The composable manages reactive state, loading and errors. The server handles the heavier processing, which can be cached where appropriate. The route is easier to test as a function that receives a request and returns an object, with no component to mount.
Organise server routes by screen and action
Server routes use the same file-based routing idea as pages/. A method suffix in the filename lets each handler deal with one HTTP method.
server/api/
bookings/
index.get.ts -> GET /api/bookings
index.post.ts -> POST /api/bookings
[id].get.ts -> GET /api/bookings/:id
[id]/cancel.post.ts -> POST /api/bookings/:id/cancel
dashboard.get.ts -> GET /api/dashboard
The filename tells you where to find each action and which HTTP method it handles.
A BFF route can serve a screen. For example, /api/dashboard makes three upstream calls in parallel and returns the fields the dashboard uses.
// server/api/dashboard.get.ts
export default defineEventHandler(async (event) => {
const { apiBaseUrl } = useRuntimeConfig(event)
const [arrivals, departures, alerts] = await Promise.all([
$fetch(`${apiBaseUrl}/arrivals/today`),
$fetch(`${apiBaseUrl}/departures/today`),
$fetch(`${apiBaseUrl}/alerts`)
])
return { arrivals: arrivals.length, departures: departures.length, alerts }
})
The browser makes one request. The remaining calls run between servers, where latency depends on how the services are deployed.
Validate incoming data. Use getValidatedQuery and readValidatedBody with a schema such as Zod to check input before forwarding it.
// server/api/bookings/index.post.ts
import { z } from 'zod'
const schema = z.object({
guestId: z.string().uuid(),
checkIn: z.string().date(),
nights: z.number().int().min(1).max(30)
})
export default defineEventHandler(async (event) => {
const body = await readValidatedBody(event, schema.parse)
// ... call the upstream API with validated data
})
Costs and trade-offs
There is an extra network hop. A route that only forwards calls adds latency and another point of failure. Give it a job such as enforcing access rules, filtering fields, aggregating calls or caching responses.
The server needs hosting and monitoring. It runs as a process, uses memory and can fail. Monitor it like the rest of your services.
Upstream errors can expose internal details. Log details such as upstream stack traces on the server and use createError to return an appropriate response to the browser.
Cache entries need the right scope. defineCachedEventHandler is useful for public or relatively stable data. A cache key that ignores the session can expose one user's data to another. When in doubt, keep session-specific responses out of shared caches.
Where each responsibility belongs
- The frontend displays the result. Keep secrets, business rules and response shaping on the server.
- The browser receives only what it uses. This reduces data exposure and the JavaScript that runs on the device.
- Nuxt already provides the server layer. Use Nitro and the
server/directory in the existing project.
If your Nuxt application calls an API directly from the browser, open the network panel and check which fields your users receive. Which of them does the interface actually need?
Need a Nuxt application audit or help structuring your frontend architecture? Get in touch to review what you have and discuss changes that fit your project.

