Worker Getting Started
Tianji Worker lets you run small functions without maintaining a separate service. A worker can receive HTTP requests, run on a schedule, call external APIs, read environment variables, and store short-lived data.
This guide walks you through creating and publishing your first worker in the Tianji dashboard. If you want an AI coding agent to generate the source code, see the Worker Agent Reference.
Before you start
Worker is disabled by default on self-hosted Tianji instances. Add this environment variable to the Tianji server and restart it:
ENABLE_FUNCTION_WORKER=true
After Worker is enabled, open Worker from the Tianji sidebar.
Ask an Agent to write your Worker
You do not need to write the Worker code by hand. Give the Worker Agent Reference to your coding agent so it can learn Tianji's runtime and generate compatible code.
If your Agent can open web pages
Copy the following prompt, replace the placeholders, and send it to your Agent:
First, read the Tianji Worker Agent Reference completely:
https://tianji.dev/docs/worker/agent-reference
Then create a Tianji Worker for me.
Target: dashboard JavaScript
Purpose: <what the Worker should do>
Input: <payload fields and validation rules>
Environment variables: <variable names only; do not include Secret values>
External APIs: <APIs the Worker should call, or none>
Temporary data: <what to store in KV, or none>
Output: <the expected result>
Return the Worker source code and example test payloads. Do not deploy it.
For example, you can replace Purpose with “receive a GitHub webhook and forward a short message to my team webhook,” then list the required payload fields and environment-variable names.
If your Agent cannot open web pages
- Open the Worker Agent Reference.
- Copy the entire page.
- Paste it into your conversation with the Agent.
- Add your Worker requirements using the prompt above.
Do not paste real API keys or passwords into the conversation. Give the Agent environment-variable names such as API_TOKEN; add the actual values later as Secret variables in Tianji.
After the Agent returns code:
- Review the environment-variable names it requires.
- Paste the code into the Tianji Worker editor.
- Add Text and Secret variables in Tianji.
- Run the provided valid and invalid test payloads.
- Save and activate the worker only after the results are correct.
Create your first worker
- Open Worker in the dashboard.
- Click Add Worker.
- Enter a name and an optional description.
- Paste the following code into the editor.
export default {
async fetch(payload, context) {
const name = typeof payload.name === 'string' ? payload.name : 'world';
return {
message: `Hello, ${name}!`,
trigger: context.type,
timestamp: new Date().toISOString(),
};
},
};
Every worker exports a fetch method. Tianji calls it with:
payload: input values sent to the worker.context: information about the trigger, request, and configured environment variables.
The value returned by fetch becomes the worker result.
Test before saving
Use the editor's Test action with this payload:
{
"name": "Tianji"
}
The result should look similar to:
{
"message": "Hello, Tianji!",
"trigger": "test",
"timestamp": "2026-08-16T00:00:00.000Z"
}
Use console.log, console.warn, or console.error while debugging. Tianji displays these messages in the execution details.
Call a worker over HTTP
Save the worker, keep it Active, and make sure it is Public. Its endpoint is:
https://<your-tianji-host>/api/worker/<workspace-id>/<worker-id>