Skip to main content

Installation

To install the ActivitySmith Node SDK, you can use npm:
Node
# npm install activitysmith

import ActivitySmith from "activitysmith";

const activitysmith = new ActivitySmith({ apiKey: "YOUR-API-KEY" });

Usage

  1. Get an API key from ActivitySmith
  2. Set the API key as an environment variable named ACTIVITYSMITH_API_KEY or pass it as apiKey when creating the client.
Here’s an example of how to use the SDK with error handling:
Node
import ActivitySmith from "activitysmith";

const activitysmith = new ActivitySmith({
  apiKey: process.env.ACTIVITYSMITH_API_KEY,
});

try {
  const push = await activitysmith.notifications.sendPushNotification({
    pushNotificationRequest: {
      title: "Build Failed",
      message: "CI pipeline failed on main branch",
    },
  });

  const start = await activitysmith.liveActivities.startLiveActivity({
    liveActivityStartRequest: {
      contentState: {
        title: "ActivitySmith API Deployment",
        subtitle: "start",
        numberOfSteps: 4,
        currentStep: 1,
        type: "segmented_progress",
        color: "yellow",
      },
    },
  });

  const activityId = start.activityId;

  await activitysmith.liveActivities.updateLiveActivity({
    liveActivityUpdateRequest: {
      activityId,
      contentState: {
        title: "ActivitySmith API Deployment",
        subtitle: "npm i & pm2",
        currentStep: 3,
      },
    },
  });

  await activitysmith.liveActivities.endLiveActivity({
    liveActivityEndRequest: {
      activityId,
      contentState: {
        title: "ActivitySmith API Deployment",
        subtitle: "done",
        currentStep: 4,
        autoDismissMinutes: 3,
      },
    },
  });

  console.log("Push sent:", push.success);
} catch (error) {
  console.error(error);
}

Send a Push Notification

Use activitysmith.notifications.sendPushNotification with a pushNotificationRequest. title is required; message and subtitle are optional.
Node
const response = await activitysmith.notifications.sendPushNotification({
  pushNotificationRequest: {
    title: "Build Failed",
    message: "CI pipeline failed on main branch",
  },
});

console.log(response.devicesNotified, response.timestamp);

Start a Live Activity

Use activitysmith.liveActivities.startLiveActivity with a contentState payload. For the segmented progress type, title, numberOfSteps, currentStep, and type are required. Use camelCase fields in the SDK (numberOfSteps, currentStep, stepColor, autoDismissMinutes).
Node
const start = await activitysmith.liveActivities.startLiveActivity({
  liveActivityStartRequest: {
    contentState: {
      title: "ActivitySmith API Deployment",
      subtitle: "start",
      numberOfSteps: 4,
      currentStep: 1,
      type: "segmented_progress",
      color: "yellow",
    },
  },
});

const activityId = start.activityId;

Update a Live Activity

Use activitysmith.liveActivities.updateLiveActivity with the activityId you received from startLiveActivity.
Node
await activitysmith.liveActivities.updateLiveActivity({
  liveActivityUpdateRequest: {
    activityId,
    contentState: {
      title: "ActivitySmith API Deployment",
      subtitle: "npm i & pm2",
      currentStep: 3,
    },
  },
});

End a Live Activity

Use activitysmith.liveActivities.endLiveActivity with the activityId. You can optionally control how long the ended Live Activity stays visible using autoDismissMinutes (default 3, 0 for immediate dismissal).
Node
await activitysmith.liveActivities.endLiveActivity({
  liveActivityEndRequest: {
    activityId,
    contentState: {
      title: "ActivitySmith API Deployment",
      subtitle: "done",
      currentStep: 4,
      autoDismissMinutes: 3,
    },
  },
});

Error Handling

Handle errors with try/catch around API calls:
Node
try {
  await activitysmith.notifications.sendPushNotification({
    pushNotificationRequest: { title: "Hello" },
  });
} catch (error) {
  console.error(error);
}