Trimble Connect Workspace API

0.3.34 · active · verified Tue Apr 21

The Trimble Connect Workspace API facilitates interaction between client applications and the Trimble Connect Web application, including the 3D Viewer. It primarily uses `window.postMessage()` for secure communication, allowing for two main integration patterns: embedding Trimble Connect Web Modules or the 3D Viewer within an `iframe` in a client application, or embedding the client application itself as an extension within Trimble Connect Web or the 3D Viewer. This library, currently at version 0.3.34, is actively maintained, with release notes and updates frequently published by Trimble. Its key differentiator is providing a standardized, type-safe interface for programmatic control and data exchange within the Trimble Connect ecosystem, abstracting the complexities of cross-origin `postMessage` communication.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to the Trimble Connect Workspace API as an extension, retrieving project details, setting a UI status message, and dynamically updating the extension's menu. It includes basic error handling and event listening.

import * as WorkspaceAPI from 'trimble-connect-workspace-api';

interface ProjectInfo {
  projectId: string;
  projectName: string;
}

// This example assumes the client application is embedded as an extension
// within the Trimble Connect environment, communicating with its parent window.
// If embedding the Trimble Connect Viewer/Modules, `window.parent` would be replaced
// with the `contentWindow` of the iframe.

async function initializeTrimbleConnectExtension() {
  try {
    console.log('Attempting to connect to Trimble Connect Workspace API...');
    const api = await WorkspaceAPI.connect(
      window.parent, // Target origin is handled internally; window.parent is typically used for extensions
      (eventName: string, data: any) => {
        console.log(`Received event: ${eventName}`, data);
        // Handle various events from the Trimble Connect environment
        if (eventName === 'extension.accessToken') {
          console.log('Access token received:', data.data); // data.data contains the actual token
        }
      },
      30000 // Connection timeout in milliseconds
    );

    console.log('Successfully connected to Workspace API.');

    // Example: Get current project information
    const project: ProjectInfo = await api.project.getProject();
    console.log('Current project:', project.projectName, `(ID: ${project.projectId})`);

    // Example: Set a status message in the Trimble Connect UI
    api.extension.setStatusMessage(`Extension loaded for project: ${project.projectName}`);

    // Example: Update the UI menu (assuming this is an extension)
    api.ui.setMenu({
      id: 'my-extension-menu',
      title: 'My Custom Extension',
      icon: 'https://example.com/menu-icon.png',
      command: 'open-my-extension',
      children: [
        { id: 'sub-menu-1', title: 'Action 1', command: 'action-1-clicked' },
        { id: 'sub-menu-2', title: 'Action 2', command: 'action-2-clicked' }
      ]
    });

  } catch (error) {
    console.error('Failed to connect or use Workspace API:', error);
    if (error instanceof Error && error.message.includes('timeout')) {
        console.error('Connection timed out. Ensure the host application is ready and accessible.');
    }
  }
}

initializeTrimbleConnectExtension();

view raw JSON →