netlify
Netlify Node.js API client
Last updated a day ago by netlify-bot .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install netlify 
SYNC missed versions from official npm registry.

netlify/js-client

npm version build status coverage dependencies downloads

A Netlify OpenAPI client that works in the browser and Node.js.

Usage

const NetlifyAPI = require('netlify')

const listNetlifySites = async function () {
  const client = new NetlifyAPI('1234myAccessToken')
  const sites = await client.listSites()
  return sites
}

Using OpenAPI operations

const NetlifyAPI = require('netlify')

const client = new NetlifyAPI('1234myAccessToken')

const listCreateAndDeleteSite = async function () {
  // Fetch sites
  const sites = await client.listSites()

  // Create a site. Notice `body` here for sending OpenAPI body
  const site = await client.createSite({
    body: {
      name: `my-awesome-site`,
      // ... https://open-api.netlify.com/#/default/createSite
    },
  })

  // Delete site. Notice `site_id` is a path parameter https://open-api.netlify.com/#/default/deleteSite
  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, // e.g. HttpsProxyAgent
  globalParams: {}, // parameters you want available for every request.
  // Global params are only sent of the OpenAPI spec specifies the provided params.
}

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.
// example params
const params = {
  any_param_needed,
  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.

// example opts
const opts = {
  headers: {
    // Default headers
    'User-agent': 'netlify-js-client',
    accept: 'application/json',
  },
  // any other properties for node-fetch
}

All operations are conveniently consumed with async/await:

const getSomeData = async () => {
  // Calls may fail!
  try {
    return await client.getSiteDeploy({
      siteId: '1234abcd',
      deploy_id: '4567',
    })
  } catch (error) {
    // handle error
  }
}

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, // number of ms to wait between polling
  timeout: 3.6e6, // number of ms to wait before timing out
}

See the authenticating docs for more context.

// example:
const open = require('open') // installed with 'npm i open'

