0

Create Machine

by
Published Oct 17, 2025

Create a Machine within a specific app using the details provided in the request body. **Important**: This request can fail, and you’re responsible for handling that failure. If you ask for a large Machine, or a Machine in a region we happen to be at capacity for, you might need to retry the request, or to fall back to another region. If you’re working directly with the Machines API, you’re taking some responsibility for your own orchestration!

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 Machine
7
 * Create a Machine within a specific app using the details provided in the request body.
8

9
**Important**: This request can fail, and you’re responsible for handling that failure. If you ask for a large Machine, or a Machine in a region we happen to be at capacity for, you might need to retry the request, or to fall back to another region. If you’re working directly with the Machines API, you’re taking some responsibility for your own orchestration!
10

11
 */
12
export async function main(
13
  auth: Fly,
14
  app_name: string,
15
  body: {
16
    config?: {
17
      auto_destroy?: false | true;
18
      checks?: {};
19
      containers?: {
20
        cmd?: string[];
21
        depends_on?: {
22
          condition?: "exited_successfully" | "healthy" | "started";
23
          name?: string;
24
        }[];
25
        entrypoint?: string[];
26
        env?: {};
27
        env_from?: {
28
          env_var?: string;
29
          field_ref?:
30
            | "id"
31
            | "version"
32
            | "app_name"
33
            | "private_ip"
34
            | "region"
35
            | "image";
36
        }[];
37
        exec?: string[];
38
        files?: {
39
          guest_path?: string;
40
          image_config?: string;
41
          mode?: number;
42
          raw_value?: string;
43
          secret_name?: string;
44
        }[];
45
        healthchecks?: {
46
          exec?: { command?: string[] };
47
          failure_threshold?: number;
48
          grace_period?: number;
49
          http?: {
50
            headers?: { name?: string; values?: string[] }[];
51
            method?: string;
52
            path?: string;
53
            port?: number;
54
            scheme?: "http" | "https";
55
            tls_server_name?: string;
56
            tls_skip_verify?: false | true;
57
          };
58
          interval?: number;
59
          kind?: "readiness" | "liveness";
60
          name?: string;
61
          success_threshold?: number;
62
          tcp?: { port?: number };
63
          timeout?: number;
64
          unhealthy?: "stop";
65
        }[];
66
        image?: string;
67
        name?: string;
68
        restart?: {
69
          gpu_bid_price?: number;
70
          max_retries?: number;
71
          policy?: "no" | "always" | "on-failure" | "spot-price";
72
        };
73
        secrets?: { env_var?: string; name?: string }[];
74
        stop?: { signal?: string; timeout?: { "time.Duration"?: number } };
75
        user?: string;
76
      }[];
77
      disable_machine_autostart?: false | true;
78
      dns?: {
79
        dns_forward_rules?: { addr?: string; basename?: string }[];
80
        hostname?: string;
81
        hostname_fqdn?: string;
82
        nameservers?: string[];
83
        options?: { name?: string; value?: string }[];
84
        searches?: string[];
85
        skip_registration?: false | true;
86
      };
87
      env?: {};
88
      files?: {
89
        guest_path?: string;
90
        image_config?: string;
91
        mode?: number;
92
        raw_value?: string;
93
        secret_name?: string;
94
      }[];
95
      guest?: {
96
        cpu_kind?: string;
97
        cpus?: number;
98
        gpu_kind?: string;
99
        gpus?: number;
100
        host_dedication_id?: string;
101
        kernel_args?: string[];
102
        memory_mb?: number;
103
      };
104
      image?: string;
105
      init?: {
106
        cmd?: string[];
107
        entrypoint?: string[];
108
        exec?: string[];
109
        kernel_args?: string[];
110
        swap_size_mb?: number;
111
        tty?: false | true;
112
      };
113
      metadata?: {};
114
      metrics?: { https?: false | true; path?: string; port?: number };
115
      mounts?: {
116
        add_size_gb?: number;
117
        encrypted?: false | true;
118
        extend_threshold_percent?: number;
119
        name?: string;
120
        path?: string;
121
        size_gb?: number;
122
        size_gb_limit?: number;
123
        volume?: string;
124
      }[];
125
      processes?: {
126
        cmd?: string[];
127
        entrypoint?: string[];
128
        env?: {};
129
        env_from?: {
130
          env_var?: string;
131
          field_ref?:
132
            | "id"
133
            | "version"
134
            | "app_name"
135
            | "private_ip"
136
            | "region"
137
            | "image";
138
        }[];
139
        exec?: string[];
140
        ignore_app_secrets?: false | true;
141
        secrets?: { env_var?: string; name?: string }[];
142
        user?: string;
143
      }[];
144
      restart?: {
145
        gpu_bid_price?: number;
146
        max_retries?: number;
147
        policy?: "no" | "always" | "on-failure" | "spot-price";
148
      };
149
      schedule?: string;
150
      services?: {
151
        autostart?: false | true;
152
        autostop?: "stop" | "off" | "suspend";
153
        checks?: {
154
          grace_period?: { "time.Duration"?: number };
155
          headers?: { name?: string; values?: string[] }[];
156
          interval?: { "time.Duration"?: number };
157
          method?: string;
158
          path?: string;
159
          port?: number;
160
          protocol?: string;
161
          timeout?: { "time.Duration"?: number };
162
          tls_server_name?: string;
163
          tls_skip_verify?: false | true;
164
          type?: string;
165
        }[];
166
        concurrency?: {
167
          hard_limit?: number;
168
          soft_limit?: number;
169
          type?: string;
170
        };
171
        force_instance_description?: string;
172
        force_instance_key?: string;
173
        internal_port?: number;
174
        min_machines_running?: number;
175
        ports?: {
176
          end_port?: number;
177
          force_https?: false | true;
178
          handlers?: string[];
179
          http_options?: {
180
            compress?: false | true;
181
            h2_backend?: false | true;
182
            headers_read_timeout?: number;
183
            idle_timeout?: number;
184
            response?: { headers?: {}; pristine?: false | true };
185
          };
186
          port?: number;
187
          proxy_proto_options?: { version?: string };
188
          start_port?: number;
189
          tls_options?: {
190
            alpn?: string[];
191
            default_self_signed?: false | true;
192
            versions?: string[];
193
          };
194
        }[];
195
        protocol?: string;
196
      }[];
197
      size?: string;
198
      standbys?: string[];
199
      statics?: {
200
        guest_path: string;
201
        index_document?: string;
202
        tigris_bucket?: string;
203
        url_prefix: string;
204
      }[];
205
      stop_config?: { signal?: string; timeout?: { "time.Duration"?: number } };
206
    };
207
    lease_ttl?: number;
208
    lsvd?: false | true;
209
    min_secrets_version?: number;
210
    name?: string;
211
    region?: string;
212
    skip_launch?: false | true;
213
    skip_secrets?: false | true;
214
    skip_service_registration?: false | true;
215
  },
216
) {
217
  const url = new URL(`https://api.machines.dev/v1/apps/${app_name}/machines`);
218

219
  const response = await fetch(url, {
220
    method: "POST",
221
    headers: {
222
      "Content-Type": "application/json",
223
      Authorization: "Bearer " + auth.token,
224
    },
225
    body: JSON.stringify(body),
226
  });
227
  if (!response.ok) {
228
    const text = await response.text();
229
    throw new Error(`${response.status} ${text}`);
230
  }
231
  return await response.json();
232
}
233