@octokit/rest
GitHub REST API client for Node.js
Last updated 6 years ago by octokitbot .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @octokit/rest 
SYNC missed versions from official npm registry.

rest.js

GitHub REST API client for JavaScript

@latest Build Status Coverage Status Greenkeeper

Usage

Node

Install with npm install @octokit/rest.

const octokit = require('@octokit/rest')()

// Compare: https://developer.github.com/v3/repos/#list-organization-repositories
octokit.repos.getForOrg({
  org: 'octokit',
  type: 'public'
}).then(({ data, headers, status }) => {
  // handle data
})

Browser

  1. Download octokit-rest.min.js from the latest release: https://github.com/octokit/rest.js/releases

  2. Load it as script into your web application:

    <script src="octokit-rest.min.js"></script>
    
  3. Initialize octokit

    const octokit = new Octokit()
    
    // Compare: https://developer.github.com/v3/repos/#list-organization-repositories
    octokit.repos.getForOrg({
      org: 'octokit',
      type: 'public'
    }).then(({data, headers, status}) => {
      // handle data
    })
    

Client options

All available client options with default values

const octokit = require('@octokit/rest')({
  timeout: 0, // 0 means no request timeout
  headers: {
    accept: 'application/vnd.github.v3+json',
    'user-agent': 'octokit/rest.js v1.2.3' // v1.2.3 will be current version
  },

  // custom GitHub Enterprise URL
  baseUrl: 'https://api.github.com',

  // Node only: advanced request options can be passed as http(s) agent
  agent: undefined
})

API docs

Find all APIs documented at https://octokit.github.io/rest.js/.

API Previews

To take advantage of GitHub’s API Previews, pass a custom accept header, which you can do with any endpoint method documented in the API docs, e.g.

const { data: { topics } } = octokit.repos.get({
  owner: 'octokit',
  repo: 'rest.js',
  headers: {
    accept: 'application/vnd.github.mercy-preview+json'
  }
})

Multiple preview headers can be combined by separating them with commas

const { data: { topics, codeOfConduct } } = octokit.repos.get({
  owner: 'octokit',
  repo: 'rest.js',
  headers: {
    accept: 'application/vnd.github.mercy-preview+json,application/vnd.github.scarlet-witch-preview+json'
  }
})

Authentication

Most GitHub API calls don't require authentication. Rules of thumb:

  1. If you can see the information by visiting the site without being logged in, you don't have to be authenticated to retrieve the same information through the API.
  2. If you want to change data, you have to be authenticated.
// basic
octokit.authenticate({
  type: 'basic',
  username: 'yourusername',
  password: 'password'
})

// oauth
octokit.authenticate({
  type: 'oauth',
  token: 'secrettoken123'
})

// oauth key/secret (to get a token)
octokit.authenticate({
  type: 'oauth',
  key: 'client_id',
  secret: 'client_secret'
})

// token (https://github.com/settings/tokens)
octokit.authenticate({
  type: 'token',
  token: 'secrettoken123'
})

// GitHub app
octokit.authenticate({
  type: 'app',
  token: 'secrettoken123'
})

Note: authenticate is synchronous because it only sets the credentials for the following requests.

Custom requests

To send custom requests you can use the lower-level octokit.request() method

octokit.request('GET /')

The baseUrl, headers and other defaults are already set. For more information on the octokit.request() API see @octokit/request

All the endpoint methods such as octokit.repos.get() are aliases of octokit.request() with pre-bound default options. So you can use the @octokit/request API to get the default options or get generic request option to use with your prefered request library.

const defaultOptions = octokit.repos.get.endpoint.DEFAULTS
const requestOptions = octokit.repos.get.endpoint()

Pagination

All endpoint methods starting with .list* do not return all responses at once but instead return the first 30 items by default, see also GitHub’s REST API pagination documentation.

To automatically receive all results across all pages, you can use the github.paginate() method:

octokit.paginate('GET /repos/:owner/:repo/issues', { owner: 'octokit', repo: 'rest.js' })
  .then(issues => {
    // issues is an array of all issue objects
  })

octokit.paginate() accepts the same options as octokit.request(). You can optionally pass an additional function to map the results from each response. The map mast return a new value, usually an array with mapped data.

octokit.paginate('GET /repos/:owner/:repo/issues', { owner: 'octokit', repo: 'rest.js' }, response => response.data.map(issue => issue.title))
  .then(issueTitles => {
    // issueTitles is now an array with the titles only
  })

To paginate responses for one of the registered endpoint methods such as octokit.issues.listForRepo() you can use the .endpoint.merge() method registered for all endpoint methods:

