> ## Documentation Index
> Fetch the complete documentation index at: https://docs.envzero.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Download a state file

> Download an environment's Terraform or OpenTofu state file from the env zero remote backend API with a token, an environment ID, and a state version ID.

Download an environment's state file from the env zero remote backend API. Use it to read state from a script, across many environments, or from a pipeline outside env zero.

The remote backend API is separate from the REST API in the rest of this reference. It is served from `https://backend.api.env0.com`, and it authenticates with a remote backend token instead of HTTP Basic authentication.

## Prerequisites

* The environment stores its state in the [remote backend](https://docs.envzero.com/guides/admin-guide/remote-backend.md) and has at least one state version.
* A remote backend token. [Generate a token](https://docs.envzero.com/guides/admin-guide/remote-backend/login.md#generating-a-token) from a personal API key, or from an admin or user API key.
* The **Read State** permission on the environment. A [read-only API key](https://docs.envzero.com/guides/admin-guide/user-role-and-team-management/api-keys.md#read-only-api-key) keeps it. See [custom roles](https://docs.envzero.com/guides/admin-guide/user-role-and-team-management/custom-roles.md) for the permission.
* The environment ID. See [Finding the environment ID](https://docs.envzero.com/guides/admin-guide/environments.md#finding-the-environment-id).

> [!WARNING]
>
> State files can contain secret values in plain text. Store the downloaded file as you would a secret.

## Download the current state

Two requests download the state. The first returns the ID of the current state version. The second downloads that version.

**1. Export the token**

```bash theme={null}
export REMOTE_BACKEND_TOKEN="<YOUR_TOKEN>"
```

**2. Get the current state version ID**

```bash theme={null}
curl -s -H "Authorization: Bearer $REMOTE_BACKEND_TOKEN" \
  https://backend.api.env0.com/api/v2/workspaces/<ENVIRONMENT_ID>/current-state-version
```

The `id` in the response is the state version ID. `hosted-state-download-url` is the full URL for the next request:

```json theme={null}
{
  "data": {
    "id": "<VERSION_ID>",
    "type": "state-versions",
    "attributes": {
      "hosted-state-download-url": "https://backend.api.env0.com/api/v2/workspaces/<ENVIRONMENT_ID>/versions/<VERSION_ID>/download-state"
    }
  }
}
```

The `attributes` object can contain more fields, such as `serial`.

**3. Download the state file**

```bash theme={null}
curl -s -H "Authorization: Bearer $REMOTE_BACKEND_TOKEN" \
  -o terraform.tfstate \
  https://backend.api.env0.com/api/v2/workspaces/<ENVIRONMENT_ID>/versions/<VERSION_ID>/download-state
```

The response body is the state file as stored, with the content type `application/json`.

To download a previous state version, use the **States** tab, as described on the [Remote backend](https://docs.envzero.com/guides/admin-guide/remote-backend.md) page.

## Download state for many environments

To download the current state of several environments, repeat both requests for each environment ID. Get the IDs from [List Environments](https://docs.envzero.com/api-reference/environments/list-environments.md).

```bash theme={null}
BASE_URL="https://backend.api.env0.com/api/v2/workspaces"

for ENVIRONMENT_ID in <ENVIRONMENT_ID_1> <ENVIRONMENT_ID_2>; do
  VERSION_ID=$(curl -s -H "Authorization: Bearer $REMOTE_BACKEND_TOKEN" \
    "$BASE_URL/$ENVIRONMENT_ID/current-state-version" | jq -r '.data.id')

  curl -s -H "Authorization: Bearer $REMOTE_BACKEND_TOKEN" \
    -o "$ENVIRONMENT_ID.tfstate" \
    "$BASE_URL/$ENVIRONMENT_ID/versions/$VERSION_ID/download-state"
done
```

## Endpoints

Both endpoints use the `GET` method and the base URL `https://backend.api.env0.com`.

| Path | Returns |
| :- | :- |
| `/api/v2/workspaces/<ENVIRONMENT_ID>/current-state-version` | The metadata of the current state version, including its ID. |
| `/api/v2/workspaces/<ENVIRONMENT_ID>/versions/<VERSION_ID>/download-state` | The state file of that version. |

| Status | Meaning |
| :- | :- |
| `401` | The token is missing or invalid, or the request comes from an address outside an [IP allowlist](https://docs.envzero.com/guides/overview/security-overview/ip-allowlisting.md). |
| `403` | The token does not have the **Read State** permission on the environment. |
| `404` | The environment does not exist or has no state. |

## Related pages

* [State access control](https://docs.envzero.com/guides/admin-guide/remote-backend/state-access-control.md) sets which environments can read state during env zero deployments. Requests with an API key use the key's permissions instead.
* [API access](https://docs.envzero.com/guides/community-and-resources/support-and-help/api-access.md) covers authentication for the rest of the REST API.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.