Skip to content

Quickstart: Next.js SDK

  • Node.js 22+
  • A Rakomi account with an API key (starts with akm_live_ or akm_test_)
  1. Install the SDK

    Terminal window
    pnpm add @rakomi/node
  2. Initialize the client

    import { Rakomi } from '@rakomi/node';
    const ca = new RakomiClient({
    apiKey: 'akm_test_xxx', // Start with test key — switch to akm_live_ for production
    });
  3. Protect your routes with middleware

    Create middleware.ts in your Next.js project root:

    import { NextResponse, type NextRequest } from 'next/server';
    import { Rakomi } from '@rakomi/node';
    const ca = new RakomiClient({ apiKey: 'akm_live_xxx' });
    export async function middleware(request: NextRequest) {
    const token = request.headers.get('authorization')?.slice(7);
    if (!token) {
    return NextResponse.json({ error: 'Missing token' }, { status: 401 });
    }
    const result = await ca.verifyToken(token);
    if (!result.ok) {
    const { code, message, docs_url } = result.error;
    return NextResponse.json(
    { error: { code, message, docs_url } },
    { status: 401 },
    );
    }
    const response = NextResponse.next();
    response.headers.set('x-user-id', result.data.userId);
    response.headers.set('x-tenant-id', result.data.tenantId);
    return response;
    }
    export const config = {
    matcher: ['/api/:path*', '/dashboard/:path*'],
    };
  4. Verify a token manually (optional)

    const result = await ca.verifyToken(token);
    if (result.ok) {
    console.log('User:', result.data.userId);
    console.log('Email:', result.data.email);
    console.log('Tenant:', result.data.tenantId);
    } else {
    console.error('Error:', result.error.code, result.error.suggestion);
    }