Knowledge Statistics

Get aggregated statistics about knowledge items in your workspace, including counts by status and role/permission assignments.

Endpoint

GET /v1/workspaces/{workspaceId}/knowledge/stats

Authentication

This endpoint requires a Bearer token obtained through the two-step authentication flow:

  1. First, generate an access token using your API key
  2. Use that access token in the Authorization header

See Authentication for the complete authentication flow.

Path Parameters

ParameterTypeRequiredDescription
workspaceIdstring (UUID)YesThe ID of your workspace

Headers

HeaderTypeRequiredDescription
AuthorizationstringYesBearer token with access token
organizationidstring (UUID)YesYour organization ID

Response

Success Response

Status Code: 200 OK

Body:

{
  "total": 1523,
  "byStatus": {
    "COMPLETED": 1450,
    "BACKGROUND_START": 50,
    "BACKGROUND_ERROR": 23
  },
  "withRoles": 800,
  "withoutRoles": 723,
  "withPermissions": 650,
  "withoutPermissions": 873
}

Response Fields

FieldTypeDescription
totalintegerTotal number of knowledge items in the workspace
byStatusobjectBreakdown of knowledge items by processing status
byStatus.COMPLETEDintegerSuccessfully processed knowledge items
byStatus.BACKGROUND_STARTintegerKnowledge items currently being processed
byStatus.BACKGROUND_ERRORintegerKnowledge items that failed processing
withRolesintegerKnowledge items assigned to at least one role
withoutRolesintegerKnowledge items not assigned to any role
withPermissionsintegerKnowledge items with custom permissions
withoutPermissionsintegerKnowledge items without custom permissions

Examples

Basic Request

# Step 1: Generate access token (do this once, reuse for multiple requests)
ACCESS_TOKEN=$(curl -s -X POST \
  'https://api.sharely.ai/workspaces/your-workspace-id/generate-access-key-token' \
  -H 'Content-Type: application/json' \
  -H 'x-api-key: sk-sharely-your-api-key' \
  -d '{}' | jq -r '.token')
 
# Step 2: Get knowledge statistics
curl -X GET \
  'https://api.sharely.ai/v1/workspaces/your-workspace-id/knowledge/stats' \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H 'organizationid: your-organization-id'

JavaScript Example

const API_KEY = 'sk-sharely-your-api-key';
const WORKSPACE_ID = 'your-workspace-id';
const ORGANIZATION_ID = 'your-organization-id';
const API_BASE_URL = 'https://api.sharely.ai';
 
// Step 1: Generate access token
async function getAccessToken() {
  const response = await fetch(
    `${API_BASE_URL}/workspaces/${WORKSPACE_ID}/generate-access-key-token`,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': API_KEY
      },
      body: JSON.stringify({})
    }
  );
  const data = await response.json();
  return data.token;
}
 
// Step 2: Get knowledge statistics
async function getKnowledgeStats(accessToken) {
  const response = await fetch(
    `${API_BASE_URL}/v1/workspaces/${WORKSPACE_ID}/knowledge/stats`,
    {
      headers: {
        'Authorization': `Bearer ${accessToken}`,
        'organizationid': ORGANIZATION_ID
      }
    }
  );
 
  if (!response.ok) {
    throw new Error(`Failed to get stats: ${response.statusText}`);
  }
 
  return await response.json();
}
 
// Usage
const accessToken = await getAccessToken();
const stats = await getKnowledgeStats(accessToken);
 
console.log(`Total knowledge items: ${stats.total}`);
console.log(`Completed: ${stats.byStatus.COMPLETED}`);
console.log(`Processing: ${stats.byStatus.BACKGROUND_START}`);
console.log(`Failed: ${stats.byStatus.BACKGROUND_ERROR}`);
console.log(`With RBAC roles: ${stats.withRoles}`);

Use Cases

Monitor Processing Status

async function checkProcessingStatus(accessToken) {
  const stats = await getKnowledgeStats(accessToken);
 
  const processingRate = (stats.byStatus.COMPLETED / stats.total * 100).toFixed(1);
  const errorRate = (stats.byStatus.BACKGROUND_ERROR / stats.total * 100).toFixed(1);
 
  console.log(`Processing complete: ${processingRate}%`);
  console.log(`Error rate: ${errorRate}%`);
 
  if (stats.byStatus.BACKGROUND_START > 0) {
    console.log(`Still processing: ${stats.byStatus.BACKGROUND_START} items`);
  }
 
  return stats;
}

RBAC Coverage Report

async function rbacCoverageReport(accessToken) {
  const stats = await getKnowledgeStats(accessToken);
 
  const rolesCoverage = (stats.withRoles / stats.total * 100).toFixed(1);
  const permissionsCoverage = (stats.withPermissions / stats.total * 100).toFixed(1);
 
  return {
    total: stats.total,
    rolesCoverage: `${rolesCoverage}%`,
    permissionsCoverage: `${permissionsCoverage}%`,
    unassignedToRoles: stats.withoutRoles,
    unassignedPermissions: stats.withoutPermissions
  };
}

Error Responses

401 Unauthorized

{
  "error": "Unauthorized",
  "message": "Invalid or missing access token"
}

404 Not Found

{
  "error": "Not Found",
  "message": "Workspace not found"
}

Related Endpoints