0

Create site

by
Published Oct 17, 2025

**Note:** Environment variable keys and values have moved from `build_settings.env` and `repo.env` to a new endpoint. Please use [createEnvVars](#tag/environmentVariables/operation/createEnvVars) to create environment variables for a site.

Script netlify Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Netlify = {
3
  token: string;
4
};
5
/**
6
 * Create site
7
 * **Note:** Environment variable keys and values have moved from `build_settings.env` and `repo.env` to a new endpoint. Please use [createEnvVars](#tag/environmentVariables/operation/createEnvVars) to create environment variables for a site.
8
 */
9
export async function main(
10
  auth: Netlify,
11
  configure_dns: string | undefined,
12
  body: {
13
    id?: string;
14
    state?: string;
15
    plan?: string;
16
    name?: string;
17
    custom_domain?: string;
18
    domain_aliases?: string[];
19
    branch_deploy_custom_domain?: string;
20
    deploy_preview_custom_domain?: string;
21
    password?: string;
22
    notification_email?: string;
23
    url?: string;
24
    ssl_url?: string;
25
    admin_url?: string;
26
    screenshot_url?: string;
27
    created_at?: string;
28
    updated_at?: string;
29
    user_id?: string;
30
    session_id?: string;
31
    ssl?: false | true;
32
    force_ssl?: false | true;
33
    managed_dns?: false | true;
34
    deploy_url?: string;
35
    published_deploy?: {
36
      id?: string;
37
      site_id?: string;
38
      user_id?: string;
39
      build_id?: string;
40
      state?: string;
41
      name?: string;
42
      url?: string;
43
      ssl_url?: string;
44
      admin_url?: string;
45
      deploy_url?: string;
46
      deploy_ssl_url?: string;
47
      screenshot_url?: string;
48
      review_id?: number;
49
      draft?: false | true;
50
      required?: string[];
51
      required_functions?: string[];
52
      error_message?: string;
53
      branch?: string;
54
      commit_ref?: string;
55
      commit_url?: string;
56
      skipped?: false | true;
57
      created_at?: string;
58
      updated_at?: string;
59
      published_at?: string;
60
      title?: string;
61
      context?: string;
62
      locked?: false | true;
63
      review_url?: string;
64
      framework?: string;
65
      function_schedules?: { name?: string; cron?: string }[];
66
    };
67
    account_id?: string;
68
    account_name?: string;
69
    account_slug?: string;
70
    git_provider?: string;
71
    deploy_hook?: string;
72
    capabilities?: {};
73
    processing_settings?: { html?: { pretty_urls?: false | true } };
74
    build_settings?: {
75
      id?: number;
76
      provider?: string;
77
      deploy_key_id?: string;
78
      repo_path?: string;
79
      repo_branch?: string;
80
      dir?: string;
81
      functions_dir?: string;
82
      cmd?: string;
83
      allowed_branches?: string[];
84
      public_repo?: false | true;
85
      private_logs?: false | true;
86
      repo_url?: string;
87
      env?: {};
88
      installation_id?: number;
89
      stop_builds?: false | true;
90
    };
91
    id_domain?: string;
92
    default_hooks_data?: { access_token?: string };
93
    build_image?: string;
94
    prerender?: string;
95
    functions_region?: string;
96
  } & {
97
    repo?: {
98
      id?: number;
99
      provider?: string;
100
      deploy_key_id?: string;
101
      repo_path?: string;
102
      repo_branch?: string;
103
      dir?: string;
104
      functions_dir?: string;
105
      cmd?: string;
106
      allowed_branches?: string[];
107
      public_repo?: false | true;
108
      private_logs?: false | true;
109
      repo_url?: string;
110
      env?: {};
111
      installation_id?: number;
112
      stop_builds?: false | true;
113
    };
114
  },
115
) {
116
  const url = new URL(`https://api.netlify.com/api/v1/sites`);
117
  for (const [k, v] of [["configure_dns", configure_dns]]) {
118
    if (v !== undefined && v !== "" && k !== undefined) {
119
      url.searchParams.append(k, v);
120
    }
121
  }
122
  const response = await fetch(url, {
123
    method: "POST",
124
    headers: {
125
      "Content-Type": "application/json",
126
      Authorization: "Bearer " + auth.token,
127
    },
128
    body: JSON.stringify(body),
129
  });
130
  if (!response.ok) {
131
    const text = await response.text();
132
    throw new Error(`${response.status} ${text}`);
133
  }
134
  return await response.json();
135
}
136