Complete guide to using Dyrected in a Next.js App Router project.
Dyrected integrates natively with the Next.js App Router via a catch-all API route. You can fetch data server-side, client-side, or use the SDK in Server Components.
Easiest path: paste the Dyrected setup prompt into an AI coding tool with access to your repo — it runs the CLI, models your content as collections and globals, and mounts the admin for you.
Setting up manually? The CLI is still the safest way to install — it detects your App Router and optional src/ layout, installs dependencies, writes all required files, and generates instrumentation.ts to log URLs on startup.
npx dyrected init
The steps below cover manual setup and all available options.
@dyrected/next provides the API route handler, getDyrectedClient, and Next.js-optimised <DyrectedImage /> / <DyrectedMedia /> components. @dyrected/react is the React integration layer — it provides <DyrectedAdmin /> , DyrectedProvider, useDyrected, and useLivePreview. @dyrected/next re-exports all of these so you can import
from a single package.
Wrap your next.config.ts with withDyrected to ensure a single React instance is shared between your app and the Dyrected Admin UI bundle. Without this you may see an "Invalid hook call" error when rendering <DyrectedAdmin />.
// next.config.tsimport type { NextConfig } from "next";import { withDyrected } from "@dyrected/next/config";const nextConfig: NextConfig = { // your config here};export default withDyrected(nextConfig);
This applies the correct react and react-dom resolve aliases for both Webpack and Turbopack.
The @dyrected/next package provides a <DyrectedAdmin /> component that handles Next.js router integration, CSS imports, and client-only mounting.
If you used npx dyrected init, this file is already written in the correct location — the CLI detects your optional src/ layout and writes an App Router page.
// app/admin/page.tsx (or src/app/admin/page.tsx)import { DyrectedAdmin } from "@dyrected/next/admin";export default function AdminPage() { return <DyrectedAdmin />;}
@dyrected/next requires the Next.js App Router. Pages Router projects need an app/ directory before running npx dyrected init.
DyrectedAdmin automatically reads your environment variables (like NEXT_PUBLIC_DYRECTED_URL) if they follow the
standard naming convention. You only need to pass props if you want to override the defaults.
When you run npx dyrected init, it generates an instrumentation.ts file at your project root (or src/instrumentation.ts for src/ layouts). This uses Next.js's built-in instrumentation hook to log the admin and API URLs whenever the server starts:
# .env.localDATABASE_URL=postgresql://user:pass@localhost:5432/mydb# Dyrected site URL (SDK methods append /api themselves)NEXT_PUBLIC_DYRECTED_URL=http://localhost:3000# Server-side API key (never expose to browser)DYRECTED_API_KEY=sk_live_...# Client-side keys (safe to expose — access is controlled by Dyrected access functions)NEXT_PUBLIC_DYRECTED_API_KEY=pk_live_...NEXT_PUBLIC_SITE_ID=site_...
Annotate elements with useDyPath so clicking them in the preview jumps to the field. Render block arrays with Blocks — it scopes each block's base path automatically. All three are re-exported from @dyrected/next: