Skip to content

Instantly share code, notes, and snippets.

@leon-do
Created March 9, 2025 03:35
Show Gist options
  • Save leon-do/b09c313a90495e3fb03b974fc8575421 to your computer and use it in GitHub Desktop.
Save leon-do/b09c313a90495e3fb03b974fc8575421 to your computer and use it in GitHub Desktop.
Deploy Next.js app to Netlify via API
import { NetlifyAPI } from "netlify";
// https://app.netlify.com/user/applications#personal-access-tokens
const client = new NetlifyAPI("nfp_YGNVq");
async function main() {
// create empty site
const site = await client.createSite().catch((err) => console.log(err));
if (!site) return;
console.log(site);
// https://open-api.netlify.com/#tag/environmentVariables/operation/createEnvVars
await client
.createEnvVars({
account_id: site.account_id as string,
site_id: site.id,
body: [
{
key: "NEXT_PUBLIC_VAR",
values: [{ value: "NEXT_PUBLIC_VALUE" }],
},
],
})
.catch((err) => console.log(err));
// https://open-api.netlify.com/#/default/createSite
await client
.updateSite({
site_id: site.id as string,
body: {
name: `my-awesome-site-abcdef`,
repo: {
provider: "gitlab",
repo_path: "leon-do/next-env-lab",
repo_branch: "main",
dir: ".next",
cmd: "npm run build",
},
},
})
.catch((err) => console.log(err));
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment