Typescript SDK
This is the Hatchet Typescript SDK reference. On this page, we'll get you up and running with a Typescript worker. This guide assumes that you already have a Hatchet engine instance running. If you don't, you can:
If you run into any issues, please file an issue on the Hatchet Typescript SDK GitHub repository (opens in a new tab).
Installation
npm i @hatchet-dev/typescript-sdk
Generate a Token
Navigate to your Hatchet dashboard and navigate to your settings tab. You should see a section called "API Keys". Click "Create API Key", input a name for the key and copy the key. Then set the following environment variables:
HATCHET_CLIENT_TOKEN="<your-api-key>"
You may need to set additional environment variables depending on your self-hosted configuration. The Hatchet clients default to SSL by default, but to disable this you can set:
HATCHET_CLIENT_TLS_STRATEGY=none
Run your first worker
Make sure you've set the HATCHET_CLIENT_TOKEN
environment variable via export HATCHET_CLIENT_TOKEN="<your-api-key>"
. Next, copy the following code into a worker.ts
file:
import Hatchet, { Workflow } from "@hatchet-dev/typescript-sdk";
const hatchet = Hatchet.init();
const workflow: Workflow = {
id: "first-typescript-workflow",
description: "This is my first workflow",
on: {
event: "user:create",
},
steps: [
{
name: "step1",
run: async (ctx) => {
console.log(
"starting step1 with the following input",
ctx.workflowInput(),
);
return {
result: "success!",
};
},
},
],
};
const worker = hatchet.worker("my-worker");
await worker.registerWorkflow(workflow);
worker.start();
Next, modify your package.json
to include a script to start:
{
// ...rest of your `package.json`
"scripts": {
// ...existing scripts
"worker": "npx ts-node worker.ts"
}
}
Now to start the worker, in a new terminal run:
npm run worker
Run your first workflow
The worker is now running and listening for steps to execute. You should see your first worker registered in the Workers
tab of the Hatchet dashboard:
You can now trigger your first workflow by navigating to the Workflows
tab, selecting your workflow, and clicking the top right "Trigger workflow" button:
That's it! You've successfully deployed Hatchet and run your first workflow.
Next Steps
Congratulations on running your first workflow!
To test out some more complicated examples, check out the Hatchet Typescript Quickstart (opens in a new tab).