btst-integration
Guides developers and AI agents through manual BTST library consumption in existing apps, including plugin registration, CSS imports, adapter setup, StackProvider wiring, React Query, and CLI schema workflows. Use when integrating `@btst/stack`, configuring `@btst/adapter-*`, wiring plugin `api/client` packages, setting up `StackProvider` overrides, adding `/api/data` and `/pages` routes, or running `@btst/cli generate|migrate` without scaffolding.
How do I install this agent skill?
npx skills add https://github.com/better-stack-ai/better-stack --skill btst-integrationIs this agent skill safe to install?
- Gen Agent Trust Hubpass
This skill provides standard integration instructions and code templates for the BTST library. It uses industry-standard patterns for environment variables and package management without security risks.
- Socketpass
No alerts
- Snykpass
Risk: LOW · No issues
- ZeroLeakspass
Score: 93/100 · 2 sections analyzed
What does this agent skill do?
BTST Library Integration
Quick start
- Install
@btst/stack,@tanstack/react-query, and one@btst/adapter-*. - Install each selected @btst/plugin-{name} package and register its applicable backend/client halves. Required package-code dependencies and runtime registrations are separate; follow each plugin’s documented graph.
- Create
lib/stack.ts→ export{ handler, dbSchema }. - Mount
handlerwith the framework API entry factory at/api/data/*. - Add
@import "@btst/plugin-{plugin}/css"per plugin in your global CSS. - Create
lib/stack-client.tsx,lib/query-client.ts, and the/pages/*catch-all route with the framework page entry factory. - Create the pages layout file with
QueryClientProvider+StackProvider. - Generate a candidate schema, preserve application-owned models/indexes, and apply reviewed migrations before database-backed builds (and before traffic).
See REFERENCE.md for full code shapes for every file.
Workflow
1) Install prerequisites and packages
- Prereqs: shadcn/ui (CSS variables enabled), Sonner
<Toaster />, Tailwind v4. - Install:
@btst/stack,@tanstack/react-query, and one adapter:@btst/adapter-prisma/@btst/adapter-drizzle/@btst/adapter-kysely/@btst/adapter-mongodb@btst/adapter-memory— dev/testing only, not for production
- Install each plugin package alongside
@btst/stack(they ship inside the monorepo package).
2) Register plugins (backend + client)
- Backend (
lib/stack.ts): import{plugin}BackendPluginfrom@btst/plugin-{plugin}/api.- Pass hooks and config in the backend plugin options object (e.g.
blogBackendPlugin({ hooks })). - Use camelCase keys for plugins that have compound names:
aiChat,formBuilder,uiBuilder.
- Pass hooks and config in the backend plugin options object (e.g.
- Client (
lib/stack-client.tsx): import{plugin}ClientPluginfrom@btst/plugin-{plugin}/client.- Client definitions receive only plugin-specific options such as
seo, hooks, or page components. Configureapi,site, andqueryClientonce oncreateClientStack(); its resolver binds that runtime to every definition. - Pass incoming SSR request headers through the top-level
createClientStack({ api: { headers } })configuration. - Resolve one trusted API/site origin snapshot on the server and hydrate it into the provider. A managed backend may use a distinct
BTST_API_URL; never reconstruct it fromwindow.locationafter hydration.
- Client definitions receive only plugin-specific options such as
Embedded Search
For Search dialogs on pages with their own query filters, scope StackProvider
to the embedded UI with a navigation-only router: omit both getSearchParams
and setSearchParams, including bindings returned by a framework useRouter.
Keep the results page under the normal URL-aware provider. Reuse the resolved
stack, QueryClient, and enclosing identity boundary.
Use SearchInput/SearchDialog renderResult and renderLoading for visual
customization; route equivalents live in overrides.search. renderResult(item, query) can reuse the exported SearchHighlight for literal phrase matches. Return
presentational content so Search retains options/links, announcements, keyboard
selection, pagination, retry, and fixed sources. See
search.mdx
for the complete provider example.
For manual Next.js results pages, createNextPage.getStackClient receives page
props as its second argument. Await props.searchParams, convert them to
URLSearchParams, and forward them through the server helper to
searchClientPlugin({ searchParams, identityPartition }). Otherwise SSR loads an
empty query even when the browser URL contains one. See the
SSR example.
3) Configure backend stack
- Call
createBackendStack({ basePath: "/api/data", plugins: { ... }, adapter: (db) => createXxxAdapter(..., db, {}), auth: serverAuth })when application authorization is enabled. - Create
serverAuthwithcreateServerAuth({ authorization, getIdentity }). Omittingauthintentionally preserves permissive no-authorization compatibility; plugin labels such as AI Chat'saccess: "authorized"are not an enforcement boundary by themselves. - Export
handleranddbSchema. - Memory adapter + Next.js: pin to
globalThisto avoid two instances in the same process:function createAppStack() { return createBackendStack({ ... }) } type AppStack = ReturnType<typeof createAppStack> const g = globalThis as typeof globalThis & { __btst__?: AppStack } export const myStack = g.__btst__ ??= createAppStack() export const { handler, dbSchema } = myStack
4) CSS — edit global stylesheet
Add one line per plugin before your Tailwind layers:
@import "@btst/plugin-blog/css";
@import "@btst/plugin-cms/css";
@import "@btst/plugin-ai-chat/css";
/* …one per selected plugin */
Do not duplicate — the patcher and manual edits must both be idempotent.
5) Wire framework routes and client runtime
- API route: use
toNextRouteHandlers,toReactRouterHandlers, ortoTanStackHandlersfrom the framework entry point. - Pages route: use
createNextPage,createReactRouterPage, orcreateTanStackPageOptions. The factory owns route matching, loader ordering, hydration, metadata, and 404 handling. - Pages layout (
"use client"in Next.js): wrap inQueryClientProviderthenStackProvider:- Pass the resolved client stack with
stack={clientStack}. Its top-levelsite.basePathmust match your pages catch-all prefix. router={nextRouter()}/reactRouter()/tanstackRouter()for framework-wide links, images, navigation, and refresh.auth={createClientAuth({ authorization, getIdentity, loginPath })}when plugins need identity or permission presentation gates.overrides={{ pluginKey: { uploadImage?, ...pluginSpecificValues } }}only for plugin-specific customization.- Let the resolved plugin map infer override keys and values; do not duplicate a manual provider map.
- See REFERENCE.md for provider wiring and per-plugin override shapes.
- Pass the resolved client stack with
- Page context: wrap the root layout (above
StackProvider) withPageAIContextProvider:
Pages then callimport { PageAIContextProvider } from "@btst/stack/page-context" // root layout.tsx (not the pages layout) export default function RootLayout({ children }) { return <PageAIContextProvider>{children}</PageAIContextProvider> }useRegisterPageAIContext(...)from the lightweight Core entry, including pages without AI Chat installed — see thebtst-ai-contextskill.
6) Generate schemas and run migrations
- Use the pinned low-level CLI in isolation:
pnpm dlx @btst/cli@3.0.1. - Existing Prisma apps: generate to a scratch file with
pnpm dlx @btst/cli@3.0.1 generate --config=lib/stack.ts --orm=prisma --output=prisma/btst-candidate.prisma. Keep it outside a configured multi-file schema directory. Merge BTST additions into the application schema, retaining custom models, indexes, mappings, and relations; validate with Prisma and review additive migration SQL. Do not overwrite the application schema or accept a reset to resolve drift. - Drizzle:
pnpm dlx @btst/cli@3.0.1 generate --config=lib/stack.ts --orm=drizzle --output=src/db/schema.ts - Kysely:
pnpm dlx @btst/cli@3.0.1 generate --config=lib/stack.ts --orm=kysely --output=migrations/schema.sql --database-url=... - Kysely migrate:
pnpm dlx @btst/cli@3.0.1 migrate --config=lib/stack.ts --database-url=... - Prisma/Drizzle: use native migration tooling after generate.
- Package extraction alone preserves tables; later Blog/CMS revision and editorial features require schema additions. Apply committed migrations to the database used by
next buildbefore static params, metadata, or prefetching.prisma generatecreates client code, not tables. Isolated CI migrations do not prepare preview/production. For shared preview/production databases, coordinate one backward-compatible migration through the release job; do not auto-migrate production from every preview build. - See existing-app workflow for environment ordering, trusted CMS slug reads and full relationship pagination, Search SSR parameters, static Blog checks, and strict publisher snapshots. Preserve current constructors/provider wiring; a stale consumer copy should be refreshed rather than used to rewrite upstream examples.
Validation checklist
stack.tsexports bothhandleranddbSchema.- Protected applications pass
createServerAuth(...)tocreateBackendStack({ auth }); omitted auth is intentionally permissive. - Every plugin is registered on both backend and client sides.
- API
basePathandcreateBackendStack({ basePath })match exactly. - API and page catch-all routes use the framework entry factories.
- Pages layout is
"use client"and wrapsQueryClientProviderthenStackProvider. - The resolved client stack's
site.basePathmatches the/pagescatch-all route prefix. - Global CSS has one
@importline per selected plugin. /pages/*routes render expected plugin pages.- CLI commands run with required env vars in scope.
Gotchas
"use client"missing on pages layout —StackProvideruses hooks and must be client-side.- Wrong key casing — backend/client plugin map keys must match; compound names use camelCase (
aiChat, notai-chat). - Missing CSS import — plugin UI breaks silently even when data loads correctly.
- Half-registered plugin — backend-only = routes don't exist; client-only = 404 on data calls.
- Memory adapter + Next.js — always pin to
globalThisto share one in-memory store across API and page bundles. - Path aliases in CLI —
@btst/cliexecutes your config file directly; use relative imports inlib/stack.tsand its dependencies. - Kysely generate needs DB — pass
DATABASE_URLor--database-url; usedotenv-clifor.env.local. - Provider services copied into plugin overrides — never add
Link,Image, navigation, refresh, API paths, identity, or login values to built-in plugin overrides. Use top-levelrouter,api, andauth.
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/better-stack-ai/better-stack/btst-integration">View btst-integration on skillZs</a>