Sites without users

Search box that calls a paid API

access: 'visitor'rateLimiterdefineCachedFunction
Keep other sites and scripts from spending your API quota through your BFF.

A search box, a translation widget or an AI assistant on a public site often calls a paid API with your key. The key stays on the server, but the BFF route itself becomes a free proxy to that API unless you protect it.

nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@kurashina-design/nuxt-bff-guard', 'nuxt-security'],
  bffGuard: { access: 'visitor' },
  runtimeConfig: { searchApiKey: '' }, // NUXT_SEARCH_API_KEY
  routeRules: {
    '/api/search': {
      security: { rateLimiter: { tokensPerInterval: 30, interval: 60_000 } },
    },
  },
})
server/utils/search.ts
// Identical queries within 10 minutes are answered from the cache, not the paid API.
export const searchCatalog = defineCachedFunction(async (query: string) => {
  return await $fetch<SearchResult[]>('https://search.example/v1/query', {
    headers: { 'x-api-key': useRuntimeConfig().searchApiKey },
    query: { q: query, limit: 10 },
  })
}, { maxAge: 600, name: 'search', getKey: (query: string) => query.toLowerCase() })
server/api/search.get.ts
import { defineBffHandler } from '#bff-guard'

export default defineBffHandler(async (event) => {
  const q = String(getQuery(event).q ?? '').trim().slice(0, 100)
  if (q.length < 2) return []
  return await searchCatalog(q)
})

The route is a GET, and visitor routes check reads too. Another website cannot use its visitors' browsers to query your API, and a URL like /api/search?q=… pasted into the address bar answers 403.

Cache the paid call in a server function, not the route: the route's response carries Cache-Control: private, no-store, and Nitro's route cache would hide the request headers the guard checks.