| API Workflow | Description |
|---|---|
| CI/CD Integration | Trigger Terraform runs from pipelines |
| State & Data Query | Programmatically retrieve Terraform state and outputs |
| Self-Service Portals | Build user-facing interfaces for provisioning |
| Custom CLI Automation | Enhance scripts with API-driven capabilities |
- Authenticate via the CLI
- Configure workspace variables
- Initiate a Terraform run using the API

1. Authenticate and Prepare
Log in to Terraform Cloud via CLI to obtain your API credentials:terraform login opens a browser for authentication and saves your token in ~/.terraform.d/credentials.tfrc.json.
MYORGNAME with your org):
Keep your
TOKEN secure. Do not commit it to source control or share it publicly.2. Retrieve the Workspace ID
Every Terraform Cloud workspace has a unique ID. Store it in an environment variable:
3. Define Variable Payloads
Create three JSON payloads in thejson/ directory to configure workspace variables:
| File | Key | Value | Category | Sensitive | HCL |
|---|---|---|---|---|---|
placeholder.json | placeholder | placedog.net | terraform | false | false |
width.json | width | 800 | terraform | false | false |
height.json | height | 600 | terraform | false | false |
POST /workspaces/:id/vars endpoint:

4. Create Workspace Variables via API
Switch into the JSON directory and run the followingcurl commands:

5. Trigger a Terraform Run via API
Prepare theapply.json payload to create a run:
REPLACEME with your $WORKSPACE_ID:
On macOS, use
sed -i '' for inline replacements.
Conclusion
You have successfully:- Authenticated to Terraform Cloud via CLI
- Used the Workspace Variables API to set variables
- Triggered a Terraform run through the Runs API