# /api/instance-context/active (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-active



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/active`                                                 |
| 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 { serviceId } = body;

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

    // Set cookie with secure attributes
    const cookieStore = await cookies();
    cookieStore.set(COOKIE_NAME, serviceId, {
      httpOnly: true,
      secure: process.env.NODE_ENV === 'production',
      sameSite: 'lax',
      maxAge: COOKIE_MAX_AGE,
      path: '/',
    });

    return NextResponse.json({ success: true, serviceId });
  } catch (error) {
    console.error('Failed to set active service:', error);

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

Source: `minds-ui/apps/app/app/api/instance-context/active/route.ts:17`.

### GET [#get]

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

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

    const cookieStore = await cookies();
    const activeServiceId = cookieStore.get(COOKIE_NAME)?.value || null;

    return NextResponse.json({ serviceId: activeServiceId });
  } catch (error) {
    console.error('Failed to get active service:', error);

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

Source: `minds-ui/apps/app/app/api/instance-context/active/route.ts:65`.

## 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/active/route.ts:25`.

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

Source: `minds-ui/apps/app/app/api/instance-context/active/route.ts:29`.

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

Source: `minds-ui/apps/app/app/api/instance-context/active/route.ts:45`.

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

Source: `minds-ui/apps/app/app/api/instance-context/active/route.ts:49`.

```typescript
NextResponse.json({ serviceId: activeServiceId })
```

Source: `minds-ui/apps/app/app/api/instance-context/active/route.ts:76`.

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

Source: `minds-ui/apps/app/app/api/instance-context/active/route.ts:80`.

## 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/active/route.ts`. SHA-256: `02a6326563947ed5724911c50d01aab64fa89435ee7d2438552066de8a55f13f`.
