# /api/instance-context/tenant (dashboard)

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

Source: https://docs.minds.sh/docs/api/platform/reference/dashboard--api-instance-context-tenant



This is the **dashboard** service route. Its origin and authentication are described in [Platform API overview](/docs/api/platform) and [Authentication](/docs/api/platform/authentication). A path shared by another service is a different endpoint.

| Property                       | Contract                                                                       |
| ------------------------------ | ------------------------------------------------------------------------------ |
| Methods                        | POST, GET                                                                      |
| Path                           | `/api/instance-context/tenant`                                                 |
| Path parameters                | None                                                                           |
| Query parameters read in route | None read directly; schema validation below may define additional fields       |
| Headers read in route          | No additional direct header reads; authentication helpers may read credentials |

## Authentication and access [#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 [#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 [#post]

```typescript
export async function POST(request: NextRequest) {
  try {
    const { userId, redirectToSignIn } = await auth();

    if (!userId) {
      return redirectToSignIn();
    }

    const body = await request.json();
    const { tenantId } = body;

    if (!tenantId || typeof tenantId !== 'string') {
      return NextResponse.json(
        { error: { code: 'INVALID_REQUEST', message: 'tenantId is required' } },
        { status: 400 }
      );
    }

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

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

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:13`.

### GET [#get]

```typescript
export async function GET() {
  try {
    const { userId, redirectToSignIn } = await auth();

    if (!userId) {
      return redirectToSignIn();
    }

    const cookieStore = await cookies();
    const tenantId = cookieStore.get(ACTIVE_TENANT_COOKIE)?.value || null;

    return NextResponse.json({ tenantId });
  } catch (error) {
    console.error('Failed to get active tenant:', error);
    return NextResponse.json(
      {
        error: {
          code: 'GET_FAILED',
          message: error instanceof Error ? error.message : 'Failed to get active tenant',
        },
      },
      { status: 500 }
    );
  }
}
```

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:55`.

## Response and error branches [#response-and-error-branches]

Literal HTTP statuses in the route: 400, 500. Shared management error classes are defined in [Errors and responses](/docs/api/platform/errors).

```typescript
request.json()
```

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:21`.

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

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:25`.

```typescript
NextResponse.json({ success: true, tenantId })
```

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:40`.

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

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:43`.

```typescript
NextResponse.json({ tenantId })
```

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:66`.

```typescript
NextResponse.json(
      {
        error: {
          code: 'GET_FAILED',
          message: error instanceof Error ? error.message : 'Failed to get active tenant',
        },
      },
      { status: 500 }
    )
```

Source: `minds-ui/apps/app/app/api/instance-context/tenant/route.ts:69`.

## Service dependencies [#service-dependencies]

* `minds-ui/apps/app/lib/organizations.ts`

## Evidence [#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/instance-context/tenant/route.ts`. SHA-256: `90cce6416c828390c3c715b4c96e51389317d4a62eb0227d7a27fb8d5158dc57`.
