Developer tools

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

Last checked 2026-10-25 against HCP Terraform (Terraform Cloud)'s documentation

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.

HCP Terraform (Terraform Cloud) key types

TypePrefixSecret?ScopeExpiry
User token
Removing someone from an SSO identity provider does not revoke their user token. Owners can disable user tokens for the organization.
—YesSame permissions as the user accountChosen at creation; organization owners can set a maximum time to live
Team token
A team can hold several tokens with unique descriptions. Tokens without a description are legacy tokens, only one allowed.
—YesWorkspaces the team can access, not tied to a userHas an expiration; cannot be changed after creation
Organization token
One valid token per organization.
—YesCreate and configure workspaces and teams; cannot start runs or create configuration versionsHas an expiration; cannot be changed after creation
Audit trail token
One valid token per organization.
—YesRead audit trailsHas 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.

The user token topic describes no rotate button; the approach is create a new token and revoke the old one. HCP Terraform emails 30 days and 7 days before expiration.

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 your HCP Terraform (Terraform Cloud) 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 and CLI

HCP Terraform (Terraform Cloud)'s SDKs and docs read the key from TF_TOKEN_app_terraform_io, TF_CLI_CONFIG_FILE or TFE_TOKEN.

Terraform CLI (terraform): terraform login fetches credentials in a browser and saves them to the CLI configuration file (.terraformrc, or terraform.rc on Windows); TF_TOKEN_ environment variables take priority over it

Things that catch people out

  • 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.

Recent incidents that put HCP Terraform (Terraform Cloud) keys at risk

If you were affected by one of these, rotate your HCP Terraform (Terraform Cloud) keys using the rotation steps above.

Using your HCP Terraform (Terraform Cloud) key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a HCP Terraform (Terraform Cloud) key, these intents fit:

IntentWhat BotPassword does
list_credentialsLists the credentials this agent is granted and what it may do with each. Never values.
request_accessAsks 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_withRuns 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_requestMakes 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_loginLets 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.
rotatePartly 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_secretPlanned, not built yet: will read a newly created key straight from the page or command output into the vault.
store_secretSaves a value the agent already holds as a new vault item.

HCP Terraform (Terraform Cloud) also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

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.

Stop pasting HCP Terraform (Terraform Cloud) keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.