Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c25bbd

Browse files
stirbyClaude
and
Claude
committed
docs: improve guides for workspace build transitions
Add documentation explaining how to use the CreateWorkspaceBuildRequest API for common operations like starting, stopping, and deleting workspaces. Fixes #10910 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent e4c6c10 commit 2c25bbd

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-2
lines changed

docs/admin/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ environments for your developers, check out our
1212
recommend the [user guides](../user-guides/index.md).
1313

1414
For automation and scripting workflows, see our [CLI](../reference/cli/index.md)
15-
and [API](../reference/api/index.md) docs.
15+
and [API](../reference/api/index.md) docs. Common administrative API use cases are documented in the [Admin Use Cases](./use-cases/index.md) section.
1616

1717
For any information not strictly contained in these sections, check out our
1818
[Tutorials](../tutorials/index.md) and [FAQs](../tutorials/faqs.md).

docs/admin/use-cases/index.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Admin Use Cases
2+
3+
This section covers common administrative tasks and automation scenarios for Coder administrators.
4+
5+
## API Use Cases
6+
7+
- [Workspace Build Transitions](./workspace-build-transitions.md): Learn how to manage workspace state changes (start, stop, delete) via the API
8+
9+
<children></children>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Workspace Build Transitions
2+
3+
This guide explains how to perform common workspace operations like start, stop, and delete using the Coder API. Due to the nature of Terraform, these operations are executed as workspace build transitions rather than having dedicated endpoints.
4+
5+
## Overview of Workspace Builds
6+
7+
In Coder, all workspace operations that change the state of a workspace (e.g., start, stop, delete) are implemented as builds. A workspace build is a Terraform operation (`terraform apply` or `terraform destroy`) that provisions or destroys workspace resources.
8+
9+
Workspace builds are created by sending a `CreateWorkspaceBuildRequest` to the `/workspaces/{workspace}/builds` endpoint. The `transition` parameter specifies which operation to perform.
10+
11+
## Workspace State Transitions
12+
13+
Workspaces can have the following transitions:
14+
15+
| Transition | Description |
16+
|------------|-------------|
17+
| `start` | Start a stopped workspace |
18+
| `stop` | Stop a running workspace |
19+
| `delete` | Delete a workspace |
20+
21+
## Common Use Cases
22+
23+
### Starting a Workspace
24+
25+
To start a workspace, create a build with the `start` transition:
26+
27+
```shell
28+
curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \
29+
-H "Content-Type: application/json" \
30+
-H "Coder-Session-Token: {your_token}" \
31+
-d '{
32+
"transition": "start"
33+
}'
34+
```
35+
36+
### Stopping a Workspace
37+
38+
To stop a workspace, create a build with the `stop` transition:
39+
40+
```shell
41+
curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \
42+
-H "Content-Type: application/json" \
43+
-H "Coder-Session-Token: {your_token}" \
44+
-d '{
45+
"transition": "stop"
46+
}'
47+
```
48+
49+
### Deleting a Workspace
50+
51+
To delete a workspace, create a build with the `delete` transition:
52+
53+
```shell
54+
curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \
55+
-H "Content-Type: application/json" \
56+
-H "Coder-Session-Token: {your_token}" \
57+
-d '{
58+
"transition": "delete"
59+
}'
60+
```
61+
62+
### Advanced: Deleting a Workspace Without Destroying Resources (Orphaning)
63+
64+
In some cases, administrators might need to delete a workspace from Coder's database without destroying the underlying infrastructure resources:
65+
66+
```shell
67+
curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \
68+
-H "Content-Type: application/json" \
69+
-H "Coder-Session-Token: {your_token}" \
70+
-d '{
71+
"transition": "delete",
72+
"orphan": true
73+
}'
74+
```
75+
76+
**Warning**: Use this option with caution as it will leave cloud resources running without being tracked by Coder, potentially leading to unaccounted costs.
77+
78+
### Updating a Workspace with a New Template Version
79+
80+
To update a workspace to use a new template version:
81+
82+
```shell
83+
curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \
84+
-H "Content-Type: application/json" \
85+
-H "Coder-Session-Token: {your_token}" \
86+
-d '{
87+
"transition": "start",
88+
"template_version_id": "{new_template_version_id}"
89+
}'
90+
```
91+
92+
## Monitoring Build Status
93+
94+
You can monitor the progress of a build using the returned build ID:
95+
96+
```shell
97+
curl "https://coder.example.com/api/v2/workspacebuilds/{build_id}" \
98+
-H "Coder-Session-Token: {your_token}"
99+
```
100+
101+
To fetch build logs:
102+
103+
```shell
104+
curl "https://coder.example.com/api/v2/workspacebuilds/{build_id}/logs?follow=true" \
105+
-H "Coder-Session-Token: {your_token}"
106+
```
107+
108+
## Using the Coder CLI
109+
110+
The Coder CLI provides a more user-friendly way to manage workspace transitions:
111+
112+
```shell
113+
# Start a workspace
114+
coder start my-workspace
115+
116+
# Stop a workspace
117+
coder stop my-workspace
118+
119+
# Delete a workspace
120+
coder delete my-workspace
121+
```
122+
123+
## Error Handling
124+
125+
Common errors when dealing with workspace builds include:
126+
127+
1. **Build already in progress**: Only one build can be active for a workspace at a time
128+
2. **Unauthorized**: The requesting user doesn't have access to perform the operation
129+
3. **Invalid state transition**: For example, trying to stop an already stopped workspace
130+
131+
Always check the status code and error message in the API response to handle these cases gracefully.

docs/reference/api/index.md

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)