const options = octokit.issues.listForRepo.endpoint.merge({ owner: 'octokit', repo: 'rest.js' })
octokit.paginate(options)
  .then(issues => {
    // issues is an array of all issue objects
  })

If your runtime environment supports async iterators (such as Node 10+), you can iterate trough each response

for await (const response of github.paginate.iterator(options) {
  // do whatever you want with each response, break out of loop, etc.
}

github.paginate.iterator() accepts the same options as github.paginate().

Hooks

You can customize Octokit’s request lifecycle with hooks. Available methods are

github.hook.before('request', async (options) => {
  validate(options)
})
github.hook.after('request', async (response, options) => {
  console.log(`${options.method} ${options.url}: ${response.status}`)
})
github.hook.error('request', async (error, options) => {
  if (error.status === 304) {
    return findInCache(error.headers.etag)
  }

  throw error
})
github.hook.wrap('request', async (request, options) => {
  // add logic before, after, catch errors or replace the request altogether
  return request(options)
})

See before-after-hook for more documentation on hooks.

Plugins

You can customize and extend Octokit’s functionality using plugins

// index.js
const MyOctokit = require('@octokit/request')
  .plugin([
    require('./lib/my-plugin'),
    require('octokit-plugin-example')
  ])

// lib/my-plugin.js
module.exports = (octokit, options = { greeting: 'Hello' }) => {
  // add a custom method
  octokit.helloWorld = () => console.log(`${options.greeting}, world!`)

  // hook into the request lifecycle
  octokit.hook.wrap('request', async (request, options) => {
    const time = Date.now()
    const response = await request(options)
    console.log(`${options.method} ${options.url} – ${response.status} in ${Date.now() - time}ms`)
    return response
  })
}

.plugin accepts a function or an array of functions.

You can add new methods to the octokit instance passed as first argument to the plugin function. The 2nd argument is the options object passed to the constructor when instantiating the octokit client.

const octokit = new MyOctokit({ greeting: 'Hola' })
octokit.helloWorld()
// Hola, world!

Register custom endpoint methods

You can register custom endpoint methods such as octokit.repos.get() using the octokit.registerEndpoints(routes) method

octokit.registerEndpoints({
  foo: {
    bar: {
      method: 'PATCH',
      url: '/repos/:owner/:repo/foo',
      headers: {
        accept: 'application/vnd.github.foo-bar-preview+json'
      },
      params: {
        owner: {
          required: true,
          type: 'string'
        },
        repo: {
          required: true,
          type: 'string'
        },
        baz: {
          required: true,
          type: 'string',
          enum: [
            'qux',
            'quux',
            'quuz'
          ]
        }
      }
    }
  }
})

octokit.foo.bar({
  owner: 'octokit',
  repo: 'rest.js',
  baz: 'quz'
})

This is useful when you participate in private beta features and prefer the convenience of methods for the new endpoints instead of using octokit.request().

Debug

Set DEBUG=octokit:rest* for additional debug logs.

Contributing

We would love you to contribute to @octokit/rest, pull requests are very welcomed! Please see CONTRIBUTING.md for more information.

Credits

@octokit/rest was originally created as node-github in 2012 by Mike de Boer from Cloud9 IDE, Inc. It was adopted and renamed by GitHub in 2017

LICENSE

MIT

Current Tags

  • 21.0.0-beta.4                                ...           beta (6 months ago)
  • 15.18.1                                ...           deprecations-for-v16 (6 years ago)
  • 21.0.2                                ...           latest (4 months ago)
  • 15.18.3                                ...           release-15.x (5 years ago)
  • 16.43.2                                ...           release-16.x (5 years ago)
  • 17.11.0                                ...           release-17.x (5 years ago)
  • 18.12.0                                ...           release-18.x (3 years ago)
  • 19.0.13                                ...           release-19.x (2 years ago)
  • 20.1.0                                ...           release-20.x (9 months ago)

297 Versions

  • 21.0.2                                ...           4 months ago
  • 21.0.1                                ...           5 months ago
  • 21.0.0                                ...           6 months ago
  • 21.0.0-beta.4                                ...           6 months ago
  • 20.1.1                                ...           8 months ago
  • 21.0.0-beta.3                                ...           8 months ago
  • 21.0.0-beta.2                                ...           8 months ago
  • 20.1.0                                ...           9 months ago
  • 21.0.0-beta.1                                ...           10 months ago
  • 20.0.2                                ...           a year ago
  • 20.0.1                                ...           a year ago
  • 20.0.0                                ...           a year ago
  • 20.0.0-beta.5                                ...           a year ago
  • 20.0.0-beta.4                                ...           a year ago
  • 20.0.0-beta.3                                ...           a year ago
  • 19.0.13                                ...           2 years ago
  • 19.0.12                                ...           2 years ago
  • 20.0.0-beta.2                                ...           2 years ago
  • 20.0.0-beta.1                                ...           2 years ago
  • 19.0.11                                ...           2 years ago
  • 19.0.10                                ...           2 years ago
  • 19.0.9                                ...           2 years ago
  • 19.0.8                                ...           2 years ago
  • 19.0.7                                ...           2 years ago
  • 19.0.6                                ...           2 years ago
  • 19.0.5                                ...           2 years ago
  • 19.0.4                                ...           2 years ago
  • 19.0.3                                ...           2 years ago
  • 19.0.2                                ...           2 years ago
  • 19.0.1                                ...           2 years ago
  • 19.0.0                                ...           2 years ago
  • 18.12.0                                ...           3 years ago
  • 18.11.4                                ...           3 years ago
  • 18.11.3                                ...           3 years ago
  • 18.11.2                                ...           3 years ago
  • 18.11.1                                ...           3 years ago
  • 18.11.0                                ...           3 years ago
  • 18.10.0                                ...           3 years ago
  • 18.9.1                                ...           3 years ago
  • 18.9.0                                ...           3 years ago
  • 18.8.0                                ...           3 years ago
  • 18.7.2                                ...           3 years ago
  • 18.7.1                                ...           3 years ago
  • 18.7.0                                ...           3 years ago
  • 18.6.8                                ...           3 years ago
  • 18.6.7                                ...           3 years ago
  • 18.6.6                                ...           3 years ago
  • 18.6.5                                ...           3 years ago
  • 18.6.4                                ...           3 years ago
  • 18.6.3                                ...           3 years ago
  • 18.6.2                                ...           4 years ago
  • 18.6.1                                ...           4 years ago
  • 18.6.0                                ...           4 years ago
  • 18.5.6                                ...           4 years ago
  • 18.5.6-beta.1                                ...           4 years ago
  • 18.5.5                                ...           4 years ago
  • 18.5.4                                ...           4 years ago
  • 18.5.3                                ...           4 years ago
  • 18.5.2                                ...           4 years ago
  • 18.5.1                                ...           4 years ago
  • 18.5.0                                ...           4 years ago
  • 18.4.0                                ...           4 years ago
  • 18.3.5                                ...           4 years ago
  • 18.3.4                                ...           4 years ago
  • 18.3.3                                ...           4 years ago
  • 18.3.2                                ...           4 years ago
  • 18.3.1                                ...           4 years ago
  • 18.3.0                                ...           4 years ago
  • 18.2.1                                ...           4 years ago
  • 18.2.0                                ...           4 years ago
  • 18.1.1                                ...           4 years ago
  • 18.1.0                                ...           4 years ago
  • 18.0.15                                ...           4 years ago
  • 18.0.14                                ...           4 years ago
  • 18.0.13                                ...           4 years ago
  • 18.0.12                                ...           4 years ago
  • 18.0.11                                ...           4 years ago
  • 18.0.10                                ...           4 years ago
  • 18.0.9                                ...           4 years ago
  • 18.0.8                                ...           4 years ago
  • 18.0.7                                ...           4 years ago
  • 18.0.6                                ...           4 years ago
  • 18.0.5                                ...           4 years ago
  • 18.0.4                                ...           4 years ago
  • 18.0.3                                ...           4 years ago
  • 18.0.2                                ...           4 years ago
  • 18.0.1                                ...           4 years ago
  • 16.43.2                                ...           5 years ago
  • 18.0.0                                ...           5 years ago
  • 17.11.2                                ...           5 years ago
  • 17.11.1                                ...           5 years ago
  • 17.11.0                                ...           5 years ago
  • 17.10.0                                ...           5 years ago
  • 17.9.3                                ...           5 years ago
  • 17.9.2                                ...           5 years ago
  • 17.9.1                                ...           5 years ago
  • 17.9.0                                ...           5 years ago
  • 17.8.0                                ...           5 years ago
  • 17.7.0                                ...           5 years ago
  • 17.6.0                                ...           5 years ago
  • 17.5.2                                ...           5 years ago
  • 17.5.1                                ...           5 years ago
  • 17.5.0                                ...           5 years ago
  • 17.4.0                                ...           5 years ago
  • 17.3.0                                ...           5 years ago
  • 17.2.1                                ...           5 years ago
  • 17.2.0                                ...           5 years ago
  • 17.1.4                                ...           5 years ago
  • 17.1.3                                ...           5 years ago
  • 17.1.2                                ...           5 years ago
  • 17.1.1                                ...           5 years ago
  • 17.1.0                                ...           5 years ago
  • 17.0.1                                ...           5 years ago
  • 17.0.0                                ...           5 years ago
  • 17.0.0-beta.3                                ...           5 years ago
  • 17.0.0-beta.2                                ...           5 years ago
  • 16.43.1                                ...           5 years ago
  • 16.43.0                                ...           5 years ago
  • 16.42.2                                ...           5 years ago
  • 16.42.1                                ...           5 years ago
  • 16.42.0                                ...           5 years ago
  • 17.0.0-beta.1                                ...           5 years ago
  • 16.41.2                                ...           5 years ago
  • 16.41.1                                ...           5 years ago
  • 16.41.0                                ...           5 years ago
  • 16.40.2                                ...           5 years ago
  • 16.40.1                                ...           5 years ago
  • 16.40.0                                ...           5 years ago
  • 16.39.0                                ...           5 years ago
  • 16.38.3                                ...           5 years ago
  • 16.38.2                                ...           5 years ago
  • 16.38.1                                ...           5 years ago
  • 16.38.0                                ...           5 years ago
  • 16.37.0                                ...           5 years ago
  • 16.36.0                                ...           5 years ago
  • 16.35.2                                ...           5 years ago
  • 16.35.1                                ...           5 years ago
  • 16.35.0                                ...           5 years ago
  • 16.34.1                                ...           5 years ago
  • 16.34.0                                ...           5 years ago
  • 16.33.1                                ...           5 years ago
  • 16.33.0                                ...           5 years ago
  • 16.32.0                                ...           5 years ago
  • 16.31.0                                ...           5 years ago
  • 16.30.2                                ...           5 years ago
  • 16.30.1                                ...           5 years ago
  • 16.30.0                                ...           5 years ago
  • 16.29.0                                ...           5 years ago
  • 16.28.9                                ...           5 years ago
  • 16.28.8                                ...           5 years ago
  • 15.18.3                                ...           5 years ago
  • 15.18.2                                ...           5 years ago
  • 16.28.7                                ...           5 years ago
  • 16.28.6                                ...           5 years ago
  • 16.28.5                                ...           5 years ago
  • 16.28.4                                ...           5 years ago
  • 16.28.3                                ...           5 years ago
  • 16.28.2                                ...           6 years ago
  • 16.28.1                                ...           6 years ago
  • 16.28.0                                ...           6 years ago
  • 16.27.3                                ...           6 years ago
  • 16.27.2                                ...           6 years ago
  • 16.27.1                                ...           6 years ago
  • 16.27.0                                ...           6 years ago
  • 16.26.0                                ...           6 years ago
  • 16.25.6                                ...           6 years ago
  • 16.25.5                                ...           6 years ago
  • 16.25.4                                ...           6 years ago
  • 16.25.3                                ...           6 years ago
  • 16.25.2                                ...           6 years ago
  • 16.25.1                                ...           6 years ago
  • 16.25.0                                ...           6 years ago
  • 16.24.3                                ...           6 years ago
  • 16.24.2                                ...           6 years ago
  • 16.24.1                                ...           6 years ago
  • 16.24.0                                ...           6 years ago
  • 16.23.5                                ...           6 years ago
  • 16.23.4                                ...           6 years ago
  • 16.23.3                                ...           6 years ago
  • 16.23.2                                ...           6 years ago
  • 16.23.1                                ...           6 years ago
  • 16.23.0                                ...           6 years ago
  • 16.22.0                                ...           6 years ago
  • 16.21.1                                ...           6 years ago
  • 16.21.0                                ...           6 years ago
  • 16.20.0                                ...           6 years ago
  • 16.19.0                                ...           6 years ago
  • 16.18.1                                ...           6 years ago
  • 16.18.0                                ...           6 years ago
  • 16.17.1                                ...           6 years ago
  • 16.17.0                                ...           6 years ago
  • 16.16.5                                ...           6 years ago
  • 16.16.4                                ...           6 years ago
  • 16.16.3                                ...           6 years ago
  • 16.16.2                                ...           6 years ago
  • 16.16.1                                ...           6 years ago
  • 16.16.0                                ...           6 years ago
  • 16.15.1                                ...           6 years ago
  • 16.15.0                                ...           6 years ago
  • 16.14.1                                ...           6 years ago
  • 16.14.0                                ...           6 years ago
  • 16.13.4                                ...           6 years ago
  • 16.13.3                                ...           6 years ago
  • 16.13.2                                ...           6 years ago
  • 16.13.1                                ...           6 years ago
  • 16.13.0                                ...           6 years ago
  • 16.12.0                                ...           6 years ago
  • 16.11.0                                ...           6 years ago
  • 16.10.0                                ...           6 years ago
  • 16.9.0                                ...           6 years ago
  • 16.8.1                                ...           6 years ago
  • 16.8.0                                ...           6 years ago
  • 16.7.0                                ...           6 years ago
  • 16.6.1                                ...           6 years ago
  • 16.6.0                                ...           6 years ago
  • 16.5.0                                ...           6 years ago
  • 16.4.0                                ...           6 years ago
  • 16.3.2                                ...           6 years ago
  • 16.3.1                                ...           6 years ago
  • 16.3.0                                ...           6 years ago
  • 15.18.1                                ...           6 years ago
  • 16.2.0                                ...           6 years ago
  • 16.1.0                                ...           6 years ago
  • 16.0.5                                ...           6 years ago
  • 16.0.4                                ...           6 years ago
  • 16.0.3                                ...           6 years ago
  • 16.0.2                                ...           6 years ago
  • 16.0.1                                ...           6 years ago
  • 16.0.0                                ...           6 years ago
  • 15.18.0                                ...           6 years ago
  • 15.17.0                                ...           6 years ago
  • 15.16.1                                ...           6 years ago
  • 15.16.0                                ...           6 years ago
  • 15.15.1                                ...           6 years ago
  • 15.15.0                                ...           6 years ago
  • 15.14.0                                ...           6 years ago
  • 15.13.1                                ...           6 years ago
  • 15.13.0                                ...           6 years ago
  • 15.12.1                                ...           6 years ago
  • 15.12.0                                ...           6 years ago
  • 15.11.4                                ...           6 years ago
  • 15.11.3                                ...           6 years ago
  • 15.11.2                                ...           6 years ago
  • 15.11.1                                ...           6 years ago
  • 15.11.0                                ...           6 years ago
  • 15.10.0                                ...           6 years ago
  • 15.9.6                                ...           6 years ago
  • 15.9.5                                ...           6 years ago
  • 15.9.4                                ...           6 years ago
  • 15.9.3                                ...           6 years ago
  • 15.9.2                                ...           7 years ago
  • 15.9.1                                ...           7 years ago
  • 15.9.0                                ...           7 years ago
  • 15.8.2                                ...           7 years ago
  • 15.8.1                                ...           7 years ago
  • 15.8.0                                ...           7 years ago
  • 15.7.0                                ...           7 years ago
  • 15.6.2                                ...           7 years ago
  • 15.6.1                                ...           7 years ago
  • 15.6.0                                ...           7 years ago
  • 15.5.1                                ...           7 years ago
  • 15.5.0                                ...           7 years ago
  • 15.4.1                                ...           7 years ago
  • 15.4.0                                ...           7 years ago
  • 15.3.0                                ...           7 years ago
  • 15.2.7                                ...           7 years ago
  • 15.2.6                                ...           7 years ago
  • 15.2.5                                ...           7 years ago
  • 15.2.4                                ...           7 years ago
  • 15.2.3                                ...           7 years ago
  • 15.2.2                                ...           7 years ago
  • 15.2.1                                ...           7 years ago
  • 15.2.0                                ...           7 years ago
  • 15.1.9                                ...           7 years ago
  • 15.1.8                                ...           7 years ago
  • 15.1.7                                ...           7 years ago
  • 15.1.6                                ...           7 years ago
  • 15.1.5                                ...           7 years ago
  • 15.1.4                                ...           7 years ago
  • 15.1.3                                ...           7 years ago
  • 15.1.2                                ...           7 years ago
  • 15.1.1                                ...           7 years ago
  • 15.1.0                                ...           7 years ago
  • 15.0.4                                ...           7 years ago
  • 15.0.3                                ...           7 years ago
  • 15.0.2                                ...           7 years ago
  • 15.0.1                                ...           7 years ago
  • 14.0.9                                ...           7 years ago
  • 14.0.8                                ...           7 years ago
  • 14.0.7                                ...           7 years ago
  • 14.0.6                                ...           7 years ago
  • 14.0.5                                ...           7 years ago
  • 14.0.4                                ...           7 years ago
  • 14.0.3                                ...           7 years ago
  • 14.0.2                                ...           7 years ago
  • 14.0.1                                ...           7 years ago
  • 14.0.0                                ...           7 years ago
Downloads
Total 4
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (11)
Dev Dependencies (35)

© 2010 - cnpmjs.org x YWFE | Home | YWFE