Writing actions
- TypeScript
- JavaScript
- Python Experimental
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.
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.
Actions are the foundation of tools built with Interval. Actions are just async coroutines which means anything you can do within a coroutine function is possible within an action. And by extension, if you know how to write coroutine functions, you know how to write Interval actions.
But actions have access to special functionality that other functions don't, namely they can:
- be run by a member of your organization from the Interval dashboard,
- collect input and display output through I/O methods,
- and send push notifications to members of your organization.
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 routes
. 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:
caution
With the introduction of the Pages API, the actions
param in the Interval
class has been renamed to routes
. You can still define actions here as usual, in addition to pages.
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 routes
. 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:
caution
With the introduction of the Pages API, the actions
param in the Interval
class has been renamed to routes
. You can still define actions here as usual, in addition to pages.
An action can be defined in two ways. The simplest and most common is to add the @interval.action
decorator to an async coroutine. If you want to set additional metadata for an action in code, you can pass options to the decorator specifying metadata properties:
- TypeScript
- JavaScript
- Python Experimental
import Interval from "@interval/sdk";
const interval = new Interval({
apiKey: "<YOUR API KEY>", // get an API key at https://interval.com/dashboard/develop/keys
routes: {
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();
import Interval from "@interval/sdk";
const interval = new Interval({
apiKey: "<YOUR API KEY>", // get an API key at https://interval.com/dashboard/develop/keys
routes: {
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();
from interval_sdk import Interval, IO
@interval.action
async def refund_user(io: IO):
# action logic here
pass
@interval.action(name="Import data", description="Imports data.", backgroundable=True)
async def import_data(io: IO):
# action logic here
pass
@interval.action(name="Edit user", description="Only accessible via direct link with user ID parameters", unlisted=True)
async def edit_user(io: IO):
# action logic here
pass
interval.listen()
The following properties can be defined for an action:
handler
: The asynchronous function that performs your actionname
: The action's name string as visible in the dashboarddescription
: A supplementary description string for the actionbackgroundable
: A boolean indicating that the action will continue after the user leaves the page, or started on a schedule automaticallyunlisted
: 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
Dynamically adding and removing actions
Actions can also be defined dynamically after calling listen()
by using
the interval.routes.add()
method:
- TypeScript
- JavaScript
- Python Experimental
interval.listen();
interval.routes.add("export_data", async () => {
// action logic here
});
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.routes.remove()
method:
interval.listen();
interval.routes.remove("export_data");
interval.listen();
interval.routes.add("export_data", async () => {
// action logic here
});
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.routes.remove()
method:
interval.listen();
interval.routes.remove("export_data");
interval.listen()
async def export_data(io: IO):
# action logic here
pass
interval.routes.add("export_data", export_data)
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.routes.remove()
method:
interval.listen();
interval.routes.remove("export_data")