param( [ValidateSet("GET", "POST", "PUT", "PATCH", "DELETE")] [string]$Method = "GET", [Parameter(Mandatory = $true)] [string]$Path, [string]$BodyJson, [switch]$Raw ) $ErrorActionPreference = "Stop" $baseUrl = if ($env:COOLIFY_API_URL) { $env:COOLIFY_API_URL } else { "https://coolify.urieljareth.org/api/v1" } if ([string]::IsNullOrWhiteSpace($env:COOLIFY_TOKEN)) { throw "Set COOLIFY_TOKEN before calling the Coolify API." } $baseUrl = $baseUrl.TrimEnd("/") $cleanPath = if ($Path.StartsWith("/")) { $Path } else { "/$Path" } $uri = "$baseUrl$cleanPath" $headers = @{ Authorization = "Bearer $($env:COOLIFY_TOKEN)" Accept = "application/json" } $request = @{ Method = $Method Uri = $uri Headers = $headers TimeoutSec = 240 } if ($PSBoundParameters.ContainsKey("BodyJson")) { try { $null = $BodyJson | ConvertFrom-Json } catch { throw "BodyJson is not valid JSON: $($_.Exception.Message)" } $request.Body = $BodyJson $request.ContentType = "application/json" } try { $response = Invoke-RestMethod @request } catch { throw "Coolify API request failed: $($_.Exception.Message)" } if ($Raw) { $response } else { $response | ConvertTo-Json -Depth 50 }