20.1.0 • Published 10 days ago

@octokit/rest v20.1.0

Weekly downloads
3,286,840
License
MIT
Repository
github
Last release
10 days ago

rest.js

GitHub REST API client for Node.js

Build Status Coverage Status Greenkeeper npm

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}) => {
  // 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 scr="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}) => {
      // handle data
    })

Options

All available client options with default values

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

  // change for custom GitHub Enterprise URL
  host: 'api.github.com',
  pathPrefix: '',
  protocol: 'https',
  port: 433,

  // advanced request options
  // see https://nodejs.org/api/http.html
  proxy: undefined,
  ca: undefined,
  rejectUnauthorized: undefined,
  family: undefined
})

@octokit/rest API docs: https://octokit.github.io/rest.js/
GitHub v3 REST API docs: https://developer.github.com/v3/

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: 'integration',
  token: 'secrettoken123'
})

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

Pagination

There are a few pagination-related methods:

  • hasNextPage(response)
  • hasPreviousPage(response)
  • hasFirstPage(response)
  • hasLastPage(response)
  • getNextPage(response)
  • getPreviousPage(response)
  • getFirstPage(response)
  • getLastPage(response)

Usage

async function paginate (method) {
  let response = await method({per_page: 100})
  let {data} = response
  while (octokit.hasNextPage(response)) {
    response = await octokit.getNextPage(response)
    data = data.concat(response.data)
  }
  return data
}

paginate(octokit.repos.getAll)
  .then(data => {
    // handle all results
  })

Debug

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

Tests

Before running any tests you have to start the fixtures server

$ npm run start-fixtures-server

In a second terminal, run the tests

$ npm test

Or run a specific test

$ ./node_modules/.bin/mocha test/scenarios/get-repository-test.js

Run browser tests

$ npm run test:browser

Note: In order to run the same scenario tests in both Node and browser, we simulate the Cypress environment in Node, see test/mocha-node-setup.js.

The examples are run as part of the tests. You can set an EXAMPLES_GITHUB_TOKEN environment variable (or set it in a .env file) to avoid running against GitHub's rate limit.

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

@lerna/github-client@lerna/createlerna@tonyyanga/mintlify_cli@tonyyanga/mintlify_previewing@june21x/semantic-release-github-no-comment@falloutstudios/rmmcarbon-reactcommit-cz-fix@balena/jellyfish-plugin-github@atomist/sdm@atomist/automation-client@atomist/sdm-core@atomist/skill@bejoistic/gitfeedgist-managesite-rocketgithub-followers-notifier@manstis/test@aptoma/amp-cli@zendeskgarden/tool-shedrd-vuerdvue2@canvanauts/check-a-blockrd-vue-clidc-ginitplatform-builder@ahmadnassri/github-bulk-cloneairtable-github-json-exportrdvue-cli@balena.io/repo-configplexi-supportgit-sanalfredinoauth0-deploy-extensionsframer-tools@squeezer/travis-github@nodesoccoop/angular-ncr-srcsbuggiest-files-cli@particle/device-os-flashcreate-tfl-app2gitbuilder@lakshay35/ginit@google-cloud/code-suggestercreate-cli-hhhpage-metrics-rootresinci-deployinsomnia-plugin-stephen-strange@peerio/desktop-release-builderamazon-pacman@featureninjas/cligitfeeddepth-coveragecopy-github-labels-reloadedaz-depth-coveragesemantic-github-pullrequest@joshpress/git-cms@jessitron/automation-clientyuthi@mrjerz/plugin-scaffolder-backendginit-xyztowify-clinewman-reporter-pullrequest-decoratornewman-reporter-github-pullrequest@growlab/ginit@jaccomeijer/make@rbi-backstage/plugin-scaffolder-backend@code.gov/code-gov-integrationsfjord_app@wheelroom/make@project-furnace/furnace-cli@githaxs/action-app-wrapper@githaxs/github-application-wrapperwe-plugin-github@ahmadnassri/github-cli-authweb-compile@sdlc.vitechteam/pipe-helper@sdlc.vitechteam/sdlc-pipelines-helper@sdlc.vitechteam/sdlcpipe@wdcli/toolsqmmicro-cliweb-compressaka-cli-alphamdb-git-servicesvite-plugin-certificategroup-stats-event-schemas@wearerequired/wordpress-plugin-boilerplateringcentral-add-in-frameworkextra-githubgitssuesquick-repositoryteasim-scripts@jamiemagee/ghsa-offlinegithub-doc-server-libfomantic-ui-semeiapushinitgnccr@runcitadel/dev-toolsrepodown-clixycool
21.0.0-beta.2

