Netlify Client
TypeScript client for the Netlify API.
Installation
Section titled “Installation”pnpm add @theholocron/netlify-clientimport { createNetlifyClient } from "@theholocron/netlify-client";
const netlify = createNetlifyClient({ token: process.env.NETLIFY_AUTH_TOKEN!,});
// List sitesconst sites = await netlify.sites.list();
// Get a site by idconst site = await netlify.sites.get("site-id");
// Create a site — scoped to /{account_slug}/sites, not account idconst created = await netlify.sites.create("my-team-slug", { name: "my-site" });
// Patch a site (e.g. attach a custom domain)await netlify.sites.update("site-id", { custom_domain: "example.com" });
// Trigger a build from a git-linked site's default branchconst { deploy_id } = await netlify.sites.triggerBuild("site-id");
// Create a deploy — static files (plain text) and/or functions (pre-built// zips). This is the digest-based flow: real, invokable functions — not// the single-whole-zip method, which only deploys static assets.import { buildZip } from "@theholocron/netlify-client";
const functionZip = buildZip({ "webhook.mjs": "exports.handler = async () => ({ statusCode: 200, body: 'ok' });",});const deploy = await netlify.deploys.create("site-id", { files: { "index.html": "<!doctype html><title>status</title>" }, functions: [{ name: "webhook", zip: functionZip }],});
// Get a deploy by idconst status = await netlify.deploys.get(deploy.id);
// List account-scoped env vars for a siteconst vars = await netlify.env.list("account-id", "site-id");
// Set (upsert) one value for one context on a key — Netlify's PUT /env/{key}// replaces the entire values array, so this reads the current list firstawait netlify.env.set("account-id", "site-id", "API_URL", "production", "https://api.example.com");- Functions need the digest flow — the single-zip method doesn’t deploy
functions at all. Confirmed live: a function file placed inside a
single whole-site zip (
POST .../deployswithContent-Type: application/zip) is served as an inert static asset, never wired up as a Lambda.deploys.create()’sfunctionsfield uses the real flow (SHA1 file manifest + SHA256 function manifest, thenPUT .../functions/{name}?runtime=js) — this is the only way to ship an actually-invokable function through this API. - A function’s zip entry file basename must exactly match the function
name. Confirmed live: naming the function
webhookbut the zip’s entry filehandler.mjsfails at invoke time withRuntime.ImportModuleError: Cannot find module 'webhook'. No nested paths either (unlike Vercel’sapi/webhook.mjs) — the file needs to sit at the zip root as<name>.mjs(or.js). runtime: "js"expects a classic CommonJS/Lambda handler, not the modern(Request) => Responsestyle. Confirmed live:export default (req) => new Response(...)deploys without error but fails at invoke time withRuntime.HandlerNotFound: <name>.handler is undefined or not exported. Useexports.handler = async (event, context) => ({ statusCode, headers, body })(or the ESM equivalent,export const handler = ...) instead.- No build step on a function upload. Netlify runs no
npm installor configured build command for a raw API-uploaded function, unlike a Git-linked deploy (ornetlify deployrun locally via the CLI). If the function imports anything beyond Node builtins, itszipneeds a realnode_modulesincluded — resolve dependencies into the zip yourself before callingcreate(). - Sites are account-slug-scoped to create, account-id-scoped for env
vars. Netlify’s own API is inconsistent here —
POST /{account_slug}/sitesbutGET/POST/PUT /accounts/{account_id}/env. Not something this client can paper over; pass whichever one each method asks for. The slug is also mutable (an account/team can rename it in settings) — don’t treat a previously-observed slug as permanent. stateon a deploy is a free-form string, not a published enum. Known values seen in practice:new,building,uploading,uploaded,preparing,prepared,processing,ready,error,retrying,current.
The full API reference is generated from the package’s TSDoc comments.