diff --git a/lib/coolify.ts b/lib/coolify.ts index c95efce..42671c0 100644 --- a/lib/coolify.ts +++ b/lib/coolify.ts @@ -576,10 +576,26 @@ export async function upsertServiceEnv( uuid: string, env: ServiceEnvVar, ): Promise { - await coolifyFetch(`/services/${uuid}/envs`, { - method: 'POST', - body: JSON.stringify(env), - }); + // Coolify auto-creates env entries (with empty values) for every + // ${VAR} reference in the compose file at service-creation time. + // POST to /envs returns "already exists" for those — we must use + // PATCH to update them. Try POST first, fall back to PATCH. + try { + await coolifyFetch(`/services/${uuid}/envs`, { + method: 'POST', + body: JSON.stringify(env), + }); + } catch (err: unknown) { + const msg = err instanceof Error ? err.message : String(err); + if (msg.includes('already exists')) { + await coolifyFetch(`/services/${uuid}/envs`, { + method: 'PATCH', + body: JSON.stringify(env), + }); + return; + } + throw err; + } } export async function listAllServices(): Promise>> {