10 days ago

20.1.0

23 days ago

21.0.0-beta.1

2 months ago

20.0.0-beta.5

10 months ago

20.0.0-beta.4

10 months ago

20.0.1

10 months ago

20.0.0

10 months ago

20.0.2

7 months ago

20.0.0-beta.3

10 months ago

19.0.9

11 months ago

19.0.8

12 months ago

19.0.10

11 months ago

19.0.13

10 months ago

19.0.12

11 months ago

19.0.11

11 months ago

20.0.0-beta.2

11 months ago

20.0.0-beta.1

11 months ago

19.0.7

1 year ago

19.0.6

1 year ago

19.0.5

2 years ago

19.0.1

2 years ago

19.0.0

2 years ago

19.0.3

2 years ago

19.0.2

2 years ago

19.0.4

2 years ago

18.12.0

3 years ago

18.11.3

3 years ago

18.11.4

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

3 years ago

18.6.1

3 years ago

18.5.6-beta.1

3 years ago

18.6.0

3 years ago

18.5.6

3 years ago

18.5.5

3 years ago

18.5.4

3 years ago

18.5.3

3 years ago

18.5.2

3 years ago

18.5.1

3 years ago

18.5.0

3 years ago

18.4.0

3 years ago

18.3.5

3 years ago

18.3.4

3 years ago

18.3.3

3 years ago

18.3.2

3 years ago

18.3.1

3 years ago

18.3.0

3 years ago

18.2.1

3 years ago

18.2.0

3 years ago

18.1.1

3 years ago

18.1.0

3 years ago

18.0.15

3 years ago

18.0.14

3 years ago

18.0.13

3 years ago

18.0.12

3 years ago

18.0.11

3 years ago

18.0.10

3 years ago

18.0.9

3 years ago

18.0.8

3 years ago

18.0.7

3 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

4 years ago

18.0.0

4 years ago

17.11.2

4 years ago

17.11.1

4 years ago

17.11.0

4 years ago

17.10.0

4 years ago

17.9.3

4 years ago

17.9.2

4 years ago

17.9.1

4 years ago

17.9.0

4 years ago

17.8.0

4 years ago

17.7.0

4 years ago

17.6.0

4 years ago

17.5.2

4 years ago

17.5.1

4 years ago

17.5.0

4 years ago

17.4.0

4 years ago

17.3.0

4 years ago

17.2.1

4 years ago

17.2.0

4 years ago

17.1.4

4 years ago

17.1.3

4 years ago

17.1.2

4 years ago

17.1.1

4 years ago

17.1.0

4 years ago

17.0.1

4 years ago

17.0.0

4 years ago

17.0.0-beta.3

4 years ago

17.0.0-beta.2

4 years ago

16.43.1

4 years ago

16.43.0

4 years ago

16.42.2

4 years ago

16.42.1

4 years ago

17.0.0-beta.1

4 years ago

16.42.0

4 years ago

16.41.2

4 years ago

16.41.0

4 years ago

16.41.1

4 years ago

16.40.2

4 years ago

16.40.1

4 years ago

16.40.0

4 years ago

16.39.0

4 years ago

16.38.3

4 years ago

16.38.2

4 years ago

16.38.1

4 years ago

