← All Articles πŸ“ Code Patterns Medium By Nishant Kumar Verma

Backend for Frontend (BFF) Pattern: When to Use It

Backend for frontend (BFF) pattern: one backend per user experience. When to use it, how it keeps tokens out of the browser, its costs and a Next.js example.

Quick Answer

The backend for frontend (BFF) pattern gives each user experience its own small backend, owned by the team that builds that frontend. The BFF calls the shared services and returns exactly what its screen needs. It saves the frontend from many calls and a general-purpose API, and it can keep OAuth tokens out of the browser, at the cost of an extra network hop, some duplicated code, and requests that are harder to trace from the browser to the service that answers them.

What Is the Backend for Frontend Pattern?

Sam Newman described the pattern in Backends For Frontends (November 2015), after seeing it at REA and SoundCloud. He credits Phil CalΓ§ado, formerly of SoundCloud, with the name.

The idea: instead of one general-purpose API for every client, run one backend per user experience. The web app gets a BFF, the mobile app gets its own. Each BFF is tightly coupled to its frontend and is usually maintained by the same team. Newman quotes a guideline from Stewart Gleadow: "one experience, one BFF".

A BFF sits inside your perimeter. The browser or phone talks only to its BFF, and the BFF talks to the services behind it: users, orders, payments.

When to Use a BFF, and When Not To

Microsoft's Azure Architecture Center lists when the pattern fits:

  • A shared backend needs a lot of work to keep serving very different clients.
  • You want to tune the backend for one specific interface.
  • One language suits one interface's backend better than the others.

And when it might not: when every interface makes the same requests, or when only one interface uses the backend. GraphQL with frontend-specific resolvers, or an API gateway in front of your services, may be enough. Newman makes a similar point for the web: he suspects a web-only UI needs a BFF only when it does significant aggregation on the server.

BFF for Security: Keep Tokens Out of the Browser

Security is the other common reason to add a BFF. RFC 10017, OAuth 2.0 for Browser-Based Applications, an IETF Best Current Practice published in August 2026, describes a BFF with three jobs:

  • It talks to the authorization server as a confidential OAuth client.
  • It keeps the access and refresh tokens in a cookie-based session, so no token reaches the browser.
  • It adds the right access token to each request it forwards to the API.

A script injected into the page then has no token to steal. It can still send requests through the BFF from the user's browser, so a BFF does not replace XSS and CSRF defenses. The RFC also treats the BFF as part of the frontend application and its OAuth client, which is what sets it apart from an API gateway or a reverse proxy.

What a BFF Costs

CostWhat happens
LatencyEvery request takes an extra network hop through the BFF.
DuplicationMicrosoft calls duplicated code across BFFs "a probable outcome".
More servicesEach BFF has its own deploys, security and lifecycle.
Fan-outOne BFF call can trigger several service calls. Newman suggests calling them in parallel and degrading gracefully when one fails.
TraceabilityThe path in the browser is not the path of the service that answers it.

The last row is the one teams notice late.

A BFF in a Next.js App

In a Next.js app, a route handler often plays the BFF. The dashboard page needs a user and their orders, so it asks one route, and that route calls two services in parallel with the user's token.

// app/api/dashboard/route.ts
import { getAccessToken } from '@/lib/session'; // your helper: the token stored for this session cookie

const getJson = (url: string, token: string) =>
  fetch(url, { headers: { Authorization: `Bearer ${token}` } })
    .then((r) => (r.ok ? r.json() : null))
    .catch(() => null);

export async function GET() {
  const token = await getAccessToken();
  if (!token) return Response.json({ error: 'Not signed in' }, { status: 401 });

  const [user, orders] = await Promise.all([
    getJson(`${process.env.USERS_API}/users/me`, token),
    getJson(`${process.env.ORDERS_API}/orders?limit=5`, token),
  ]);
  if (!user) return Response.json({ error: 'Users service unavailable' }, { status: 502 });
  return Response.json({ name: user.name, recentOrders: orders ?? [] });
}

If the orders service fails, the page still loads with an empty list: the graceful degradation Newman recommends. The browser holds only the session cookie.

The page calls fetch('/api/dashboard'). Clean for the page, but follow a bug: the browser shows one request to /api/dashboard, and nothing in the page's code names /users/me or /orders. A search for the orders path finds only the BFF. Someone who changes the orders service has to know that this BFF calls it.

Catch-all proxies hide it further. A route like app/api/proxy/[...path]/route.ts forwards any path to the backend, so the frontend's request to /api/proxy/orders and the backend route /orders never appear side by side in the code.

How to Keep BFF Requests Traceable

  • Name the BFF routes after screens, such as /api/dashboard, not after services, so it is clear which frontend owns each one.
  • Keep service base URLs in one place per BFF, so a search for a service finds every caller.
  • Pass a request ID from the BFF to every service it calls, so logs show one request across the hops.
  • Map requests to routes from the code, so you can see which page calls which BFF route, and which service routes that BFF reaches. Our guide to mapping frontend API calls to backend endpoints shows how.

How Vibe X-Ray Traces Requests Through a BFF

VibeDoctor Vibe X-Ray has an HTTP Requests tab that links each request in your code to the route that answers it.

  • Each row shows the function and file:line that sends the request, its method and path, and the route that answers it, with its own file and line.
  • A request that passes a proxy says so: "through the proxy" and the proxy's path. X-Ray reads Next.js rewrites, Vite and Create React App proxy settings, Angular proxy configs, Vercel and Netlify rewrites, nginx proxy_pass, Caddy reverse_proxy, and catch-all routes that pass requests on.
  • When a service lives in another of your projects, X-Ray follows the request there: by the project's website address, or by a link you add under Linked projects in Settings.
  • Calls that leave your app, such as api.stripe.com, are marked as outside services.

Search the tab by function, file, path, method or host. After the Free Week, Free accounts see the first level of the map and the first few requests; paid plans see where every request goes.

To try VibeDoctor from Claude Code or Codex, paste this into the agent:

connect to vibedoctor using https://vibedoctor.io/mcp/start

Not a developer? Connect your GitHub repository on vibedoctor.io for a free scan.

FAQ

What is a backend for frontend?

A small backend built for one user experience, such as the web app or the mobile app. It calls the shared services and returns what that frontend needs, and the frontend team usually owns it.

What is the difference between a BFF and an API gateway?

An API gateway is one entry point shared by all clients. In Microsoft's example it handles authorization, monitoring, request caching and routing to the right BFF, and sits in front of a mobile BFF and a desktop BFF. A BFF is per frontend and shapes data for that frontend's screens.

Does a BFF make a web app more secure?

It keeps OAuth tokens out of the browser: in the design RFC 10017 describes, the BFF holds the tokens and the browser gets only a session cookie. Injected scripts can still send requests through the BFF, so you still need XSS and CSRF defenses.

How many BFFs should I have?

Newman quotes Stewart Gleadow's guideline, one per user experience, and names your organisation structure as a main driver: he is more relaxed about one BFF for both mobile apps when the same team owns both apps and the BFF.

Is a Next.js API route a BFF?

It can be. When a route handler serves one frontend's screens and calls other services to build its response, it is playing the BFF role, with the same extra hop and the same tracing problem.

Does a BFF add latency?

Yes, one network hop per request. It can still make the page faster overall when it replaces several calls from the browser with one, and calls the services in parallel.

Diagnose your codebase - free

VibeDoctor checks for and 148 other issues across 21 diagnostic areas - security, performance, code quality, and more.

SCAN MY APP →