Authentication

The Sharely.ai API uses Bearer token authentication. There are two main authentication flows depending on your use case:

  1. API Key Authentication - For server-to-server integrations and scripts
  2. User Token Authentication - For user-specific operations

API Key Authentication

Obtaining an API Key

  1. Sign in to your Sharely account.
  2. On the sidebar, click Settings.
  3. Click the API Keys tab, and then click the Create key button.
  4. Name your key, and then click Create key.
  5. Store your key in a secure location, as you will not be able to retrieve it from here later.

Make sure you're creating a key for the right workspace. To change workspaces, click Change workspace toward the top of the sidebar.

Your API key will have the format: sk-sharely-...

Two-Step Authentication Flow

For most /v1/* API endpoints, you'll use a two-step authentication process:

Step 1: Generate Access Token

Exchange your API key for a short-lived access token:

POST /workspaces/{workspaceId}/generate-access-key-token

Note that the URL to get an access token does not include v1/. However, the URLs for other API operations do include v1/, such as /v1/workspaces/{workspaceId}/knowledge.

Request:

The following snippets use environment variables. To set them, see the Complete Authentication Example.

SHARELY_ACCESS_TOKEN=$(curl -s -X POST \
  "$SHARELY_API_URL/workspaces/$SHARELY_WORKSPACE_ID/generate-access-key-token" \
  -H 'Content-Type: application/json' \
  -H "x-api-key: $SHARELY_API_KEY" \
  -d '{}' | jq -r '.token')

Response:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Step 2: Use Access Token in API Requests

The following snippets use environment variables. To set them, see the Complete Authentication Example. The snippets use the access token you stored in a variable in Step 1.

Use the generated token in the Authorization header:

curl -X GET \
  "$SHARELY_API_URL/v1/workspaces/$SHARELY_WORKSPACE_ID/knowledge" \
  -H "Authorization: Bearer $SHARELY_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -H "organizationid: $SHARELY_ORGANIZATION_ID"

Note: The organizationid header is required for all /v1/* API requests.

Important: API Key vs Access Token

Your API key (sk-sharely-...) is NOT the same as the Bearer token.

  • API Key: Used only in Step 1 to generate an access token via the x-api-key header
  • Access Token: The JWT returned from Step 1, used as Authorization: Bearer {token} for all /v1/* API calls

All /v1/* endpoints require JWT access tokens. Raw API keys cannot be used directly as Bearer tokens.

Complete Authentication Example

Here's a complete example showing the correct two-step flow.

Many of the code snippets in this documentation use environment variables in place of these values:

  • SHARELY_API_URL for https://api.sharely.ai
  • SHARELY_API_KEY for your API key. To get one, see Obtaining an API Key.
  • SHARELY_WORKSPACE_ID for {workspaceId}
  • SHARELY_ORGANIZATION_ID for your organization ID

To get your workspace ID and organization ID, sign in to your Sharely account. On the sidebar, click Settings. Click the Web control tab, and then scroll down to the Workspace & Organization IDs section.

Set the environment variables:

export SHARELY_API_URL="https://api.sharely.ai"
export SHARELY_API_KEY="sk-sharely-your-api-key"
export SHARELY_WORKSPACE_ID="your-workspace-id"
export SHARELY_ORGANIZATION_ID="your-organization-id"

Then make the requests:

# Step 1: Generate access token using your API key
SHARELY_ACCESS_TOKEN=$(curl -s -X POST \
  "$SHARELY_API_URL/workspaces/$SHARELY_WORKSPACE_ID/generate-access-key-token" \
  -H 'Content-Type: application/json' \
  -H "x-api-key: $SHARELY_API_KEY" \
  -d '{}' | jq -r '.token')
 
# Step 2: Use the access token for API calls
curl -X GET \
  "$SHARELY_API_URL/v1/workspaces/$SHARELY_WORKSPACE_ID/knowledge" \
  -H "Authorization: Bearer $SHARELY_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -H "organizationid: $SHARELY_ORGANIZATION_ID"

Workspace ID

Most API endpoints require a workspaceId parameter in the URL path. To find your workspace ID:

  1. Sign in to your Sharely account.
  2. On the sidebar, click Settings.
  3. Click the Web control tab, and then scroll down to the Workspace & Organization IDs section.

Make sure you're copying the ID for the right workspace. To change workspaces, click Change workspace toward the top of the sidebar.

Example endpoint:

/v1/workspaces/{workspaceId}/knowledge

Replace {workspaceId} with your actual workspace UUID.

API Versioning

All v1 API responses include an X-API-Version header:

X-API-Version: v1

This header helps you identify which version of the API processed your request.

Security Best Practices

Keep Your API Key Secret

  • Never commit API keys to version control
  • Don't expose API keys in client-side code
  • Use environment variables to store keys
  • Rotate keys regularly

Use HTTPS

All API requests must use HTTPS. HTTP requests will be rejected.

Scope Your Keys

Create separate API keys for different applications or environments (development, staging, production) to minimize security risks.

Revoke Compromised Keys

If an API key is exposed or compromised, revoke it immediately from your workspace settings and generate a new one.

Authentication Errors

401 Unauthorized

Your API key is missing, invalid, or expired.

{
  "error": "Unauthorized",
  "message": "Invalid or missing API key"
}

Solution: Check that your Authorization header is correctly formatted and contains a valid API key.

403 Forbidden

Your API key is valid, but you don't have permission to access the requested resource.

{
  "error": "Forbidden",
  "message": "Insufficient permissions for this workspace"
}

Solution: Verify that your API key has the necessary permissions for the workspace you're trying to access.

Testing Your Authentication

You can test your authentication by first generating an access token and then making a simple GET request:

The following snippets use environment variables. To set them, see the Complete Authentication Example.

# Step 1: Generate access token
SHARELY_ACCESS_TOKEN=$(curl -s -X POST \
  "$SHARELY_API_URL/workspaces/$SHARELY_WORKSPACE_ID/generate-access-key-token" \
  -H 'Content-Type: application/json' \
  -H "x-api-key: $SHARELY_API_KEY" \
  -d '{}' | jq -r '.token')
 
# Step 2: Test with a simple API call
curl -X GET \
  "$SHARELY_API_URL/v1/workspaces/$SHARELY_WORKSPACE_ID/knowledge" \
  -H "Authorization: Bearer $SHARELY_ACCESS_TOKEN" \
  -H "organizationid: $SHARELY_ORGANIZATION_ID"

A successful response confirms your API key and authentication flow is working correctly.

User Space Activation API

When integrating Sharely.ai with your application (e.g., using JSWebControl), you may need to activate or retrieve a user space with specific role-based access control (RBAC) settings.

Activate or Retrieve User Space

This endpoint creates or retrieves a user space with optional role assignment:

POST /workspaces/{workspaceId}/activate-or-retrieve-user-space-api-key

Request:

The following snippets use environment variables. To set them, see the Complete Authentication Example.

curl -X POST \
  "$SHARELY_API_URL/workspaces/$SHARELY_WORKSPACE_ID/activate-or-retrieve-user-space-api-key" \
  -H 'Content-Type: application/json' \
  -H "x-api-key: $SHARELY_API_KEY" \
  -d '{
    "userId": "user-123",
    "email": "user@example.com",
    "customerRoleId": "physician"
  }'

Request Parameters:

  • userId (required): Unique identifier for the user
  • email (required): User's email address
  • customerRoleId (optional): External role identifier to assign to the user

Response:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "user-123",
    "email": "user@example.com"
  },
  "space": {
    "id": "space-uuid",
    "workspaceId": "workspace-uuid"
  }
}

The returned token includes user_metadata.roleId which is automatically mapped from the customerRoleId you provided. This token can be used to initialize JSWebControl with proper RBAC filtering.

Use Case: This is particularly useful for applications like PrecisionCare that need to:

  1. Create or retrieve a space for a specific user
  2. Assign a role (e.g., "physician", "nurse") to that user
  3. Initialize JSWebControl with a token that includes the role information

Next Steps

Once you have your API key configured, you can start making API calls: