State Versions API
Create a State Version
Hands-on: Try the Version Remote State with the Terraform Cloud API tutorial on HashiCorp Learn.
POST /workspaces/:workspace_id/state-versions
Parameter | Description |
---|---|
:workspace_id | The workspace ID to create the new state version in. Obtain this from the workspace settings or the Show Workspace endpoint. |
Creates a state version and sets it as the current state version for the given workspace. The workspace must be locked by the user creating a state version. The workspace may be locked with the API or with the UI. This is most useful for migrating existing state from open source Terraform into a new Terraform Cloud workspace.
Creating state versions requires permission to read and write state versions for the workspace. (More about permissions.)
Warning: Use caution when uploading state to workspaces that have already performed Terraform runs. Replacing state improperly can result in orphaned or duplicated infrastructure resources.
Note: For Free Tier organizations, Terraform Cloud always retains at least the last 100 states (across all workspaces) and at least the most recent state for every workspace. Additional states beyond the last 100 are retained for six months, and are then deleted.
Note: This endpoint cannot be accessed with organization tokens. You must access it with a user token or team token.
Status | Response | Reason |
---|---|---|
201 | JSON API document | Successfully created a state version. |
404 | JSON API error object | Workspace not found, or user unauthorized to perform action. |
409 | JSON API error object | Conflict; check the error object for more information. |
412 | JSON API error object | Precondition failed; check the error object for more information. |
422 | JSON API error object | Malformed request body (missing attributes, wrong types, etc.). |
Request Body
This POST endpoint requires a JSON object with the following properties as a request payload.
Properties without a default value are required.
Key path | Type | Default | Description |
---|---|---|---|
data.type | string | Must be "state-versions" . | |
data.attributes.serial | integer | The serial of the state version. Must match the serial value extracted from the raw state file. | |
data.attributes.md5 | string | An MD5 hash of the raw state version | |
data.attributes.lineage | string | (nothing) | Optional Lineage of the state version. Should match the lineage extracted from the raw state file. Early versions of terraform did not have the concept of lineage, so this is an optional attribute. |
data.attributes.state | string | Base64 encoded raw state file | |
data.relationships.run.data.id | string | (nothing) | Optional The ID of the run to associate with the state version. |
Sample Payload
Sample Request
Sample Response
Note: The hosted-state-download-url
attribute provides a url from which you can download the raw state.
List State Versions for a Workspace
GET /state-versions
Listing state versions requires permission to read state versions for the workspace. (More about permissions.)
Query Parameters
This endpoint supports pagination with standard URL query parameters. Remember to percent-encode [
as %5B
and ]
as %5D
if your tooling doesn't automatically encode URLs.
Parameter | Description |
---|---|
filter[workspace][name] | Required The name of one workspace to list versions for. |
filter[organization][name] | Required The name of the organization that owns the desired workspace. |
page[number] | Optional. If omitted, the endpoint will return the first page. |
page[size] | Optional. If omitted, the endpoint will return 20 state versions per page. |
Sample Request
Sample Response
Note: The hosted-state-download-url
attribute provides a url from which you can download the raw state.
Fetch the Current State Version for a Workspace
GET /workspaces/:workspace_id/current-state-version
Parameter | Description |
---|---|
:workspace_id | The ID for the workspace whose current state version you want to fetch. Obtain this from the workspace settings or the Show Workspace endpoint. |
Fetches the current state version for the given workspace. This state version will be the input state when running terraform operations.
Viewing state versions requires permission to read state versions for the workspace. (More about permissions.)
Status | Response | Reason |
---|---|---|
200 | JSON API document | Successfully returned current state version for the given workspace. |
404 | JSON API error object | Workspace not found, workspace does not have a current state version, or user unauthorized to perform action. |
Sample Request
Sample Response
Note: The hosted-state-download-url
attribute provides a url from which you can download the raw state.
Show a State Version
GET /state-versions/:state_version_id
Viewing state versions requires permission to read state versions for the workspace. (More about permissions.)
Parameter | Description |
---|---|
:state_version_id | The ID of the desired state version. |
Status | Response | Reason |
---|---|---|
200 | JSON API document | Successfully returned current state version for the given workspace. |
404 | JSON API error object | Workspace not found, workspace does not have a current state version, or user unauthorized to perform action. |
Sample Request
Sample Response
Note: The hosted-state-download-url
attribute provides a url from which you can download the raw state.
List State Version Outputs
The output values from a state version are also available via the API. For details, see the state version outputs documentation.
Available Related Resources
The GET endpoints above can optionally return related resources, if requested with the include
query parameter. The following resource types are available:
created_by
- The user that created the state version. For state versions created via a run executed by Terraform Cloud, this is an internal user account.run
- The run that created the state version, if applicable.run.created_by
- The user that manually triggered the run, if applicable.run.configuration_version
- The configuration version used in the run.outputs
- The parsed outputs for this state version.