When run with Node.js, a version of 22 (LTS) or higher is required. Please
verify your version by running node -v, and
upgrade if necessary.
Now create a bknd.config.ts file in the root of your project. If you created the project using the CLI starter, this file is already created for you.
bknd.config.ts
import type { ReactRouterBkndConfig } from "bknd/adapter/react-router";export default { connection: { url: "file:data.db", },} satisfies ReactRouterBkndConfig;
See bknd.config.ts for more information on how to configure bknd. The ReactRouterBkndConfig type extends the BkndConfig type with the following additional properties:
type ReactRouterEnv = NodeJS.ProcessEnv;type ReactRouterFunctionArgs = { request: Request;};export type ReactRouterBkndConfig<Env = ReactRouterEnv> = FrameworkBkndConfig<Env>;
For convenience, you can create a helper file to instantiate the bknd instance and retrieve the API. This is optional but recommended as it simplifies usage throughout your app. The examples below assume you've created this helper, but you can adjust the approach according to your needs.
app/bknd.ts
import { type ReactRouterBkndConfig, getApp as getBkndApp,} from "bknd/adapter/react-router";import config from "../bknd.config";export { config };// you may adjust this function depending on your runtime environment.// e.g. when deploying to cloudflare workers, you'd want the FunctionArgs to be passed in// to resolve environment variablesexport async function getApp() { return await getBkndApp(config, process.env as any);}export async function getApi( args?: { request: Request }, opts?: { verify?: boolean },) { const app = await getApp(); if (opts?.verify) { const api = app.getApi({ headers: args?.request.headers }); await api.verifyAuth(); return api; } return app.getApi();}
For more information about the connection object, refer to the Database guide.
If you're using @react-router/fs-routes, this file will automatically be picked up as a route.
If you're manually defining routes in app/routes.ts, reference this file in your configuration:
app/routes.ts
import { type RouteConfig, route } from "@react-router/dev/routes";export default [ // your other routes... route("api/*", "./routes/api.$.ts"),] satisfies RouteConfig;
If you're using @react-router/fs-routes, this file will automatically be picked up as a route.
If you're manually defining routes in app/routes.ts, reference this file in your configuration:
app/routes.ts
import { type RouteConfig, route } from "@react-router/dev/routes";export default [ // your other routes... route("admin/*", "./routes/admin.$.tsx"),] satisfies RouteConfig;
If you want to use bknd's client-side React hooks (like useEntityQuery, useAuth, etc.), wrap your app in the ClientProvider component. This is typically done in app/root.tsx:
app/root.tsx
// other importsimport { ClientProvider } from "bknd/client";// ...export default function App() { return ( <ClientProvider> <Outlet /> </ClientProvider> );}// ...
The ClientProvider automatically uses the same origin for API requests, which works perfectly when bknd is served from your React Router app. For more details on using React hooks, see the React SDK documentation.