16.38.0

4 years ago

16.37.0

4 years ago

16.36.0

4 years ago

16.35.1

4 years ago

16.35.2

4 years ago

16.35.0

4 years ago

16.34.1

4 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

5 years ago

16.28.1

5 years ago

16.28.0

5 years ago

16.27.3

5 years ago

16.27.2

5 years ago

16.27.1

5 years ago

16.27.0

5 years ago

16.26.0

5 years ago

16.25.6

5 years ago

16.25.5

5 years ago

16.25.4

5 years ago

16.25.3

5 years ago

16.25.2

5 years ago

16.25.1

5 years ago

16.25.0

5 years ago

16.24.3

5 years ago

16.24.2

5 years ago

16.24.1

5 years ago

16.24.0

5 years ago

16.23.5

5 years ago

16.23.4

5 years ago

16.23.3

5 years ago

16.23.2

5 years ago

16.23.1

5 years ago

16.23.0

5 years ago

16.22.0

5 years ago

16.21.1

5 years ago

16.21.0

5 years ago

16.20.0

5 years ago

16.19.0

5 years ago

16.18.1

5 years ago

16.18.0

5 years ago

16.17.1

5 years ago

16.17.0

5 years ago

16.16.5

5 years ago

16.16.4

5 years ago

16.16.3

5 years ago

16.16.2

5 years ago

16.16.1

5 years ago

16.16.0

5 years ago

16.15.1

5 years ago

16.15.0

5 years ago

16.14.1

5 years ago

16.14.0

5 years ago

16.13.4

5 years ago

16.13.3

5 years ago

16.13.2

5 years ago

16.13.1

5 years ago

16.13.0

5 years ago

16.12.0

5 years ago

16.11.0

5 years ago

16.10.0

5 years ago

16.9.0

5 years ago

16.8.1

5 years ago

16.8.0

5 years ago

16.7.0

5 years ago

16.6.1

5 years ago

16.6.0

5 years ago

16.5.0

5 years ago

16.4.0

5 years ago

16.3.2

5 years ago

16.3.1

5 years ago

16.3.0

5 years ago

15.18.1

5 years ago

16.2.0

5 years ago

16.1.0

5 years ago

16.0.5

5 years ago

16.0.4

5 years ago

16.0.3

5 years ago

16.0.2

5 years ago

16.0.1

5 years ago

16.0.0

5 years ago

15.18.0

5 years ago

15.17.0

5 years ago

15.16.1

5 years ago

15.16.0

5 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

6 years ago

15.9.1

6 years ago

15.9.0

6 years ago

15.8.2

6 years ago

15.8.1

6 years ago

15.8.0

6 years ago

15.7.0

6 years ago

15.6.2

6 years ago

15.6.1

6 years ago

15.6.0

6 years ago

15.5.1

6 years ago

15.5.0

6 years ago

15.4.1

6 years ago

15.4.0

6 years ago

15.3.0

6 years ago

15.2.7

6 years ago

15.2.6

6 years ago

15.2.5

6 years ago

15.2.4

6 years ago

15.2.3

6 years ago

15.2.2

6 years ago

15.2.1

6 years ago

15.2.0

6 years ago

15.1.9

6 years ago

15.1.8

6 years ago

15.1.7

6 years ago

15.1.6

6 years ago

15.1.5

6 years ago

15.1.4

6 years ago

15.1.3

6 years ago

15.1.2

6 years ago

15.1.1

6 years ago

15.1.0

6 years ago

15.0.4

6 years ago

15.0.3

6 years ago

15.0.2

6 years ago

15.0.1

6 years ago

15.0.0

6 years ago

14.0.9

6 years ago

14.0.8

6 years ago

14.0.7

6 years ago

14.0.6

6 years ago

14.0.5

6 years ago

14.0.4

6 years ago

14.0.3

6 years ago

14.0.2

6 years ago

14.0.1

6 years ago

14.0.0

6 years ago