SDKs
The official TypeScript and Python libraries: create a task, wait for the decision and verify the signed callback, with retries and idempotency built in.
Two official SDKs wrap the public API. They create, read, list and cancel tasks, wait for a decision, and check the signature of a callback. Both have no runtime dependencies, send an Idempotency-Key with every create and retry safely. They are open source under the Apache License 2.0, in the tasktohuman-integrations repository.
| TypeScript | Python | |
|---|---|---|
| Package | @tasktohuman/sdk on npm | tasktohuman on PyPI |
| Source | sdks/typescript | sdks/python |
| Runs on | Node.js 22 or later (ESM, with type declarations) | Python 3.10 or newer, standard library only |
| Client | TaskToHuman | TaskToHuman, and AsyncTaskToHuman for asyncio |
| Overview | TypeScript SDK | Python SDK |
Use them on your server only: the client holds your API key, which must never reach a web page or an app you ship.
Install#
npm install @tasktohuman/sdkpip install tasktohumanCreate a key in the app under API & webhooks, then API keys, and keep it in the TTH_API_KEY environment variable. Start with a test key (tth_test_…): it works exactly like a live one but notifies only you. See Test mode.
Create a task and wait for the decision#
import { TaskToHuman } from "@tasktohuman/sdk";
const tth = new TaskToHuman({ apiKey: process.env.TTH_API_KEY! }); // tth_live_… or tth_test_…
const task = await tth.createTask(
{
task_type: "approve_refund",
title: "Refund €249 to order #4821?",
payload: { order_id: "4821", amount: 249, reason: "Arrived damaged" },
deadline_minutes: 120,
},
// Names the real-world request: sending it again returns the same task instead of asking twice.
{ idempotencyKey: "refund-4821" },
);
const done = await tth.waitForDecision(task.id, { timeoutMs: 15 * 60_000 });
if (done.decision && !done.decision.automatic && ["approved", "edited"].includes(done.decision.action)) {
// A person approved. For "edited", the changed values are in done.decision.payload.
}import os
from tasktohuman import TaskToHuman
tth = TaskToHuman(os.environ["TTH_API_KEY"])
order_id = "4821"
task = tth.create_task(
"approve_refund", # a task type of your workspace
title=f"Refund €249 to order #{order_id}?",
payload={"order_id": order_id, "amount": 249, "reason": "Arrived damaged"},
deadline_minutes=120,
idempotency_key=f"refund-{order_id}", # names the real-world request
)
task = tth.wait_for_decision(task["id"], timeout=600)
decision = task["decision"]
if decision and not decision["automatic"] and decision["action"] in ("approved", "edited"):
... # a person approved: refund (with decision["payload"] when edited)Act on a decision only when automatic is false: a task type's default action can finish an unanswered task as approved, with the state expired (see the decision).
Waiting checks the task at once, then after 5 seconds, doubling up to 60 seconds, and raises a WaitTimeoutError (with the last task read) when the time is up. It suits scripts, tests and short agent turns. In production, give the task a callback_url (or set one on the task type) and verify the callback: the decision arrives within seconds, survives restarts and does not use your rate limit.
Verify a callback#
Both SDKs check the TTH-Signature header against the raw body, as described in Callbacks and signatures, and return the parsed task. A refused callback raises SignatureVerificationError, with a reason (missing_header, malformed_header, timestamp_out_of_tolerance or no_matching_signature). A missing or empty secret raises a TypeError, so an unset environment variable can't turn the check off.
import express from "express";
import { SignatureVerificationError, verifyCallback } from "@tasktohuman/sdk";
const app = express();
// express.raw keeps the exact bytes that were signed.
app.post("/hooks/tasktohuman", express.raw({ type: "application/json" }), async (req, res) => {
let task;
try {
task = await verifyCallback({ body: req.body, headers: req.headers, secret: process.env.TTH_SIGNING_SECRET! });
} catch (error) {
if (error instanceof SignatureVerificationError) return res.sendStatus(400);
throw error;
}
// Skip a TTH-Event-Id you already handled (a delivery can arrive twice), answer within 10 seconds,
// and act only on a person's decision: task.decision?.automatic === false.
res.sendStatus(204);
});import os
from flask import Flask, abort, request
from tasktohuman import SignatureVerificationError, verify_callback
app = Flask(__name__)
@app.post("/hooks/tasktohuman")
def tasktohuman_callback():
try:
task = verify_callback(
request.get_data(), # the exact bytes that were signed
request.headers.get("TTH-Signature"),
os.environ["TTH_SIGNING_SECRET"],
)
except SignatureVerificationError:
abort(400)
# Skip a TTH-Event-Id you have already handled, then act on
# task["decision"] only when task["decision"]["automatic"] is False.
return "", 204While a rotated secret overlaps, pass both secrets to cover your own deploy: secret: [newSecret, oldSecret] in TypeScript, verify_callback(body, headers, [new_secret, old_secret]) in Python.
What else they do#
- Every call of
/v1: get, list (one page, or every page withlistAllTasks/iter_tasks) and cancel tasks, list task types and humans, the key's workspace and the plan's entitlements, and download the files a person sent with an answer. - Typed errors that carry the API's
code, as the Errors page lists them, includingNotEnoughCreditsErrorwhen a task is refused for lack of credits. - Retries on 429, server errors and network errors, with the same idempotency key, so a retry never asks a person twice. Redirects are refused, so your key only ever goes to the API.
The README of each SDK in the repository lists every method and option. Prefer a no-code tool? See No-code integrations.