0

Create Lease

by
Published Oct 17, 2025

Create a lease for a specific Machine within an app using the details provided in the request body. Machine leases can be used to obtain an exclusive lock on modifying a Machine.

Script fly Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Fly = {
3
  token: string;
4
};
5
/**
6
 * Create Lease
7
 * Create a lease for a specific Machine within an app using the details provided in the request body. Machine leases can be used to obtain an exclusive lock on modifying a Machine.
8

9
 */
10
export async function main(
11
  auth: Fly,
12
  app_name: string,
13
  machine_id: string,
14
  body: { description?: string; ttl?: number },
15
  fly_machine_lease_nonce?: string,
16
) {
17
  const url = new URL(
18
    `https://api.machines.dev/v1/apps/${app_name}/machines/${machine_id}/lease`,
19
  );
20

21
  const response = await fetch(url, {
22
    method: "POST",
23
    headers: {
24
      ...(fly_machine_lease_nonce
25
        ? { "fly-machine-lease-nonce": fly_machine_lease_nonce }
26
        : {}),
27
      "Content-Type": "application/json",
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: JSON.stringify(body),
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38