API referencePlatform APIEndpoint reference
GUIDE & REFERENCE

/api/organizations/active (dashboard)

Source-derived dashboard endpoint contract, authentication, request validation, responses, and errors.

This is the dashboard service route. Its origin and authentication are described in Platform API overview and Authentication. A path shared by another service is a different endpoint.

PropertyContract
MethodsPOST
Path/api/organizations/active
Path parametersNone
Query parameters read in routeNone read directly; schema validation below may define additional fields
Headers read in routeNo additional direct header reads; authentication helpers may read credentials

Authentication and access

Dashboard session cookie through @repo/auth/server. Some auth-summary routes can redirect to sign-in; inspect the status branches below.

Method behavior

The handler excerpt preserves field validation, response envelopes, cookie changes, and exception branches. Values returned by service helpers retain their named response type above; for passthrough routes, the upstream response is authoritative.

POST

export async function POST(request: NextRequest) {
  try {
    const session = await getSession();
    if (!session) {
      return NextResponse.json(
        { error: { code: 'UNAUTHENTICATED', message: 'Sign in at auth.l1fe.ai.' } },
        { status: 401 }
      );
    }

    const body = await request.json();
    const organizationId = typeof body.organizationId === 'string' ? body.organizationId : null;
    if (!organizationId) {
      return NextResponse.json(
        { error: { code: 'INVALID_REQUEST', message: 'organizationId is required' } },
        { status: 400 }
      );
    }

    const organizations = await listMindsOrganizations({
      accessToken: session.accessToken,
      includeEntitlements: false,
    });
    const organization = organizations.find((item) => item.id === organizationId);
    if (!organization) {
      return NextResponse.json(
        { error: { code: 'ORG_NOT_AVAILABLE', message: 'Organization is not available to Minds.' } },
        { status: 404 }
      );
    }

    const cookieStore = await cookies();
    cookieStore.set(ACTIVE_TENANT_COOKIE, organization.id, {
      httpOnly: true,
      secure: process.env.NODE_ENV === 'production',
      sameSite: 'lax',
      maxAge: COOKIE_MAX_AGE,
      path: '/',
    });

    return NextResponse.json({ success: true, organizationId: organization.id });
  } catch (error) {
    console.error('Failed to set active organization:', error);
    return NextResponse.json(
      {
        error: {
          code: 'SET_FAILED',
          message: error instanceof Error ? error.message : 'Failed to set active organization',
        },
      },
      { status: 500 }
    );
  }
}

Source: minds-ui/apps/app/app/api/organizations/active/route.ts:12.

Response and error branches

Literal HTTP statuses in the route: 400, 401, 404, 500. Shared management error classes are defined in Errors and responses.

NextResponse.json(
        { error: { code: 'UNAUTHENTICATED', message: 'Sign in at auth.l1fe.ai.' } },
        { status: 401 }
      )

Source: minds-ui/apps/app/app/api/organizations/active/route.ts:16.

request.json()

Source: minds-ui/apps/app/app/api/organizations/active/route.ts:22.

NextResponse.json(
        { error: { code: 'INVALID_REQUEST', message: 'organizationId is required' } },
        { status: 400 }
      )

Source: minds-ui/apps/app/app/api/organizations/active/route.ts:25.

NextResponse.json(
        { error: { code: 'ORG_NOT_AVAILABLE', message: 'Organization is not available to Minds.' } },
        { status: 404 }
      )

Source: minds-ui/apps/app/app/api/organizations/active/route.ts:37.

NextResponse.json({ success: true, organizationId: organization.id })

Source: minds-ui/apps/app/app/api/organizations/active/route.ts:52.

NextResponse.json(
      {
        error: {
          code: 'SET_FAILED',
          message: error instanceof Error ? error.message : 'Failed to set active organization',
        },
      },
      { status: 500 }
    )

Source: minds-ui/apps/app/app/api/organizations/active/route.ts:55.

Service dependencies

  • minds-ui/apps/app/lib/organizations.ts

Evidence

Generated from the local source snapshot on 2026-09-12. This page documents implemented code and does not certify a deployed service, permissions configuration, or successful provider operation.

Source file: minds-ui/apps/app/app/api/organizations/active/route.ts. SHA-256: 46eac39878b990bd2383b2f86b1375e225892f8a9c89501ad0744e571286af0a.

On this page