Skip to main content
GET
/
api
/
organization
/
context
Organization Context
curl --request GET \
  --url https://api.example.com/api/organization/context
{
  "success": true,
  "data": {
    "id": "<string>",
    "name": "<string>",
    "slug": "<string>",
    "isPlatformAdminView": true
  },
  "isPlatformAdmin": true,
  "message": "<string>"
}

Organization Context

Retrieve the active organization context for the authenticated user. In Hiro CRM’s single-tenant architecture, this returns the user’s assigned organization.

Endpoint

GET /api/organization/context

Authentication

Requires authenticated user session.

Query Parameters

orgId
string
Organization ID override (ignored in single-tenant mode)
The orgId parameter is reserved for future multi-tenant features. Currently, it’s ignored and the user’s assigned organization is always returned.

Response

success
boolean
Whether the request was successful
data
object
Organization data (null if no active organization)
isPlatformAdmin
boolean
Whether the user is a platform admin (super_admin role)
message
string
Status message (present when no organization found)

Example Request

curl -X GET https://your-domain.com/api/organization/context \
  -H "Cookie: your-session-cookie"

Example Response (Success)

{
  "success": true,
  "data": {
    "id": "org-uuid-123",
    "name": "La Tasca Group",
    "slug": "la-tasca-group",
    "isPlatformAdminView": false
  },
  "isPlatformAdmin": false
}

Example Response (No Organization)

{
  "success": true,
  "data": null,
  "isPlatformAdmin": false,
  "message": "No hay organización activa"
}

Error Response

{
  "success": false,
  "error": "Error al obtener contexto de organización"
}

Implementation Details

The endpoint uses the getActiveOrganization() helper:
app/api/organization/context/route.ts
import { getActiveOrganization } from "@/lib/organization-context";

export async function GET(request: NextRequest) {
    try {
        const activeOrg = await getActiveOrganization();

        if (!activeOrg) {
            return NextResponse.json({ 
                success: true, 
                data: null,
                isPlatformAdmin: false,
                message: "No hay organización activa"
            });
        }

        return NextResponse.json({
            success: true,
            data: activeOrg,
            isPlatformAdmin: activeOrg.isPlatformAdminView
        });
    } catch (error: any) {
        logger.error("Error getting organization context", "OrganizationAPI", error);
        return NextResponse.json(
            { success: false, error: error.message },
            { status: 500 }
        );
    }
}

Organization Resolution

The getActiveOrganization() function:
  1. Gets authenticated user from session
  2. Fetches user’s profile with organization_id
  3. Retrieves organization details from organizations table
  4. Returns organization object with metadata

Platform Admin Detection

Platform admins are identified by role:
const isPlatformAdmin = role === 'super_admin';
  • super_admin role = Platform admin
  • All other roles = Regular users

Single-Tenant Architecture

Hiro CRM uses a single-tenant per deployment model:
  • Each user belongs to one organization
  • Organization switching is not supported
  • orgId query parameter is ignored
  • isPlatformAdminView is always false
For multi-tenant deployments, extend getActiveOrganization() to support organization switching via localStorage or query parameters.

Use Cases

  • Display current organization in UI header
  • Scope API requests to user’s organization
  • Validate user has access to features
  • Show organization-specific branding
  • Redirect users without organization to setup flow

Client-Side Usage

const response = await fetch('/api/organization/context');
const { success, data } = await response.json();

if (success && data) {
  console.log(`Active organization: ${data.name}`);
  // Store in context/state management
} else {
  // Redirect to organization setup
}

Logging

Errors are logged using the Logger service:
logger.error("Error getting organization context", "OrganizationAPI", error);
This endpoint is typically called once during app initialization to establish organization context for the session.

Build docs developers (and LLMs) love