Fileoverview

SGTM Containers Edge Function

Manages Server-side Google Tag Manager container lifecycle including creation, infrastructure provisioning, and verification.

Endpoint

POST /sgtm-containers - Create a new container with infrastructure

Endpoint

POST /sgtm-containers/:id/verify - Re-verify an existing container

Auth

Required - JWT in Authorization header

Remarks

This is the main orchestration function for container management:

Create Flow (POST /sgtm-containers):

  1. Validates user permissions (admin/owner role required)
  2. Parses and validates GTM container configuration
  3. Creates container record in database
  4. Allocates ports from port_pool
  5. Calls Infrastructure Provisioner to set up Caddy, DNS, etc.
  6. Creates site and site_domains records for each domain

Verify Flow (POST /sgtm-containers/:id/verify):

  1. Fetches existing container
  2. Validates user permissions
  3. Calls Cloudflare Worker to verify GTM configuration
  4. Updates container status based on verification result

Tables touched:

  • sgtm_containers (read/write)
  • client_memberships (read)
  • port_pool (read/write via admin client)
  • sites (write)
  • site_domains (write)
  • clients (read)

External services:

  • Infrastructure Provisioner (INFRA_PROVISIONER_URL)
  • Cloudflare GTM Worker (CLOUDFLARE_GTM_WORKER_URL)

Example: Create request body

{
"clientId": "uuid",
"name": "My Container",
"domains": ["example.com", "shop.example.com"],
"webGtmIds": { "example.com": "GTM-XXXX" },
"containerConfigB64": "base64-encoded-config"
}

Returns

200 - Success with container details

Returns

400 - Invalid request (missing fields, invalid config)

Returns

401 - Unauthorized

Returns

403 - Insufficient permissions

Returns

404 - Container not found (verify endpoint)

Returns

405 - Method not allowed

Returns

500 - Internal server error or infrastructure failure

Returns

503 - No available ports