API referencePlatform APIEndpoint reference
GUIDE & REFERENCE

/api/v1/tenants/[id]/tokens (management)

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

This is the management 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/v1/tenants/[id]/tokens
Path parametersid
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

Management auth context: validated x-api-key or configured session authentication; trusted gateway integration may select its auth pipeline. Organization/tenant checks are separate.

Request and response definitions

The following named definitions are imported or declared by this route. Optional markers, defaults, bounds, and enum values are shown exactly as implemented. A response type is a source contract, not an example populated with real account data.

GenerateTokenRequest

GenerateTokenRequest = z.object({
  name: z.string().max(100).optional(),
  expires_in: z.number().int().min(60).max(3600).optional(), // 1 min to 1 hour
  permissions: z.array(PermissionSchema).optional(),
})

Source: minds-ui/apps/api-service/types/auth.ts:440.

GenerateTokenRequest

export type GenerateTokenRequest = z.infer<typeof GenerateTokenRequest>

Source: minds-ui/apps/api-service/types/auth.ts:446.

GenerateTokenResponse

GenerateTokenResponse = z.object({
  token: z.string(), // JWT
  expires_at: z.string().datetime(),
  permissions: z.array(PermissionSchema),
})

Source: minds-ui/apps/api-service/types/auth.ts:452.

GenerateTokenResponse

export type GenerateTokenResponse = z.infer<typeof GenerateTokenResponse>

Source: minds-ui/apps/api-service/types/auth.ts:458.

RouteContext

interface RouteContext {
  params: Promise<{ id: string }>
}

Source: minds-ui/apps/api-service/app/api/v1/tenants/[id]/tokens/route.ts:19.

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, context: RouteContext) {
  const requestId = generateRequestId()
  const { id } = await context.params
  let rateLimitInfo: RateLimitMetadata | undefined

  try {
    const tenantId = validateTenantId(id)
    const authContext = await resolveAuthContext(request)
    const payload = await validateBody(request, GenerateTokenRequest)

    const evaluation = await enforceRateLimit({
      context: authContext,
      tenantId,
    })

    rateLimitInfo = {
      ...evaluation.metadata,
      retryAfter: evaluation.retryAfter,
    }

    if (evaluation.blocked) {
      throw new RateLimitExceededError(evaluation.retryAfter ?? 60)
    }

    const token = await issueTenantToken(authContext, tenantId, payload)
    const response = apiSuccess<GenerateTokenResponse>(token)
    return withRequestMetadata(response, requestId, rateLimitInfo)
  } catch (error) {
    if (error instanceof ApiError) {
      const response = apiError(error, requestId)
      return withRequestMetadata(response, requestId, rateLimitInfo)
    }
    console.error('Failed to generate tenant token:', error)
    const response = apiError(
      new ApiError(500, 'INTERNAL_ERROR', 'Failed to generate tenant token'),
      requestId,
    )
    return withRequestMetadata(response, requestId, rateLimitInfo)
  }
}

Source: minds-ui/apps/api-service/app/api/v1/tenants/[id]/tokens/route.ts:23.

Response and error branches

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

new RateLimitExceededError(evaluation.retryAfter ?? 60)

Source: minds-ui/apps/api-service/app/api/v1/tenants/[id]/tokens/route.ts:44.

apiSuccess<GenerateTokenResponse>(token)

Source: minds-ui/apps/api-service/app/api/v1/tenants/[id]/tokens/route.ts:48.

apiError(error, requestId)

Source: minds-ui/apps/api-service/app/api/v1/tenants/[id]/tokens/route.ts:52.

apiError(
      new ApiError(500, 'INTERNAL_ERROR', 'Failed to generate tenant token'),
      requestId,
    )

Source: minds-ui/apps/api-service/app/api/v1/tenants/[id]/tokens/route.ts:56.

new ApiError(500, 'INTERNAL_ERROR', 'Failed to generate tenant token')

Source: minds-ui/apps/api-service/app/api/v1/tenants/[id]/tokens/route.ts:57.

Service dependencies

  • minds-ui/apps/api-service/lib/api-response.ts
  • minds-ui/apps/api-service/lib/validation.ts
  • minds-ui/apps/api-service/types/auth.ts
  • minds-ui/apps/api-service/types/errors.ts
  • minds-ui/apps/api-service/lib/services/auth-context.ts
  • minds-ui/apps/api-service/lib/services/token-service.ts
  • minds-ui/apps/api-service/lib/services/rate-limit.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/api-service/app/api/v1/tenants/[id]/tokens/route.ts. SHA-256: 33321de812002743933f127d9b6c01d1245e4ed2c5b525db38cfde98f747ba0f.

On this page