Execute a command
Run a Bash command in a World. The request answers as soon as the command finishes or needs an approval, so most commands return their result here; one that still runs after 30 seconds answers with status running. Reuse a session to preserve files, the working directory, and shell state.
/api/v1/worlds/:worldId/executeFor a new session, send userId and command. No separate user or session setup is needed.
For later commands, also send the returned session.id as sessionId. Omitting it starts a new session every time.
const response = await fetch("https://api.smallworld.sh/api/v1/worlds/01995e56-e2d4-7f19-b691-7bf9af6abdc8/execute", {
method: "POST",
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
body: JSON.stringify({
"userId": "customer_123",
"command": "echo 'Hello from Smallworld'"
}),
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
const result = await response.json();
Use your server-side apiKey. Replace the example IDs.
Path parameters#
worldIdUUIDrequiredThe World ID shown in the dashboard.
Request body#
commandstringrequiredNon-empty Bash source, at most 262144 UTF-8 bytes, with no null bytes.
userIdstringrequiredYour application's user ID. Required, also with sessionId, which must belong to this user.
sessionIdUUIDAn existing session in this World. Omit to create a new session.
groupIdstringYour application's group ID: the group a new session acts for. Optional for a user with no group or one group, and required when the user belongs to several groups. With sessionId it isn't needed; if sent, it must be the session's group. null is invalid.
contextobjectOptional JSON object, at most 4096 bytes when serialized. Forwarded to this execution's custom integration calls; never shown to the agent, and stored only while the execution is parked waiting for an approval.
filesarrayUpload up to 100 files totaling 8 MiB decoded. Each entry has path (an absolute path under /workspace), content, and optional encoding (utf8 or base64, default utf8), persistInSession (default false), and sessionOnly (default false). Uploads replace files at their paths before the command runs.
fileChangesstringWhich changes execution.fileChanges returns: all (the default) for every file the command created, modified, or deleted under /workspace, uploaded for the uploaded paths only, or none for null.
waitnumberSeconds the request is held for the command to finish or need an approval, 0 to 30. Defaults to 30. Send less to get the execution ID while the command still runs; keep your client timeout above the wait.
Responses#
| Status | Your next step |
|---|---|
completed | Read stdout and stderr. An exitCode of 0 means success. Save the fileChanges you track. |
running | Read the execution again right away: the read is held until the command finishes or needs an approval. Keep the same execution ID. |
requires_approval | Ask the user to approve or deny each pending toolCall, then read the execution again. Uploaded files stay in place meanwhile; fileChanges arrives with the final result. |
timed_out | The command reached its time limit and exited with 124. Shorten the work or raise the limit in Settings → Terminal. |
interrupted | The command stopped before finishing. Inspect output and tool calls before deciding whether to run another command. |
Send the next command only after this execution finishes. One execution can pause for approval more than once.
If a response is lost, read the execution when you have its ID. If you never received an ID, do not resend automatically: the command may have run.
The command finished. Check exitCode as well as stdout, stderr, and toolCalls; completed can have a nonzero exit code.
{
"toolCalls": [],
"user": {
"id": "customer_123",
"email": null,
"groupIds": [],
"createdAt": "2026-10-10T12:00:00.000Z",
"updatedAt": "2026-10-10T12:00:00.000Z"
},
"session": {
"id": "01995e57-0501-76b8-b797-5b24df5b307f",
"worldId": "01995e56-e2d4-7f19-b691-7bf9af6abdc8",
"userId": "customer_123",
"groupId": null,
"status": "idle",
"closedReason": null,
"stateVersion": 1,
"createdAt": "2026-10-10T12:00:00.000Z",
"updatedAt": "2026-10-10T12:00:01.000Z"
},
"created": {
"user": true,
"session": true
},
"execution": {
"id": "01995e57-3027-7557-a233-009ea3dcd8fc",
"sessionId": "01995e57-0501-76b8-b797-5b24df5b307f",
"command": "echo 'Hello from Smallworld'",
"status": "completed",
"stdout": "Hello from Smallworld\n",
"stderr": "",
"truncated": false,
"exitCode": 0,
"errorCode": null,
"fileChanges": [],
"outputFiles": [],
"mountChanges": null,
"createdAt": "2026-10-10T12:00:00.000Z",
"completedAt": "2026-10-10T12:00:01.000Z"
}
}
More about this endpoint
Public user IDs are identifiers from your application, not Smallworld's internal database IDs. Derive them from your authenticated user on your backend.
Omitting sessionId always creates a new session, even when this user already has sessions in this World. Store the returned session.id and reuse it for subsequent commands.
Use view to read PDF text, select an image, or return a file. execution.outputFiles lists the selected images and downloads, including unchanged uploads. Fetch the bytes with the same API key at each entry's contentPath. See the Send and receive files guide for a complete example.
An unknown userId is created automatically.
A session acts for one user and at most one group, fixed when the session is created. A new session for a user with no groups is groupless: it uses personal and workspace-wide resources only, and stays groupless when the user joins a group later. A new session for a user with one group acts for that group. A user in several groups needs groupId. Requests for users with no group or one group need no groupId.
A user with groups can't start a groupless session, so groupId: null is invalid. With sessionId, the session keeps its group; a groupId for another group returns session_group_mismatch.
Inside a group session the user's own connection to an integration is used first, also when it needs reconnecting, then the connection of the session's group. The group's tool overrides and integration switches apply to every call in the session, whichever connection serves it. The session's mounts are the global ones, those of the session's group, and the user's own. Two groups can therefore have different accounts for the same integration and different mounts at the same path.
A move (PUT /groups/:groupId/users/:userId, or POST /users with groupId) keeps sessions open: the user's open sessions that acted for the old group act for the new one. A closed session never reopens, also when the user rejoins the group; start a new session. The user's sessions for other groups are unaffected.
Commands start immediately. The initial response waits up to one second for completion or an approval pause, so it may already contain output.
An HTTP 200 does not guarantee a successful command. Check execution.status, exitCode, stdout, and stderr.
When the user leaves the session's group, other than by a move, or the group is deleted, the session is closed for good: session.status is closed and session.closedReason says why. A running command is stopped at once and ends interrupted with errorCode session_closed. A command waiting for approval ends the same way, and its pending tool call ends interrupted with errorCode session_closed. Executions, tool calls, and outputs stay readable.
Poll GET /executions/:executionId while the command is running or requires_approval. For each pending toolCall, render providerApproval.choices when providerApproval is present and submit the selected value through /tool-approvals/:id/provider/:approvalId. Otherwise use /tool-approvals/:id/approve or /deny.
A tool call marked unknown may have reached its remote service. Check the remote result before retrying a write.
Changes to a mount are checked once per mount at the end of the command. When they need approval, the execution is requires_approval and toolCalls includes the pending check, with integration mounts and the mount's ID as toolName; decide it like any tool call.
When a mount's changes aren't saved, stderr gets a notice such as Changes to /workspace/docs were not saved: a person rejected them. and an exit code of 0 becomes 1. The session's /workspace files and other mounts are still saved.
Each fileChanges entry has path, operation (created, modified, or deleted), and uploaded, which is true for paths uploaded in this request. Created and modified entries include the final content and its encoding, never truncated. Uploaded paths are compared with the uploaded bytes and other paths with the session's files before the command, so an unchanged upload isn't listed and removing a non-retained upload is never a deletion. Directories, modification times, and permissions aren't reported.
A retained upload becomes an ordinary session file, so later executions report its changes with uploaded false. A rename is a deletion plus a creation and a copy is a creation, also for non-retained uploads, which then persist at their new path. A non-retained upload over an existing file doesn't bring that file back. Directories created to hold an upload stay in the session, also when the upload itself isn't retained, and are never reported.
To keep your own copy of a session's files, apply your uploads and every execution's fileChanges in order, then remove your non-retained uploads. If your application can change a file while a command runs, compare its revision before saving the returned content; Smallworld doesn't merge changes.
There are no endpoints that read or write session files directly: upload files with an execution (the command can be true) and delete them with rm. Returned contents stay in the saved execution result, also for non-retained uploads.
By default, an upload into a visible mount with persistInSession true writes directly to the mount. With persistInSession false only this command sees the upload. To keep attachments private to the session, set sessionOnly true. If a guarded path overlaps a mount at execution time, the execution is interrupted with errorCode execution_file_conflict before any upload is written.
Only one command can run in a session at a time. Queue requests per session in your application.
This endpoint has no idempotency parameter. Do not blindly repeat a POST after an ambiguous network failure.
The World is a virtual terminal, not a host shell. The Overview's Terminal reference lists what persists between commands, what is supported, and the limits.