Skip to main content

Your first API call

Not yet run on a real install. The commands on this page are written from the API's description (openapi.json) and have not been run against an installation yet. Each block says so until it has been.

This page starts a task from a script: create a token, start a task, send it a message and read what the agent did. You need an account on an installation with at least one model route, curl, and jq to read the answers.

1. Create a token​

In the web app, open Settings › API tokens and choose Create token: give it a Name and when it Expires in, then copy the token, which starts with tqa_pat_. It is shown once. The token acts as you, with your role, until it expires or you revoke it (API tokens). Creating a token needs a signed-in browser session, so this step is always done in the web app.

Keep the installation's address, the one you open in the browser, and the token in two variables:

# Not yet run on a real install.
export TQA_URL=https://agent.example.com
export TQA_TOKEN=tqa_pat_... # paste your token; never commit it

Check that the token works. The answer is your account:

# Not yet run on a real install.
curl -sS "$TQA_URL/api/v1/auth/me" -H "Authorization: Bearer $TQA_TOKEN" | jq

2. Choose a model route​

A task runs on a model route. List the ones you may use and note a name:

# Not yet run on a real install.
curl -sS "$TQA_URL/api/v1/models" -H "Authorization: Bearer $TQA_TOKEN" | jq -r '.items[].name'

3. Start a task​

A task is a conversation in the API. Creating one needs an Idempotency-Key, a new unique value for each action, so that a retried request never creates a second task. Without a workspace_id, the task gets a new empty workspace.

# Not yet run on a real install.
TASK=$(curl -sS -X POST "$TQA_URL/api/v1/conversations" \
-H "Authorization: Bearer $TQA_TOKEN" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"title": "My first API task", "model_route": "<route name>"}' | jq -r .id)
echo "$TASK"

The answer is the new task (201), with its id. The task appears in your sidebar in the web app as well.

4. Send a message​

# Not yet run on a real install.
curl -sS -X POST "$TQA_URL/api/v1/conversations/$TASK/messages" \
-H "Authorization: Bearer $TQA_TOKEN" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"text": "Write a file hello.txt that says hello, then list the workspace."}' | jq

The gateway answers 202 once the message is stored: accepted, the message_id, and held, which is true when the message waits for a run that is still going. The run starts in the task's sandbox; the first start of a sandbox can take a while.

5. Read the events​

Everything the task does is a numbered event: the message, the run's start, the agent's text, each tool call and its result, and the run's end. Read them a page at a time, from after a sequence number:

# Not yet run on a real install.
curl -sS "$TQA_URL/api/v1/conversations/$TASK/events?after=0&limit=100" \
-H "Authorization: Bearer $TQA_TOKEN" | jq -r '.items[] | select(.type == "event") | "\(.seq) \(.event.type)"'

Each page has next_after, where the next read starts. The agent's visible answer is the text.delta events, in order; the run is over at run.completed, run.cancelled or run.failed. This loop prints the answer as it arrives and stops at the run's end:

# Not yet run on a real install.
AFTER=0
while :; do
PAGE=$(curl -sS "$TQA_URL/api/v1/conversations/$TASK/events?after=$AFTER&limit=100" \
-H "Authorization: Bearer $TQA_TOKEN")
echo "$PAGE" | jq -j '.items[] | select(.type == "event") | .event | select(.type == "text.delta") | .text'
echo "$PAGE" | jq -e '.items[] | select(.type == "event") | .event.type
| select(. == "run.completed" or . == "run.cancelled" or . == "run.failed")' > /dev/null && break
AFTER=$(echo "$PAGE" | jq -r --arg after "$AFTER" '.next_after // ($after | tonumber)')
sleep 2
done

To follow a task live instead, stream the same events as server-sent events. Each event's id: is its sequence number, so a client that reconnects with Last-Event-ID gets every event exactly once:

# Not yet run on a real install.
curl -sS -N "$TQA_URL/api/v1/conversations/$TASK/events?stream=1&protocol=1" \
-H "Authorization: Bearer $TQA_TOKEN"

The frames and events are listed in the protocol reference.

When a call is refused​

A refusal has a status and a body with a stable code, such as {"error": {"code": "unauthorized", ...}}. A request without a valid token answers 401, and one the token's owner may not make, such as an administration route for a member, answers 403. Creating and revoking tokens, and changing shared secrets, need a signed-in browser session and refuse a token with 403 session_required. Each operation's own codes are in the API reference.

Next​

  • Call the agent from another system: the whole flow for a script or a workflow product, from an agent profile and input files to the result files.

  • Stop a run: POST /api/v1/conversations/{id}/runs/{run_id}/cancel, with the run_id of its run.started event.

  • Give the agent a file: POST /api/v1/conversations/{id}/uploads stores it in the workspace's uploads/ folder.

  • Read the files the agent wrote: GET /api/v1/conversations/{id}/files and …/files/download.