const login = async () => {
  const ticket = await client.createTicket({
    clientId: CLIENT_ID,
  })
  // Open browser for authentication
  await open(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
  const accessToken = await client.getAccessToken(ticket)
  // API is also set up to use the returned access token as a side effect
  return accessToken // Save this for later so you can quickly set up an authenticated client
}

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:

const HttpsProxyAgent = require('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.

UMD Builds

A UMD build is provided for your convenience, however browser support is still experimental. Contributions to improve browser support are welcome.

Contributing

See CONTRIBUTING.md for more info on how to make contributions to this project.

License

MIT. See LICENSE for more details.

Current Tags

  • 8.0.2                                ...           latest (a day ago)
  • 2.0.1-beta.8                                ...           next (3 years ago)

134 Versions

  • 8.0.2                                ...           a day ago
  • 8.0.1                                ...           25 days ago
  • 8.0.0                                ...           3 months ago
  • 7.0.1                                ...           4 months ago
  • 7.0.0                                ...           5 months ago
  • 6.1.29                                ...           5 months ago
  • 6.1.28                                ...           5 months ago
  • 6.1.27                                ...           5 months ago
  • 6.1.26                                ...           5 months ago
  • 6.1.25                                ...           5 months ago
  • 6.1.24                                ...           6 months ago
  • 6.1.23                                ...           6 months ago
  • 6.1.22                                ...           6 months ago
  • 6.1.21                                ...           6 months ago
  • 6.1.20                                ...           6 months ago
  • 6.1.19                                ...           6 months ago
  • 6.1.18                                ...           6 months ago
  • 6.1.17                                ...           6 months ago
  • 6.1.16                                ...           7 months ago
  • 6.1.15                                ...           7 months ago
  • 6.1.14                                ...           7 months ago
  • 6.1.13                                ...           7 months ago
  • 6.1.11                                ...           7 months ago
  • 6.1.10                                ...           8 months ago
  • 6.1.9                                ...           8 months ago
  • 6.1.8                                ...           8 months ago
  • 6.1.7                                ...           8 months ago
  • 6.1.6                                ...           9 months ago
  • 6.1.5                                ...           9 months ago
  • 6.1.4                                ...           9 months ago
  • 6.1.3                                ...           9 months ago
  • 6.1.2                                ...           9 months ago
  • 6.1.1                                ...           9 months ago
  • 6.1.0                                ...           9 months ago
  • 6.0.12                                ...           9 months ago
  • 6.0.11                                ...           9 months ago
  • 6.0.10                                ...           9 months ago
  • 6.0.9                                ...           9 months ago
  • 6.0.7                                ...           10 months ago
  • 6.0.6                                ...           10 months ago
  • 6.0.5                                ...           10 months ago
  • 6.0.4                                ...           10 months ago
  • 6.0.3                                ...           10 months ago
  • 6.0.2                                ...           10 months ago
  • 6.0.1                                ...           10 months ago
  • 6.0.0                                ...           10 months ago
  • 5.0.2                                ...           10 months ago
  • 5.0.1                                ...           10 months ago
  • 5.0.0                                ...           10 months ago
  • 4.9.0                                ...           a year ago
  • 4.8.0                                ...           a year ago
  • 4.7.0                                ...           a year ago
  • 4.6.0                                ...           a year ago
  • 4.5.2                                ...           a year ago
  • 4.5.1                                ...           a year ago
  • 4.4.0                                ...           a year ago
  • 4.3.13                                ...           a year ago
  • 4.3.12                                ...           a year ago
  • 4.3.11                                ...           a year ago
  • 4.3.10                                ...           a year ago
  • 4.3.9                                ...           a year ago
  • 4.3.8                                ...           a year ago
  • 4.3.7                                ...           a year ago
  • 4.3.6                                ...           a year ago
  • 4.3.5                                ...           a year ago
  • 4.3.4                                ...           a year ago
  • 4.3.3                                ...           a year ago
  • 4.3.2                                ...           a year ago
  • 4.3.1                                ...           a year ago
  • 4.3.0                                ...           a year ago
  • 4.2.0                                ...           a year ago
  • 4.1.8                                ...           a year ago
  • 4.1.7                                ...           a year ago
  • 4.1.6                                ...           a year ago
  • 4.1.5                                ...           a year ago
  • 4.1.4                                ...           a year ago
  • 4.1.3                                ...           a year ago
  • 4.1.2                                ...           a year ago
  • 4.1.1                                ...           a year ago
  • 4.1.0                                ...           a year ago
  • 4.0.5                                ...           2 years ago
  • 4.0.4                                ...           2 years ago
  • 4.0.3                                ...           2 years ago
  • 4.0.2                                ...           2 years ago
  • 4.0.1                                ...           2 years ago
  • 4.0.0                                ...           2 years ago
  • 3.1.0                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.4.8                                ...           2 years ago
  • 2.4.7                                ...           2 years ago
  • 2.4.6                                ...           2 years ago
  • 2.4.5                                ...           2 years ago
  • 2.4.4                                ...           2 years ago
  • 2.4.3                                ...           2 years ago
  • 2.4.2                                ...           3 years ago
  • 2.4.1                                ...           3 years ago
  • 2.4.0                                ...           3 years ago
  • 2.3.1                                ...           3 years ago
  • 2.3.0                                ...           3 years ago
  • 2.2.5                                ...           3 years ago
  • 2.2.4                                ...           3 years ago
  • 2.2.3                                ...           3 years ago
  • 2.2.2                                ...           3 years ago
  • 2.2.1                                ...           3 years ago
  • 2.2.0                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.1-beta.8                                ...           3 years ago
  • 2.0.1-beta.7                                ...           3 years ago
  • 2.0.1-beta.6                                ...           3 years ago
  • 2.0.1-beta.5                                ...           3 years ago
  • 2.0.1-beta.4                                ...           3 years ago
  • 2.0.1-beta.3                                ...           3 years ago
  • 2.0.1-beta.1                                ...           3 years ago
  • 2.0.0-beta.5                                ...           3 years ago
  • 2.0.0-beta.4                                ...           3 years ago
  • 2.0.0-beta.3                                ...           3 years ago
  • 2.0.0-beta.2                                ...           3 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           6 years ago
  • 0.3.3                                ...           6 years ago
  • 0.3.2                                ...           6 years ago
  • 0.3.1                                ...           6 years ago
  • 0.3.0                                ...           6 years ago
  • 0.2.6                                ...           6 years ago
  • 0.2.5                                ...           6 years ago
  • 0.2.4                                ...           6 years ago
  • 0.2.3                                ...           6 years ago
  • 0.2.2                                ...           6 years ago
  • 0.2.1                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
Downloads
Today 0
This Week 335
This Month 407
Last Day 31
Last Week 62
Last Month 289
Dependencies (7)
Dev Dependencies (15)

Copyright 2014 - 2021 © npmmirror.com |