Poll and claim a WorkItem
Claims the environment’s oldest pending WorkItem compatible with the
worker’s version for the caller. The
returned item is leased: acknowledge it with ack, renew it with heartbeats
and release it with stop, always presenting the generation the poll returned. With
block_ms above zero the call long-polls until work arrives or the
budget expires (an empty response then). A successful poll is a claim:
do not retry it idempotently.
curl --request GET \
--url https://api.moonshot.ai/v1/environments/{environment_id}/work/poll \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.ai/v1/environments/{environment_id}/work/poll"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.ai/v1/environments/{environment_id}/work/poll', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.ai/v1/environments/{environment_id}/work/poll",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.ai/v1/environments/{environment_id}/work/poll"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://api.moonshot.ai/v1/environments/{environment_id}/work/poll")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.ai/v1/environments/{environment_id}/work/poll")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"id": "work_01h455vb4pex5vsknk084sn02q",
"session_id": "sesn_01h455vb4pex5vsknk084sn02q",
"environment_id": "env_01h455vb4pex5vsknk084sn02q",
"status": "queued",
"generation": 1,
"worker_id": "worker-1",
"lease_ttl_ms": 300000,
"created_at": "2026-09-20T12:00:00Z",
"claimed_at": "2026-09-20T12:00:00Z",
"secret": "eyJzZXNzaW9uc190b2tlbiI6InN3dC1hYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODlBQkNERUZHIn0"
}{
"error": {
"type": "invalid_parameter_error",
"message": "The `name` parameter is invalid.",
"request_id": "req_01h455vb4pex5vsknk084sn02q",
"param": "name"
}
}{
"error": {
"type": "authentication_error",
"message": "Provide valid credentials before retrying.",
"request_id": "req_01h455vb4pex5vsknk084sn02q"
}
}{
"error": {
"type": "rate_limit_reached_error",
"message": "Too many requests. Retry after 60 seconds.",
"request_id": "req_01h455vb4pex5vsknk084sn02q"
}
}{
"error": {
"type": "server_error",
"message": "The platform could not complete the request. Retry later.",
"request_id": "req_01h455vb4pex5vsknk084sn02q"
}
}Headers
API version for this request, in YYYY-MM-DD form. Send 2026-09-01-beta
for the version documented here. Required outside /v1/files*: a missing
header there is rejected with 400 invalid_request_error; on /v1/files*
its absence selects the compatibility behavior. An unknown value is
rejected with 400 invalid_request_error on every endpoint.
API versions served by the platform. New dated versions are added as they
ship; a version is removed after its announced Sunset date.
2026-09-01-beta Query Parameters
Long-poll budget in milliseconds, at most 999: with 0 (default) the
call returns immediately (204 when no compatible work is pending); with a positive
value the call waits for work until the budget expires. Polling is not
idempotent: each successful call claims the environment's oldest
compatible pending item.
0 <= x <= 999How early this poll reclaims delivered items, in milliseconds (default
5000). Before returning work, it puts items delivered earlier than this
threshold back on the queue. The ack window remains fixed at 5000ms;
the background sweeper reclaims items when it expires. This parameter
changes only how early this poll reclaims them. Ack immediately after
delivery, and start a slow executor after ack. lease_ttl_ms controls
recovery time.
x >= 0Response
The delivered WorkItem; 204 when the block budget expired without compatible work.
One row of the self-hosted work queue: a Session whose sandbox tool calls
execute on the environment owner's workers. GET /v1/environments/{environment_id}/work/poll
delivers the item (still queued until acked); the generation it
carries is the fencing token every later ack, heartbeat, and stop must
present.
user.tool_result writes need no explicit token: the session work token
in secret carries the generation binding and the platform derives the
fence from it.
The work item's session-scoped fencing token. Starts at 1 and increases;
a normal release, a platform reclaim, or cancelling a live item also
raises it. Use the value returned by poll for heartbeat, ack, and stop.
A stale value is rejected with 412 failed_precondition_error.
x >= 11
Credential bundle for the Session that owns this WorkItem. Only the poll
response that leases the item includes it. The bundle is a base64url-
encoded JSON object. Decode it and send its sessions_token as
Authorization: Bearer for Session write endpoints (for example,
POST /v1/sessions/{id}/events) and this WorkItem's ack, heartbeat, and
stop requests. The token is scoped to that Session. Other bundle members
are reserved; leave them unchanged. The credential expires when the item
is settled or reclaimed. A reclaim gives the item to another worker with
a new credential and invalidates this one. No other endpoint returns it.
Treat the bundle as a secret. Do not log or persist it.
"eyJzZXNzaW9uc190b2tlbiI6InN3dC1hYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODlBQkNERUZHIn0"
Was this page helpful?
curl --request GET \
--url https://api.moonshot.ai/v1/environments/{environment_id}/work/poll \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.ai/v1/environments/{environment_id}/work/poll"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.ai/v1/environments/{environment_id}/work/poll', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.ai/v1/environments/{environment_id}/work/poll",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.ai/v1/environments/{environment_id}/work/poll"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://api.moonshot.ai/v1/environments/{environment_id}/work/poll")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.ai/v1/environments/{environment_id}/work/poll")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"id": "work_01h455vb4pex5vsknk084sn02q",
"session_id": "sesn_01h455vb4pex5vsknk084sn02q",
"environment_id": "env_01h455vb4pex5vsknk084sn02q",
"status": "queued",
"generation": 1,
"worker_id": "worker-1",
"lease_ttl_ms": 300000,
"created_at": "2026-09-20T12:00:00Z",
"claimed_at": "2026-09-20T12:00:00Z",
"secret": "eyJzZXNzaW9uc190b2tlbiI6InN3dC1hYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODlBQkNERUZHIn0"
}{
"error": {
"type": "invalid_parameter_error",
"message": "The `name` parameter is invalid.",
"request_id": "req_01h455vb4pex5vsknk084sn02q",
"param": "name"
}
}{
"error": {
"type": "authentication_error",
"message": "Provide valid credentials before retrying.",
"request_id": "req_01h455vb4pex5vsknk084sn02q"
}
}{
"error": {
"type": "rate_limit_reached_error",
"message": "Too many requests. Retry after 60 seconds.",
"request_id": "req_01h455vb4pex5vsknk084sn02q"
}
}{
"error": {
"type": "server_error",
"message": "The platform could not complete the request. Retry later.",
"request_id": "req_01h455vb4pex5vsknk084sn02q"
}
}