My App
BoardKitExamples

NestJS API Example

Complete NestJS backend with Prisma, authentication, and BoardKit module

A complete NestJS application with BoardKit, Prisma storage, JWT authentication, and all endpoints.

Project Structure

src/
├── app.module.ts
├── main.ts
├── prisma.service.ts
├── auth/
│   └── jwt-auth.guard.ts
└── assets/
    └── local-asset-storage.ts

app.module.ts

import { Module } from '@nestjs/common';
import { BoardModule, InMemoryEventLogStorage } from '@hfu.digital/boardkit-nestjs';
import { PrismaBoardAdapter } from '@hfu.digital/boardkit-nestjs';
import { PrismaService } from './prisma.service';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { LocalAssetStorage } from './assets/local-asset-storage';

@Module({
    imports: [
        BoardModule.register({
            storage: new PrismaBoardAdapter({
                board: new PrismaService().board,
                page: new PrismaService().page,
                element: new PrismaService().element,
                boardMember: new PrismaService().boardMember,
                shareLink: new PrismaService().shareLink,
            }),
            assetStorage: new LocalAssetStorage('./uploads'),
            eventLogStorage: new InMemoryEventLogStorage(),
            authGuard: new JwtAuthGuard(process.env.JWT_SECRET!),
            limits: {
                maxElementsPerPage: 5000,
                maxAssetSizeMb: 10,
            },
        }),
    ],
})
export class AppModule {}

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.enableCors();
    await app.listen(3000);
    console.log('BoardKit API running on http://localhost:3000');
}
bootstrap();

prisma.service.ts

import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
    async onModuleInit() {
        await this.$connect();
    }

    async onModuleDestroy() {
        await this.$disconnect();
    }
}

auth/jwt-auth.guard.ts

import { BoardAuthGuard, type AuthenticatedUser } from '@hfu.digital/boardkit-nestjs';
import { verify } from 'jsonwebtoken';

export class JwtAuthGuard extends BoardAuthGuard {
    constructor(private readonly secret: string) {
        super();
    }

    async validateConnection(token: string): Promise<AuthenticatedUser | null> {
        return this.validate(token);
    }

    async validateRequest(token: string): Promise<AuthenticatedUser | null> {
        return this.validate(token);
    }

    private validate(token: string): AuthenticatedUser | null {
        try {
            const payload = verify(token, this.secret) as { sub: string; name: string };
            return { userId: payload.sub, displayName: payload.name };
        } catch {
            return null;
        }
    }
}

API Endpoints (auto-registered)

Once BoardModule.register() is called, these endpoints are available:

POST   /boards                              Create board
GET    /boards                              List boards
GET    /boards/:id                          Get board
PATCH  /boards/:id                          Update board
DELETE /boards/:id                          Delete board
POST   /boards/:id/archive                  Archive board

POST   /boards/:boardId/pages               Add page
GET    /boards/:boardId/pages               List pages
GET    /boards/:boardId/pages/:pageId/elements  Get elements
PUT    /boards/:boardId/pages/reorder       Reorder pages
DELETE /boards/:boardId/pages/:pageId       Delete page

POST   /boards/:boardId/assets              Upload asset
GET    /boards/:boardId/assets/:key         Get asset URL
DELETE /boards/:boardId/assets/:key         Delete asset
GET    /boards/:boardId/assets/usage        Get usage

POST   /boards/:boardId/export              Export board

POST   /boards/:boardId/shares              Create share link
GET    /shares/:token                       Resolve share link
DELETE /boards/:boardId/shares/:id          Delete share link

WebSocket: ws://localhost:3000/board

On this page