Skip to main content

Writing actions

Actions are the foundation of tools built with Interval. Actions are just async functions which means anything you can do within a JavaScript function is possible within an action. And by extension, if you know how to write JavaScript functions, you know how to write Interval actions.

But actions have access to special functionality that other functions don't, namely they can:

Defining an action

Actions are identified by slugs that are defined in your code and are unique to your organization.

An action can be defined in two ways. The simplest and most common is to provide an async function as the value in your key:value map of actions. If you want to set additional metadata for an action in code, you can specify an object containing a handler function instead, allowing you to set action metadata properties:

import Interval from "@interval/sdk";

const interval = new Interval({
apiKey: "<YOUR API KEY>", // get an API key at https://interval.com/dashboard/develop/keys
actions: {
refund_user: async () => {
// action logic here
},
import_data: {
handler: async () => {
// action logic here
},
name: "Import data",
description: "Imports data.",
backgroundable: true,
},
edit_user: {
handler: async () => {
// action logic here
},
name: "Edit user",
description: "Only accessible via direct link with user ID parameters.",
unlisted: true,
},
},
});

interval.listen();

The following properties can be defined for an action:

  • handler: The asynchronous function that performs your action
  • name: The action's name string as visible in the dashboard
  • description: A supplementary description string for the action
  • backgroundable: A boolean indicating that the action will continue after the user leaves the page, or started on a schedule automatically
  • unlisted: A boolean that excludes the action from being listed on dashboard pages, for when your action should only be accessible via a link with additional context parameters

Actions can also be defined dynamically after calling listen() by using the interval.actions.add() method:

interval.listen();

interval.actions.add("export_data", async () => {
// action logic here
});

Removing an action

Actions can be dynamically removed as well, such as to make an action unavailable in response to some event. You can do so with the interval.actions.remove() method:

interval.listen();

interval.actions.remove("export_data");
Did this section clearly explain what you wanted to learn?