Skip to content

Events

Action Types

TypeDescriptionData
page_loadedPage finished loading{ path }
document_createdDocument was created{ id, documentType }
document_updatedDocument was updated{ id, documentType }
document_deletedDocument was deleted{ id, documentType }
customer_createdCustomer was created{ id }
customer_updatedCustomer was updated{ id }
customer_deletedCustomer was deleted{ id }
navigationUser navigated{ path }
close_requestedUser wants to close

Handling Actions

Action Handlertypescript
const _si = new SpaceInvoices({
  apiKey: "ek_live_...",
  entityId: "ent_123",
  onAction: (action) => {
    switch(action.type) {
      case "document_created":
        // Handle document creation
        break;
      case "document_updated":
        // Handle document update
        break;
      case "navigation":
        // Handle navigation
        break;
      case "close_requested":
        // Handle close request
        break;
    }
  },
});

Document Events

Document Eventstypescript
onAction: (action) => {
  if (action.type === "document_created") {
    const { id, documentType } = action.data;

    // Refresh your invoice list
    refreshInvoices();

    // Show success notification
    toast.success("Invoice created!");
  }

  if (action.type === "document_updated") {
    const { id, documentType } = action.data;

    // Refresh specific document
    refreshDocument(id);
  }

  if (action.type === "document_deleted") {
    const { id } = action.data;

    // Remove from your local state
    removeFromList(id);
  }
};

Customer Events

Customer Eventstypescript
onAction: (action) => {
  if (action.type === "customer_created") {
    const { id } = action.data;

    // Refresh your customer list
    refreshCustomers();
  }

  if (action.type === "customer_updated") {
    const { id } = action.data;
  }

  if (action.type === "customer_deleted") {
    const { id } = action.data;
  }
};
Navigation Eventstypescript
onAction: (action) => {
  if (action.type === "page_loaded") {
  }

  if (action.type === "navigation") {
    const { path } = action.data;

    // Update your app's URL or breadcrumbs
    updateBreadcrumbs(path);
  }
};

Close Requests

Handle when users click a close button:

Close Requesttypescript
const si = new SpaceInvoices({
  apiKey: "ek_live_...",
  entityId: "ent_123",
  onAction: (action) => {
    if (action.type === "close_requested") {
      // User clicked a close button in the embed
      // Close the embed and show your own UI
      si.close();

      // Or hide the modal/sidebar containing the embed
      setShowEmbed(false);
    }
  },
});

Error Handling

Error Handlingtypescript
const _si = new SpaceInvoices({
  apiKey: "ek_live_...",
  entityId: "ent_123",
  onError: (error) => {
    console.error("Embed error:", error.code, error.message);

    switch(error.code) {
      case "auth_failed":
      case "invalid_api_key":
        // Re-authenticate or show error
        showAuthError();
        break;
      case "entity_not_found":
        // Entity was deleted or doesn__PROTECTED_8__t have permission
        showPermissionError();
        break;
      case "network_error":
        // Network issue - maybe retry
        showNetworkError();
        break;
      default:
        showGenericError(error.message);
    }
  },
});

Error Codes

CodeDescription
auth_failedAuthentication failed
invalid_api_keyAPI key is invalid
entity_not_foundEntity doesn’t exist
permission_deniedNo permission for action
network_errorNetwork request failed
unknown_errorUnknown error occurred

Next Steps