What is netlify?
The netlify npm package provides a command-line interface (CLI) for interacting with Netlify's platform. It allows developers to deploy sites, manage DNS settings, configure build settings, and more, directly from the terminal.
What are netlify's main functionalities?
Deploy a site
This feature allows you to deploy a site to Netlify. You need to provide your access token, site ID, and the directory path of your site.
const netlify = require('netlify');
async function deploySite() {
const client = new netlify.NetlifyAPI('YOUR_ACCESS_TOKEN');
const site = await client.deploy({
siteId: 'YOUR_SITE_ID',
dir: 'path/to/your/site'
});
console.log('Site deployed:', site);
}
deploySite();
Create a new site
This feature allows you to create a new site on Netlify. You need to provide your access token and the desired name for the new site.
const netlify = require('netlify');
async function createSite() {
const client = new netlify.NetlifyAPI('YOUR_ACCESS_TOKEN');
const site = await client.createSite({
body: {
name: 'my-new-site'
}
});
console.log('New site created:', site);
}
createSite();
List all sites
This feature allows you to list all the sites associated with your Netlify account. You need to provide your access token.
const netlify = require('netlify');
async function listSites() {
const client = new netlify.NetlifyAPI('YOUR_ACCESS_TOKEN');
const sites = await client.listSites();
console.log('All sites:', sites);
}
listSites();
Other packages similar to netlify
vercel
The vercel npm package provides a CLI for interacting with Vercel's platform. It allows developers to deploy sites, manage projects, and configure settings. Vercel is similar to Netlify in that it offers serverless functions, continuous deployment, and a global CDN.
firebase-tools
The firebase-tools npm package provides a CLI for interacting with Firebase services. It allows developers to deploy web apps, manage databases, and configure hosting settings. Firebase Hosting is similar to Netlify in that it offers static site hosting, serverless functions, and continuous deployment.
aws-cli
The aws-cli npm package provides a CLI for interacting with Amazon Web Services. It allows developers to manage S3 buckets, deploy Lambda functions, and configure CloudFront distributions. AWS offers a more extensive range of services compared to Netlify, but it requires more configuration and management.
A Netlify OpenAPI client that works in the browser and Node.js.
Usage
import { NetlifyAPI } from 'netlify'
const client = new NetlifyAPI('1234myAccessToken')
const sites = await client.listSites()
Using OpenAPI operations
import { NetlifyAPI } from 'netlify'
const client = new NetlifyAPI('1234myAccessToken')
const sites = await client.listSites()
const site = await client.createSite({
body: {
name: `my-awesome-site`,
},
})
await client.deleteSite({ site_id: siteId })
API
client = new NetlifyAPI([accessToken], [opts])
Create a new instance of the Netlify API client with the provided accessToken
.
accessToken
is optional. Without it, you can't make authorized requests.
opts
includes:
const opts = {
userAgent: 'netlify/js-client',
scheme: 'https',
host: 'api.netlify.com',
pathPrefix: '/api/v1',
accessToken: '1234myAccessToken',
agent: undefined,
globalParams: {},
}
client.accessToken
A setter/getter that returns the accessToken
that the client is configured to use. You can set this after the class is
instantiated, and all subsequent calls will use the newly set accessToken
.
client.basePath
A getter that returns the formatted base URL of the endpoint the client is configured to use.
OpenAPI Client methods
The client is dynamically generated from the OpenAPI definition file. Each method
is is named after the operationId
name of each operation. To see a list of available operations, please see the
OpenAPI website.
Every OpenAPI operation has the following signature:
response = await client.operationId([params], [opts])
Performs a call to the given endpoint corresponding with the operationId
. Returns a promise resolved with the body of
the response, or rejected with an error with the details about the request attached. Rejects if the status
> 400.
params
is an object that includes any of the required or optional endpoint parameters.params.body
should be an object which gets serialized to JSON automatically. Any object can live here but refer to
the OpenAPI specification for allowed fields in a particular request body. It can also be a function returning an
object.- If the endpoint accepts
binary
, params.body
can be a Node.js readable stream or a function returning one (e.g.
() => fs.createReadStream('./foo')
). Using a function is recommended.
const params = {
any_param_needed: anyParamNeeded,
paramsCanAlsoBeCamelCase,
body: {
an: 'arbitrary js object',
},
}
Optional opts
can include any property you want passed to node-fetch
. The
headers
property is merged with some defaultHeaders
.
const opts = {
headers: {
'User-agent': 'netlify-js-client',
accept: 'application/json',
},
}
All operations are conveniently consumed with async/await:
try {
const siteDeploy = await client.getSiteDeploy({
siteId: '1234abcd',
deploy_id: '4567',
})
} catch {
}
If the response includes json
in the contentType
header, fetch will deserialize the JSON body. Otherwise the text
of the response is returned.
API Flow Methods
Some methods have been added in addition to the open API operations that make certain actions simpler to perform.
accessToken = await client.getAccessToken(ticket, [opts])
Pass in a ticket
and get back an accessToken
. Call this with the
response from a client.createTicket({ client_id })
call. Automatically sets the accessToken
to this.accessToken
and returns accessToken
for the consumer to save for later.
Optional opts
include:
const opts = {
poll: 1000,
timeout: 3.6e6,
}
See the authenticating docs for more context.
import open from 'open'
const ticket = await client.createTicket({ clientId: CLIENT_ID })
await open(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
const accessToken = await client.getAccessToken(ticket)
Proxy support
Node.js only: If this client is used behind a corporate proxy, you can pass an HttpsProxyAgent
or any other
http.Agent
that can handle your situation as agent
option:
import HttpsProxyAgent from 'https-proxy-agent'
const proxyUri = 'http(s)://[user:password@]proxyhost:port'
const agent = new HttpsProxyAgent(proxyUri)
const client = new NetlifyAPI('1234myAccessToken', { agent })
Site deployment
Support for site deployment has been removed from this package in version 7.0.0. You should consider using the
deploy
command of Netlify CLI.
License
MIT. See LICENSE for more details.