# HCP Terraform (Terraform Cloud) API tokens: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/terraform-cloud
Last checked: 2026-10-25

HCP Terraform issues user tokens (Account settings → Tokens), team tokens, organization tokens and audit trail tokens, each shown only once and each with an expiration date. Terraform CLI picks a token up from `terraform login` or from a `TF_TOKEN_<host>` environment variable such as `TF_TOKEN_app_terraform_io`.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| User token | — | Yes | Same permissions as the user account | Chosen at creation; organization owners can set a maximum time to live |
| Team token | — | Yes | Workspaces the team can access, not tied to a user | Has an expiration; cannot be changed after creation |
| Organization token | — | Yes | Create and configure workspaces and teams; cannot start runs or create configuration versions | Has an expiration; cannot be changed after creation |
| Audit trail token | — | Yes | Read audit trails | Has an expiration |

## How to create a HCP Terraform (Terraform Cloud) key

1. Click your user icon and choose **Account settings**, then **Tokens**.
2. Click **Create an API token** and enter a description.
3. Click **Create API token** and choose an expiration.
4. Copy the token; HCP Terraform shows it only once.

## Limiting what the key can do

User tokens carry all of the user's permissions, so prefer team tokens, which reach only the workspaces a team can access, for automation. Organization, audit trail and metrics service tokens have narrow, fixed purposes. Owners can set a maximum token lifetime in organization settings.

## How to rotate a HCP Terraform (Terraform Cloud) key

1. Create a new token with the same type and owner.
2. Update `TF_TOKEN_app_terraform_io`, CI secrets and credentials blocks.
3. Delete the old token.

## How to revoke a HCP Terraform (Terraform Cloud) key

1. For a user token click the trash can icon next to it under **Account settings** → **Tokens**.
2. For a team token delete it from the row's actions menu under **Organization Settings** → **API Tokens** → **Team Tokens**.
3. Organization and audit trail tokens can be generated or revoked only by organization owners.

## If the key leaked

1. Revoke the token immediately.
2. Create a replacement with the shortest practical lifetime, preferring a team token over a user token.
3. Review recent runs, workspace variables and state access for unexpected activity.

## Environment variables

- `TF_TOKEN_app_terraform_io`
- `TF_CLI_CONFIG_FILE`
- `TFE_TOKEN`

## Gotchas

- The variable is `TF_TOKEN_` plus the hostname with periods turned into underscores; hyphens are encoded as double underscores (Terraform v1.2.0 and later).
- A `credentials` block token must be a user or team token, not an organization token.
- The Terraform MCP server reads `TFE_TOKEN` and `TFE_ADDRESS`, and keeps destructive operations off unless `ENABLE_TF_OPERATIONS` is enabled.
- Agent API tokens are for agent pools only and are not valid for the HCP Terraform API.

## Using this key with AI agents (BotPassword)

- `list_credentials`: Lists the credentials this agent is granted and what it may do with each. Never values.
- `request_access`: Asks you for a grant, which you approve with your master password or decline in BotPassword on your desktop, never in the chat. The agent gets a receipt, never a value. Approval on your phone is planned.
- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `http_request`: Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.
- `cli_login`: Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned.
- `rotate`: Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
- `capture_secret`: Planned, not built yet: will read a newly created key straight from the page or command output into the vault.
- `store_secret`: Saves a value the agent already holds as a new vault item.

## FAQ

### Where do I create an HCP Terraform API token?

Click your user icon → **Account settings** → **Tokens** → **Create an API token**.

### What is the environment variable for a Terraform Cloud token?

`TF_TOKEN_app_terraform_io` for the default host, following the `TF_TOKEN_` plus host name pattern with periods as underscores.

### Why does Terraform say the Terraform Cloud API token is not set?

Terraform has no credentials for the host; run `terraform login` or set the `TF_TOKEN_` environment variable.

### Do HCP Terraform tokens expire?

Yes. User, team and organization tokens all have an expiration, and expired tokens fail all API requests.

### Can an AI agent use my HCP Terraform (Terraform Cloud) key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `list_credentials`, `run_with`, `http_request`, so the agent gets the result and never the value.

## Sources

- https://developer.hashicorp.com/terraform/cloud-docs/users-teams-organizations/api-tokens
- https://developer.hashicorp.com/terraform/cloud-docs/users-teams-organizations/users
- https://developer.hashicorp.com/terraform/cli/config/config-file
- https://developer.hashicorp.com/terraform/mcp-server/reference
