The Tools API
The @teamkeel/sdk package contains a typed accessor for every Console tool in your project. These accessors are all available on the exported tools object.
import { tools } from "@teamkeel/sdk";Each accessor takes the tool's inputs and returns a reference to the tool:
const ref = tools.getOrder({ id: order.id });
// { toolId: "get-order", inputs: { id: "..." } }Flows use these references to redirect to a tool on completion:
return ctx.complete({
title: "Order created",
redirect: {
tool: tools.getOrder({ id: order.id }),
label: "View order",
},
});Accessor names
Accessors are named after the tool id in camelCase. A tool's default id is its action or flow name in kebab-case:
| Schema | Tool id | Accessor |
|---|---|---|
Action getOrder | get-order | tools.getOrder |
Flow ProcessReturn | process-return | tools.processReturn |
Duplicated tool get-order-2 | get-order-2 | tools.getOrder2 |
Tools you duplicate or give a custom id in the tools configuration get their own accessors. After you add, duplicate or rename a tool, run keel generate to update tools.
Hover an accessor in your editor to see the tool's display name and id.
Inputs
Inputs are typed from the tool's target:
- An action tool takes the action's inputs.
- A flow tool takes the flow's inputs.
- A hub or ledger tool takes no inputs.
When every input is optional, or the tool has no inputs, call the accessor with no arguments:
tools.listOrders();Missing required inputs, or inputs with the wrong shape, fail type-checking.
References can only come from the tools accessors. A hand-written object such as { toolId: "get-order" } is not accepted where a tool reference is expected.
Types
The SDK also exports two types that describe your project's tools:
| Type | Description |
|---|---|
ToolId | A union of every tool id, e.g. "get-order" | "list-orders" |
ToolInputs | Maps each tool id to the type of its inputs |
import type { ToolId, ToolInputs } from "@teamkeel/sdk";
const id: ToolId = "get-order";
const inputs: ToolInputs["get-order"] = { id: order.id };Reference
Ids that aren't valid identifiers
If a tool id isn't a valid TypeScript identifier once camel-cased (for example, it starts with a digit), the accessor uses the raw id:
tools["3pl-shipments"]();Name collisions
Accessors are assigned in tool id order. If an id's camelCase name is already taken by an earlier tool, the accessor uses the raw id instead, such as tools["get_order"]. If the raw id is also taken, it gets a numeric suffix such as _2.
Runtime version
The tools object needs a version of @teamkeel/functions-runtime that matches the Keel CLI. With an older runtime, calling an accessor throws:
tools.getOrder needs a newer @teamkeel/functions-runtime: upgrade the package to match the keel cli