Run terrafrom destroy by using the API on Terraform Cloud

I’m trying to run a terraform destroy through the API on TC, but I’m getting a not found error.

I base the curl command on the documentation

curl --request POST \
  --url https://app.terraform.io/api/v2/runs \
  --header 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXX' \
  --header 'Content-Type: application/vnd.api+json' \
  --data '{
  "type": "runs",
  "attributes": {
    "is-destroy":true,
    "message": "Triggered Destroy"
  },
  "relationships": {
    "workspace": {
      "data": {
        "type": "workspaces",
        "id": "XXXXXXXXXXXXX"
      }
    }
  }
}'

This is the output of the run:

{
  "errors": [
    {
      "status": "404",
      "title": "not found"
    }
  ]
}

The workspace and the infrastructure exists.
As this is a test, I’m getting the workspace ID directly from the TC UI, so I know its right. :slight_smile:

Any idea what could be wrong?

Hi, welcome to the forum!

If you’re sure the workspace exists, a 404 response could also mean an unauthorized API token. Be sure you’re using a valid User or Team API token with Write or Apply access to that workspace.

Thanks for the quick reply Chris.
I created the workspace using the API, so I don’t believe its the token.
Although, I’m using an organization token instead of a team token.

I was just reading the information on TC:

The organization API token is used to manage teams, team membership and workspaces. This token does not have permission to perform plans and applies in workspaces.

So probably this is my issue. I will test it and come back.