How to add a workflow tool to an app
This guide shows you how to add a tool to your app that workflows can call. When a workflow node runs the tool, Picked sends the call to your server. Your server does the work and answers with the output.
You need an app with a server that uses @picked/app-kit, and a public endpoint.
Build and install your first app makes these.
1. Declare the tool
Add the tool to workflowTools in the manifest. The manifest must have an endpoint,
because Picked sends each call to <endpoint>/tools/<name>.
import { defineConfig } from '@picked/config/define'
export default defineConfig({
app: {
handle: 'my-app',
name: 'My App',
endpoint: 'https://my-app.example.com',
workflowTools: [
{
name: 'create_ticket',
label: 'Create ticket',
description: 'Opens a support ticket for a lead.',
effect: 'write',
arguments: {
email: { label: 'Email', type: 'string', required: true },
subject: { label: 'Subject', type: 'string', required: true },
priority: { label: 'Priority', type: 'string', options: ['low', 'normal', 'high'] },
},
output: {
ticketId: { label: 'Ticket ID', type: 'string' },
url: { label: 'Ticket URL', type: 'string', optional: true },
},
},
],
},
})Set these fields correctly:
name. A lower-case letter, then lower-case letters, digits and_. Your server finds the tool by this name.effect. Setwritewhen a call changes data outside the run. A test run calls awritetool only when Execute writes is on. Setreadfor a tool that only reads.arguments. The fields that a node sets. Each has atype:string,number,booleanorjson.output. The values that your answer must contain. Setoptional: trueon a value that an answer can omit.
Workflow tools gives each field.
2. Write the tool
Give the tool to createAppHandler, in tools, with the name from the manifest. The
function gets the call and returns the output object.
import { createAppHandler, readAppEnvironment } from '@picked/app-kit/server'
import { serve } from '@picked/app-kit/node'
const port = process.env.APP_PORT ?? '4103'
const { pickedKeys } = readAppEnvironment(process.env, port)
const handler = createAppHandler({
pickedKeys,
tools: {
async create_ticket(call) {
const email = typeof call.arguments.email === 'string' ? call.arguments.email : ''
const subject = typeof call.arguments.subject === 'string' ? call.arguments.subject : ''
if (email === '') throw new Error('email is required')
if (call.run.test) return { ticketId: 'test-run' }
const response = await fetch('https://helpdesk.example.com/api/tickets', {
method: 'POST',
headers: {
'content-type': 'application/json',
'idempotency-key': call.idempotencyKey,
},
body: JSON.stringify({ email, subject, priority: call.arguments.priority ?? 'normal' }),
})
if (!response.ok) throw new Error(`The help desk answered ${response.status}`)
const ticket = await response.json()
return { ticketId: String(ticket.id), url: String(ticket.url) }
},
},
})
serve(handler, Number(port))createAppHandler checks that Picked signed the request. It then calls the function and
answers with { "output": … }.
Use these fields of the call:
call.arguments. The arguments of the node, after Picked resolves them.call.idempotencyKey. The same value for each send of one call. See step 3.call.run.test.truein a test run. See step 4.call.token. A token for the Picked API, with the scopes of the installation.
Workflow tool calls gives each field.
Check the type of each argument before you use it. An argument that is not required can
be absent.
Picked checks the output against output in the manifest. If a value is absent and not
optional, or has a different type, the node fails.
3. Make writes safe to repeat
Picked can send one call more than one time. It sends the call again after a 408, a
429 or a 5xx answer, or when it gets no answer. Each send of one call has the same
idempotencyKey. Picked also sends the key in the idempotency-key header.
Do one of these:
- If the service that you write to accepts an
idempotency-keyheader, sendcall.idempotencyKeyto it. The example in step 2 sends it in theidempotency-keyheader. - If not, record the output of each call. Before you write, look for a record with the key. If you find one, return that output.
The key is <runId>:<nodeId>. A node that runs two times in one run, in a loop, sends the
same key two times. If the loop calls the tool for different work, add the arguments to
the record key.
To keep the record in Picked, use a label of the installation. How to store data for an installation shows how.
4. Handle a test run
In a test run, call.run.test is true:
- Picked calls a
readtool, as in a live run. - Picked does not call a
writetool, unless the person turns on Execute writes.
When Execute writes is on, the writes are real. If your tool must not write during a
test, return an example output when call.run.test is true. The example in step 2
returns { ticketId: 'test-run' }.
5. Fail a call
To fail a call, throw an error. createAppHandler answers with status 422 and
{ "error": { "message": "…" } }. The node fails. For a node ticket, the message is:
Node 'ticket' ('app.my-app.create_ticket') failed: the app answered 422The run records the status of the answer. It does not record your message. Log the message on your server, so that you can find the reason.
The failure of the node has a retryable value. Picked sets it from the status of the
answer, and does not read a retryable field in your answer. The value is false for a
422 answer. Therefore, it is false for each failure from a tool of @picked/app-kit.
Workflow tool calls gives the result of each
answer.
A workflow can test retryable and call the tool again. Refer to
How to retry a failed call with a loop.
6. Deploy and use the tool
- Deploy a new version of the manifest.
- Upgrade each installation to that version.
Each installation adds the tool to its project. The name of the tool is
app.<installation slug>.<name>, for example app.my-app.create_ticket. Its label is
<app name>: <label>, for example My App: Create ticket.
Tools in a project gives the scope that the
tool needs.
A workflow author finds the tool by its label in the workflow editor. How to use an app tool in a workflow shows the steps.
When you remove or change a tool, upgrade the installations. Each installation uses the tools of the version that it uses.
Related
- App manifest — every field of a tool.
- App protocol — the call, the answer, and the resends.
- How to test an app — call a tool in a test with
toolCall. - How to read project data from an app — call the Picked API with the token of the call.
- Workflow tools — the tools that a workflow can call.
