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)" } # Send bytes, not a string. PowerShell 5.1 encodes a string body using the # default codepage, so any non-ASCII character (an accented comment inside a # compose file, for instance) reaches Coolify mangled and it answers # 400 {"message":"Invalid request.","error":"Invalid JSON."}. $request.Body = [Text.Encoding]::UTF8.GetBytes($BodyJson) $request.ContentType = "application/json; charset=utf-8" } try { $response = Invoke-RestMethod @request } catch { throw "Coolify API request failed: $($_.Exception.Message)" } if ($Raw) { $response } else { $response | ConvertTo-Json -Depth 50 }