Skip to content

Provider omits /projects/ prefix for GCP-hosted Commercetools endpoints, causing 404 errors #619

@sooyp

Description

@sooyp

Version information

  • terraform: 1.2.2
  • terraform provider: labd/commercetools v1.21.1

Describe the bug

When applying Commercetools resources via Terraform, the provider returns 404 Not Found responses for all API calls.
This occurs because the provider constructs incorrect request URLs - it automatically prepends /projects/{projectKey} to the configured api_url, even when the URL already includes the project key.

For example, Terraform attempts:

POST https://api.us-central1.gcp.commercetools.com/projects/ct-sync-task/customer-groups

This responds with:

404 Not Found

However, the same request made manually via curl to the correct endpoint works:

POST https://api.us-central1.gcp.commercetools.com/ct-sync-task/customer-groups

This indicates the provider is injecting an unnecessary /projects/ prefix.

To Reproduce

  1. Provider configuration:
terraform {
  required_version = ">=1.2.2"

  required_providers {
    commercetools = {
      source  = "labd/commercetools"
      version = "~> 1.21.1"
    }
  }

  backend "s3" {
    bucket               = "app-tf-state-nonprod"
    workspace_key_prefix = "app"
    key                  = "terraform.tfstate"
    region               = "us-east-1"
    encrypt              = "true"
  }
}

provider "commercetools" {
  client_id     = var.commerce_tools_client_id
  client_secret = var.commerce_tools_client_secret
  project_key   = "ct-sync-task"
  api_url       = "https://api.us-central1.gcp.commercetools.com/ct-sync-task"
  token_url     = "https://auth.us-central1.gcp.commercetools.com/oauth/token"
}
  1. Create any Commercetools resource:
resource "commercetools_customer_group" "example" {
  name = "Test Group"
}
  1. Run
terraform apply
  1. Observe Terraform debug output (incorrect path):
POST https://api.us-central1.gcp.commercetools.com/projects/ct-sync-task/customer-groups
Response: 404 Not Found

Expected behavior

Terraform should call:

https://api.us-central1.gcp.commercetools.com/mxp-store-rave-ct-sync-task7/customer-groups

without injecting the /projects/ prefix. This matches Commercetools’ documented API structure and succeeds when tested manually using curl.

Screenshots

Incorrect URL generated by Terraform:

POST https://api.us-central1.gcp.commercetools.com/projects/ct-sync-task/customer-groups
Response: 404 Not Found

Working manual curl request:

curl -X POST https://api.us-central1.gcp.commercetools.com/ct-sync-task/customer-groups
# Response: 201 Created

Additional context

  • Authentication succeeds - OAuth tokens are issued correctly with valid scopes.
  • Issue occurs for multiple resource types (customer_group, type, product_type, etc.).
  • Removing /projects/ from the URL resolves the issue.
  • It appears the provider still assumes an older API path format.

Suggested fix

It appears the provider currently assumes the /projects/{projectKey} path should always be included. Adjusting this logic so that the api_url represents the root API domain (e.g., https://api.us-central1.gcp.commercetools.com) and /projects/{projectKey} is appended dynamically could resolve the issue and ensure compatibility with regional Commercetools endpoints.

Happy to provide debug logs or test a patch version if needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions